Class: MassiveRecord::ORM::Schema::Field
- Inherits:
-
Object
- Object
- MassiveRecord::ORM::Schema::Field
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/massive_record/orm/schema/field.rb
Constant Summary collapse
- TYPES =
[:string, :integer, :float, :boolean, :array, :hash, :date, :time]
Instance Attribute Summary collapse
-
#column ⇒ Object
Returns the value of attribute column.
- #default ⇒ Object
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
-
.new_with_arguments_from_dsl(*args) ⇒ Object
Creates a new field based on arguments from DSL args: name, type, options.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #column_family ⇒ Object
- #decode(value) ⇒ Object
- #hash ⇒ Object
-
#initialize(*args) ⇒ Field
constructor
A new instance of Field.
- #unique_name ⇒ Object
Constructor Details
#initialize(*args) ⇒ Field
Returns a new instance of Field.
35 36 37 38 39 40 41 42 43 |
# File 'lib/massive_record/orm/schema/field.rb', line 35 def initialize(*args) = args.. self.fields = [:fields] self.name = [:name] self.column = [:column] self.type = [:type] || :string self.default = [:default] end |
Instance Attribute Details
#column ⇒ Object
Returns the value of attribute column.
10 11 12 |
# File 'lib/massive_record/orm/schema/field.rb', line 10 def column @column end |
#default ⇒ Object
64 65 66 |
# File 'lib/massive_record/orm/schema/field.rb', line 64 def default @default.duplicable? ? @default.dup : @default end |
#fields ⇒ Object
Returns the value of attribute fields.
10 11 12 |
# File 'lib/massive_record/orm/schema/field.rb', line 10 def fields @fields end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/massive_record/orm/schema/field.rb', line 10 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
10 11 12 |
# File 'lib/massive_record/orm/schema/field.rb', line 10 def type @type end |
Class Method Details
.new_with_arguments_from_dsl(*args) ⇒ Object
Creates a new field based on arguments from DSL args: name, type, options
25 26 27 28 29 30 31 |
# File 'lib/massive_record/orm/schema/field.rb', line 25 def self.new_with_arguments_from_dsl(*args) = args. [:name] = args[0] [:type] = args[1] new() end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
46 47 48 |
# File 'lib/massive_record/orm/schema/field.rb', line 46 def ==(other) other.instance_of?(self.class) && other.hash == hash end |
#column_family ⇒ Object
74 75 76 |
# File 'lib/massive_record/orm/schema/field.rb', line 74 def column_family fields.try :contained_in end |
#decode(value) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/massive_record/orm/schema/field.rb', line 86 def decode(value) return nil if value.nil? if type == :boolean return value if value === TrueClass || value === FalseClass else return value if value.class == type.to_s.classify.constantize end case type when :string value when :boolean value.to_s.empty? ? nil : !value.to_s.match(/^(true|1)$/i).nil? when :integer value.to_s.empty? ? nil : value.to_i when :float value.to_s.empty? ? nil : value.to_f when :date # TODO : find a nicer way to do that value.empty? || value.to_s == "0" ? nil : Date.parse(value) when :time value.empty? ? nil : Time.parse(value) when :array value when :hash value else value end end |
#hash ⇒ Object
51 52 53 |
# File 'lib/massive_record/orm/schema/field.rb', line 51 def hash name.hash end |
#unique_name ⇒ Object
69 70 71 72 |
# File 'lib/massive_record/orm/schema/field.rb', line 69 def unique_name raise "Can't generate a unique name as I don't have a column family!" if column_family.nil? [column_family.name, column].join(":") end |