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.
- .type_to_classes ⇒ Object
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.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/massive_record/orm/schema/field.rb', line 57 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
92 93 94 95 96 97 98 99 |
# File 'lib/massive_record/orm/schema/field.rb', line 92 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
47 48 49 50 51 52 53 |
# File 'lib/massive_record/orm/schema/field.rb', line 47 def self.new_with_arguments_from_dsl(*args) = args. [:name] = args[0] [:type] ||= args[1] new() end |
.type_to_classes ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/massive_record/orm/schema/field.rb', line 32 def self.type_to_classes unless @type_to_classes @type_to_classes = Hash.new { |h,k| h[k] = [] } @type_to_classes[:boolean] << TrueClass << FalseClass @type_to_classes[:integer] << Fixnum << Bignum end @type_to_classes end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
74 75 76 |
# File 'lib/massive_record/orm/schema/field.rb', line 74 def ==(other) other.instance_of?(self.class) && other.hash == hash end |
#allow_nil? ⇒ Boolean
116 117 118 |
# File 'lib/massive_record/orm/schema/field.rb', line 116 def allow_nil? !!allow_nil end |
#column_family ⇒ Object
107 108 109 |
# File 'lib/massive_record/orm/schema/field.rb', line 107 def column_family fields.try :contained_in end |
#decode(value) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/massive_record/orm/schema/field.rb', line 122 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 value = value.force_encoding(Encoding::BINARY) 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
158 159 160 161 162 163 164 165 |
# File 'lib/massive_record/orm/schema/field.rb', line 158 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
79 80 81 |
# File 'lib/massive_record/orm/schema/field.rb', line 79 def hash name.hash end |
#unique_name ⇒ Object
102 103 104 105 |
# File 'lib/massive_record/orm/schema/field.rb', line 102 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 |