Module: N4j::Attributes

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods, ActiveModel::Dirty
Defined in:
lib/n4j/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attribute(key) ⇒ Object



22
23
24
# File 'lib/n4j/attributes.rb', line 22

def attribute(key)
  instance_variable_get("@#{key}")
end

#attribute=(key, value) ⇒ Object



26
27
28
29
30
# File 'lib/n4j/attributes.rb', line 26

def attribute=(key,value)
  # puts "Setting attribute #{key}!"
  send("#{key}_will_change!") unless value == send(key)
  instance_variable_set("@#{key}", value)
end

#attributesObject



32
33
34
35
36
37
# File 'lib/n4j/attributes.rb', line 32

def attributes
  self.class.attributes.inject({}) do |hsh, attr|
    hsh[attr] = send(attr)
    hsh
  end
end

#load_attributes(new_attributes) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/n4j/attributes.rb', line 39

def load_attributes(new_attributes)
  self.place_in_batch = nil
  self.class.attributes.each do |key|
    unless new_attributes[key].blank?
      value = new_attributes[key].dup
      instance_variable_set("@#{key}", value)
    end
  end
end