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_DEFAULTS_TO =
{ :string => '', :integer => 0, :float => 0.0, :boolean => false, :array => [], :hash => {}, :date => lambda { Date.today }, :time => lambda { Time.now } }.freeze
- TYPES =
TYPES_DEFAULTS_TO.keys.freeze
Instance Attribute Summary collapse
-
#allow_nil ⇒ Object
Returns the value of attribute allow_nil.
-
#coder ⇒ Object
Returns the value of attribute coder.
-
#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?)
- #allow_nil? ⇒ Boolean
- #column_family ⇒ Object
- #decode(value) ⇒ Object
- #encode(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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/massive_record/orm/schema/field.rb', line 46 def initialize(*args) = args.. self.fields = [:fields] self.name = [:name] self.column = [:column] self.type = [:type] || :string self.default = [:default] self.allow_nil = .has_key?(:allow_nil) ? [:allow_nil] : true self.coder = [:coder] || Base.coder @@encoded_nil_value = coder.dump(nil) @@encoded_null_string = coder.dump("null") end |
Instance Attribute Details
#allow_nil ⇒ Object
Returns the value of attribute allow_nil.
21 22 23 |
# File 'lib/massive_record/orm/schema/field.rb', line 21 def allow_nil @allow_nil end |
#coder ⇒ Object
Returns the value of attribute coder.
21 22 23 |
# File 'lib/massive_record/orm/schema/field.rb', line 21 def coder @coder end |
#column ⇒ Object
Returns the value of attribute column.
21 22 23 |
# File 'lib/massive_record/orm/schema/field.rb', line 21 def column @column end |
#default ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/massive_record/orm/schema/field.rb', line 81 def default @default = TYPES_DEFAULTS_TO[type] if !allow_nil? && @default.nil? if @default.respond_to? :call @default.call else @default.duplicable? ? @default.dup : @default end end |
#fields ⇒ Object
Returns the value of attribute fields.
21 22 23 |
# File 'lib/massive_record/orm/schema/field.rb', line 21 def fields @fields end |
#name ⇒ Object
Returns the value of attribute name.
21 22 23 |
# File 'lib/massive_record/orm/schema/field.rb', line 21 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
21 22 23 |
# File 'lib/massive_record/orm/schema/field.rb', line 21 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
36 37 38 39 40 41 42 |
# File 'lib/massive_record/orm/schema/field.rb', line 36 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?
63 64 65 |
# File 'lib/massive_record/orm/schema/field.rb', line 63 def ==(other) other.instance_of?(self.class) && other.hash == hash end |
#allow_nil? ⇒ Boolean
105 106 107 |
# File 'lib/massive_record/orm/schema/field.rb', line 105 def allow_nil? !!allow_nil end |
#column_family ⇒ Object
96 97 98 |
# File 'lib/massive_record/orm/schema/field.rb', line 96 def column_family fields.try :contained_in end |
#decode(value) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/massive_record/orm/schema/field.rb', line 111 def decode(value) value = value.force_encoding(Encoding::UTF_8) if utf_8_encoded? && !value.frozen? && value.respond_to?(:force_encoding) return value if value.nil? || value_is_already_decoded?(value) value = case type when :boolean value.blank? || value == @@encoded_nil_value ? nil : !value.to_s.match(/^(true|1)$/i).nil? when :date value.blank? || value.to_s == "0" ? nil : (Date.parse(value) rescue nil) when :time value.blank? ? nil : (Time.parse(value) rescue nil) when :string if value.present? value = value.to_s if value.is_a? Symbol coder.load(value) end when :integer if value =~ /\A\d*\Z/ coder.load(value) if value.present? else hex_string_to_integer(value) end when :float, :array, :hash coder.load(value) if value.present? else raise "Unable to decode #{value}, class: #{value}" end ensure unless loaded_value_is_of_valid_class?(value) raise SerializationTypeMismatch.new("Expected #{value} (class: #{value.class}) to be any of: #{classes.join(', ')}.") end end |
#encode(value) ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'lib/massive_record/orm/schema/field.rb', line 145 def encode(value) if value.nil? || should_not_be_encoded? value else value = value.try(:utc) if Base.time_zone_aware_attributes && field_affected_by_time_zone_awareness? coder.dump(value).to_s end end |
#hash ⇒ Object
68 69 70 |
# File 'lib/massive_record/orm/schema/field.rb', line 68 def hash name.hash end |
#unique_name ⇒ Object
91 92 93 94 |
# File 'lib/massive_record/orm/schema/field.rb', line 91 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 |