Class: MassiveRecord::ORM::Schema::Field

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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)
  options = args.extract_options!.to_options

  self.fields = options[:fields]
  self.name = options[:name]
  self.column = options[:column]
  self.type = options[:type] || :string
  self.default = options[:default]
  self.allow_nil = options.has_key?(:allow_nil) ? options[:allow_nil] : true

  self.coder = options[:coder] || Base.coder

  @@encoded_nil_value = coder.dump(nil)
  @@encoded_null_string = coder.dump("null")
end

Instance Attribute Details

#allow_nilObject

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

#coderObject

Returns the value of attribute coder.



21
22
23
# File 'lib/massive_record/orm/schema/field.rb', line 21

def coder
  @coder
end

#columnObject

Returns the value of attribute column.



21
22
23
# File 'lib/massive_record/orm/schema/field.rb', line 21

def column
  @column
end

#defaultObject



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

#fieldsObject

Returns the value of attribute fields.



21
22
23
# File 'lib/massive_record/orm/schema/field.rb', line 21

def fields
  @fields
end

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/massive_record/orm/schema/field.rb', line 21

def name
  @name
end

#typeObject

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)
  field_options = args.extract_options!
  field_options[:name] = args[0]
  field_options[:type] ||= args[1]

  new(field_options)
end

.type_to_classesObject



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

Returns:

  • (Boolean)


116
117
118
# File 'lib/massive_record/orm/schema/field.rb', line 116

def allow_nil?
  !!allow_nil
end

#column_familyObject



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

#hashObject



79
80
81
# File 'lib/massive_record/orm/schema/field.rb', line 79

def hash
  name.hash
end

#unique_nameObject



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