Class: DynamicAttribute

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/datts_right/dynamic_attribute.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attr_column(v) ⇒ Object

Returns what column this kind of data should be in



42
43
44
# File 'lib/datts_right/dynamic_attribute.rb', line 42

def self.attr_column(v)
  "#{self.attr_type?(v)}_value"
end

.attr_type?(v) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/datts_right/dynamic_attribute.rb', line 29

def self.attr_type?(v)
  object_type = if v.is_a?(Integer)
    "integer"
  elsif v.is_a?(Float)
    "float"
  elsif v.is_a?(TrueClass) || v.is_a?(FalseClass)
    "boolean"
  elsif v.is_a?(String)
    v.size < 255 ? "string" : "text"
  end
end

Instance Method Details

#definerObject

Returns the hash that defined this



8
9
10
11
12
# File 'lib/datts_right/dynamic_attribute.rb', line 8

def definer
  defining_record = attributable.defining_record
  return {} if defining_record.nil?
  defining_record.definition[attr_key.to_sym]
end

#valueObject



14
15
16
# File 'lib/datts_right/dynamic_attribute.rb', line 14

def value
  send("#{object_type}_value")
end

#value=(v) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/datts_right/dynamic_attribute.rb', line 18

def value=(v)
  object_type = self.class.attr_type?(v)
  if v.nil?
    send "#{self.object_type}_value=", v
  elsif object_type == "string" && self.object_type == "text"
    self.text_value = v
  else
    send "#{self.class.attr_column(v)}=", v
  end
end