Module: HdaArAccessor::InstanceMethods

Defined in:
lib/hda/ar_accessors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mth, *args) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/hda/ar_accessors.rb', line 40

def method_missing mth, *args
  if mth.to_s =~ /^da_(.*)=$/  
    return  @dy_attrs.send("#{$1}=", args.shift)
  elsif  mth.to_s =~ /^da_(.*)$/ 
    return @dy_attrs.send($1)
  end
  super
end

Instance Attribute Details

#dy_attrsObject

Returns the value of attribute dy_attrs.



38
39
40
# File 'lib/hda/ar_accessors.rb', line 38

def dy_attrs
  @dy_attrs
end

Instance Method Details

#dy_attributes=(attrs) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hda/ar_accessors.rb', line 58

def dy_attributes= attrs
  if attrs.blank?
    self.dy_attrs = OpenStruct.new({})
  else
    deleted_attrs = dy_attrs_hash.keys - attrs.map{|hsh| hsh[:da_label]}
    deleted_attrs.each do |attr|
      self.send("da_#{attr}=",nil)
    end 
    attrs.each do |hash|
      dy_attr, value = hash[:da_label], hash[:da_value]
      next if (dy_attr.blank? || value.blank?)
      self.send("da_#{dy_attr}=", value)
    end
  end
end

#dy_attrs_hashObject



74
75
76
# File 'lib/hda/ar_accessors.rb', line 74

def dy_attrs_hash
  @dy_attrs.as_json.values.shift rescue {}
end

#initialize(args = {}) ⇒ Object



49
50
51
# File 'lib/hda/ar_accessors.rb', line 49

def initialize args={}
  super args.merge(:dy_attrs => OpenStruct.new)
end

#map_json_text_to_struct!Object



53
54
55
56
# File 'lib/hda/ar_accessors.rb', line 53

def map_json_text_to_struct!
  self.dy_attrs = OpenStruct.new(JSON.parse(self.send(dynamic_attr) || "{}")) 
  self
end