Class: MetaModel::Record::Property
- Inherits:
-
Object
- Object
- MetaModel::Record::Property
- Defined in:
- lib/metamodel/record/property.rb
Instance Attribute Summary collapse
-
#modifiers ⇒ Object
readonly
Returns the value of attribute modifiers.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #convert_symbol_to_type(symbol) ⇒ Object
- #database_type ⇒ Object
- #default_value ⇒ Object
- #has_default_value? ⇒ Boolean
-
#initialize(json_key, type = :string, *modifiers) ⇒ Property
constructor
A new instance of Property.
- #is_array? ⇒ Boolean
- #is_optional? ⇒ Boolean
- #is_primary? ⇒ Boolean
- #is_unique? ⇒ Boolean
- #real_type ⇒ Object
- #type_without_optional ⇒ Object
Constructor Details
#initialize(json_key, type = :string, *modifiers) ⇒ Property
Returns a new instance of Property.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/metamodel/record/property.rb', line 8 def initialize(json_key, type = :string, *modifiers) @name = json_key.to_s.camelize(:lower) @type = convert_symbol_to_type type @modifiers = {} @modifiers.default = false modifiers.flatten.map do |modifier| @modifiers[modifier] = true if modifier.is_a? Symbol @modifiers[:default] = modifier[:default] if modifier.is_a? Hash and modifier[:default] end end |
Instance Attribute Details
#modifiers ⇒ Object (readonly)
Returns the value of attribute modifiers.
6 7 8 |
# File 'lib/metamodel/record/property.rb', line 6 def modifiers @modifiers end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/metamodel/record/property.rb', line 4 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/metamodel/record/property.rb', line 5 def type @type end |
Class Method Details
Instance Method Details
#convert_symbol_to_type(symbol) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/metamodel/record/property.rb', line 52 def convert_symbol_to_type(symbol) case symbol when :int then "Int" when :double then "Double" when :bool then "Bool" when :string then "String" when :date then "Date" else symbol.to_s.camelize end end |
#database_type ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/metamodel/record/property.rb', line 34 def database_type case type_without_optional when "String" then "TEXT" when "Int", "Bool" then "INTEGER" when "Double", "Date", "Float" then "REAL" else raise Informative, "Unsupported type #{self.type}" end end |
#default_value ⇒ Object
84 85 86 |
# File 'lib/metamodel/record/property.rb', line 84 def default_value has_default_value? ? modifiers[:default] : "" end |
#has_default_value? ⇒ Boolean
80 81 82 |
# File 'lib/metamodel/record/property.rb', line 80 def has_default_value? !!@modifiers[:default] end |
#is_array? ⇒ Boolean
64 65 66 |
# File 'lib/metamodel/record/property.rb', line 64 def is_array? @type.pluralize == str end |
#is_optional? ⇒ Boolean
76 77 78 |
# File 'lib/metamodel/record/property.rb', line 76 def is_optional? @type.to_s.end_with? "?" end |
#is_primary? ⇒ Boolean
72 73 74 |
# File 'lib/metamodel/record/property.rb', line 72 def is_primary? @modifiers.include? :primary end |
#is_unique? ⇒ Boolean
68 69 70 |
# File 'lib/metamodel/record/property.rb', line 68 def is_unique? @modifiers.include? :unique end |
#real_type ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/metamodel/record/property.rb', line 43 def real_type case type_without_optional when "String" then "String" when "Int", "Bool" then "Int64" when "Double", "Date", "Float" then "Double" else raise Informative, "Unsupported type #{self.type}" end end |
#type_without_optional ⇒ Object
29 30 31 32 |
# File 'lib/metamodel/record/property.rb', line 29 def type_without_optional return type.to_s[0..-2] if type.to_s.end_with? "?" type end |