Module: AttributeHashAccessors

Defined in:
lib/hashish/attribute_hash_accessors.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



33
34
35
36
37
38
# File 'lib/hashish/attribute_hash_accessors.rb', line 33

def self.included(base)
  base.send(:include, ActiveModel::AttributeMethods)
  base.send(:include, ActiveModel::Dirty)

  base.extend ClassMethods
end

Instance Method Details

#attributesObject



56
57
58
# File 'lib/hashish/attribute_hash_accessors.rb', line 56

def attributes
  ActiveSupport::HashWithIndifferentAccess.new
end

#attributes=(attrs = {}) ⇒ Object

TODO: Raise error if an attrubute passed in does not exist in the list of acceptable attributes



49
50
51
52
53
54
# File 'lib/hashish/attribute_hash_accessors.rb', line 49

def attributes=(attrs = {})
  attrs           = attrs.with_indifferent_access
  safe_attributes = attrs.slice *attributes.keys

  @attributes.merge! safe_attributes
end

#initialize(attrs = {}) ⇒ Object



40
41
42
43
# File 'lib/hashish/attribute_hash_accessors.rb', line 40

def initialize(attrs = {})
  @attributes     = ActiveSupport::HashWithIndifferentAccess.new
  self.attributes = attrs
end