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 }
}
TYPES =
TYPES_DEFAULTS_TO.keys

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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)
  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



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

#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



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

  new(field_options)
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

Returns:

  • (Boolean)


105
106
107
# File 'lib/massive_record/orm/schema/field.rb', line 105

def allow_nil?
  !!allow_nil
end

#column_familyObject



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
# File 'lib/massive_record/orm/schema/field.rb', line 111

def decode(value)
  return value if value.nil? || value_is_already_decoded?(value)
  
  value = case type
          when :boolean
            value.blank? ? 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, :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



137
138
139
140
141
142
143
# File 'lib/massive_record/orm/schema/field.rb', line 137

def encode(value)
  if type == :string && !(value.nil? || value == @@encoded_nil_value)
    value
  else
    coder.dump(value)
  end
end

#hashObject



68
69
70
# File 'lib/massive_record/orm/schema/field.rb', line 68

def hash
  name.hash
end

#unique_nameObject



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