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 =
[:string, :integer, :float, :boolean, :array, :hash, :date, :time]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#columnObject

Returns the value of attribute column.



10
11
12
# File 'lib/massive_record/orm/schema/field.rb', line 10

def column
  @column
end

#defaultObject



64
65
66
# File 'lib/massive_record/orm/schema/field.rb', line 64

def default
  @default.duplicable? ? @default.dup : @default
end

#fieldsObject

Returns the value of attribute fields.



10
11
12
# File 'lib/massive_record/orm/schema/field.rb', line 10

def fields
  @fields
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/massive_record/orm/schema/field.rb', line 10

def name
  @name
end

#typeObject

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



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_familyObject



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

#hashObject



51
52
53
# File 'lib/massive_record/orm/schema/field.rb', line 51

def hash
  name.hash
end

#unique_nameObject



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