Class: Roda::Project::Bin::Generators::Migration::FieldParser
- Inherits:
-
Object
- Object
- Roda::Project::Bin::Generators::Migration::FieldParser
- Includes:
- Helpers::Inflections
- Defined in:
- lib/roda/project/bin/generators/migration/field_parser.rb
Constant Summary collapse
- TYPE_MAP =
{ "string" => "String", "text" => "String", "integer" => "Integer", "bigint" => "Bignum", "float" => "Float", "decimal" => "BigDecimal", "datetime" => "DateTime", "timestamp" => "DateTime", "time" => "Time", "date" => "Date", "boolean" => "TrueClass", "binary" => "File", "json" => ":json", "jsonb" => ":jsonb", "uuid" => ":uuid" }.freeze
Instance Attribute Summary collapse
-
#raw_fields ⇒ Object
readonly
Returns the value of attribute raw_fields.
Instance Method Summary collapse
-
#initialize(raw_fields) ⇒ FieldParser
constructor
A new instance of FieldParser.
- #parse ⇒ Object
Methods included from Helpers::Inflections
camelize, plural?, pluralize, singular?, singularize, underscore
Constructor Details
#initialize(raw_fields) ⇒ FieldParser
Returns a new instance of FieldParser.
31 32 33 |
# File 'lib/roda/project/bin/generators/migration/field_parser.rb', line 31 def initialize(raw_fields) @raw_fields = raw_fields || [] end |
Instance Attribute Details
#raw_fields ⇒ Object (readonly)
Returns the value of attribute raw_fields.
29 30 31 |
# File 'lib/roda/project/bin/generators/migration/field_parser.rb', line 29 def raw_fields @raw_fields end |
Instance Method Details
#parse ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/roda/project/bin/generators/migration/field_parser.rb', line 35 def parse fields = [] raw_fields.each do |raw_field| parts = raw_field.to_s.split(":") next if parts.empty? name = parts[0] type_part = parts[1] || "string" index_part = parts[2] # Extract modifier e.g. string{50}, decimal{10.2}, references{polymorphic} type, modifier = extract_type_and_modifier(type_part) if type == "references" || type == "belongs_to" if modifier == "polymorphic" fields << { name: "#{name}_id", type: "Bignum", options: {}, index: false, composite_index: ["#{name}_type", "#{name}_id"] } fields << { name: "#{name}_type", type: "String", options: {}, index: false } else singular_name = singularize(name) plural_table = pluralize(singular_name) fields << { name: "#{name}_id", is_reference: true, target_table: plural_table, index: true, options: {} } end else sequel_type = TYPE_MAP[type] || "String" = {} [:text] = true if type == "text" if modifier if modifier.include?(".") prec, scale = modifier.split(".").map(&:to_i) [:size] = [prec, scale] elsif /^\d+$/.match?(modifier) [:size] = modifier.to_i end end index_val = parse_index_spec(index_part) fields << { name: name, type: sequel_type, options: , index: index_val } end end fields end |