Module: ArDocStore::EmbeddableModel::InstanceMethods

Defined in:
lib/ar_doc_store/embeddable_model.rb

Instance Method Summary collapse

Instance Method Details

#attributes=(attrs = HashWithIndifferentAccess.new) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ar_doc_store/embeddable_model.rb', line 52

def attributes=(attrs=HashWithIndifferentAccess.new)
  virtual_attributes.keys.each do |attr|
    @attributes[attr] ||= nil
  end
  if attrs
    attrs.each { |key, value|
      key = "#{key}=".to_sym
      self.public_send(key, value) if methods.include?(key)
    }
  end
  self
end

#id_will_change!Object



97
98
# File 'lib/ar_doc_store/embeddable_model.rb', line 97

def id_will_change!
end

#initialize(attrs = HashWithIndifferentAccess.new) ⇒ Object



35
36
37
38
# File 'lib/ar_doc_store/embeddable_model.rb', line 35

def initialize(attrs=HashWithIndifferentAccess.new)
  @_initialized = true
  initialize_attributes attrs
end

#initialize_attributes(attrs) ⇒ Object



46
47
48
49
50
# File 'lib/ar_doc_store/embeddable_model.rb', line 46

def initialize_attributes(attrs)
  @attributes ||= HashWithIndifferentAccess.new
  self.parent = attributes.delete(:parent) if attributes
  self.attributes = attrs
end

#inspectObject



69
70
71
# File 'lib/ar_doc_store/embeddable_model.rb', line 69

def inspect
  "#{self.class}: #{attributes.inspect}"
end

#instantiate(attrs = HashWithIndifferentAccess.new) ⇒ Object



40
41
42
43
44
# File 'lib/ar_doc_store/embeddable_model.rb', line 40

def instantiate(attrs=HashWithIndifferentAccess.new)
  initialize_attributes attrs
  @_initialized = true
  self
end

#persisted?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/ar_doc_store/embeddable_model.rb', line 65

def persisted?
  false
end

#read_store_attribute(store, attr) ⇒ Object



73
74
75
# File 'lib/ar_doc_store/embeddable_model.rb', line 73

def read_store_attribute(store, attr)
  attributes[attr]
end

#to_paramObject



93
94
95
# File 'lib/ar_doc_store/embeddable_model.rb', line 93

def to_param
  id
end

#write_default_store_attribute(attr, value) ⇒ Object



89
90
91
# File 'lib/ar_doc_store/embeddable_model.rb', line 89

def write_default_store_attribute(attr, value)
  attributes[attr] = value
end

#write_store_attribute(store, attribute, value) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ar_doc_store/embeddable_model.rb', line 77

def write_store_attribute(store, attribute, value)
  if @_initialized
    old_value = attributes[attribute]
    if attribute.to_s != 'id' && value != old_value
      public_send :"#{attribute}_will_change!"
      parent.data_will_change! if parent
    end

  end
  attributes[attribute] = value
end