Module: Cistern::Attributes::InstanceMethods

Included in:
Collection, Model, Singular
Defined in:
lib/cistern/attributes.rb

Instance Method Summary collapse

Instance Method Details

#_dump(level) ⇒ Object



112
113
114
# File 'lib/cistern/attributes.rb', line 112

def _dump(level)
  Marshal.dump(attributes)
end

#attributesObject



148
149
150
# File 'lib/cistern/attributes.rb', line 148

def attributes
  @attributes ||= {}
end

#attributes=(attributes) ⇒ Object



152
153
154
# File 'lib/cistern/attributes.rb', line 152

def attributes=(attributes)
  @attributes = attributes
end

#changedObject



223
224
225
# File 'lib/cistern/attributes.rb', line 223

def changed
  @changes ||= {}
end

#dirty?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/cistern/attributes.rb', line 215

def dirty?
  changed.any?
end

#dirty_attributesObject



219
220
221
# File 'lib/cistern/attributes.rb', line 219

def dirty_attributes
  changed.inject({}) { |r,(k,(_,v))| r.merge(k => v) }
end

#dupObject



156
157
158
159
160
# File 'lib/cistern/attributes.rb', line 156

def dup
  copy = super
  copy.attributes= copy.attributes.dup
  copy
end

#identityObject



162
163
164
# File 'lib/cistern/attributes.rb', line 162

def identity
  send(self.class.instance_variable_get('@identity'))
end

#identity=(new_identity) ⇒ Object



166
167
168
# File 'lib/cistern/attributes.rb', line 166

def identity=(new_identity)
  send("#{self.class.instance_variable_get('@identity')}=", new_identity)
end

#merge_attributes(new_attributes = {}) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/cistern/attributes.rb', line 170

def merge_attributes(new_attributes = {})
  new_attributes.each do |key, value|
    # find nested paths
    value.is_a?(::Hash) && self.class.attributes.each do |name, options|
      if (options[:squash] || []).first == key
        send("#{name}=", {key => value})
      end
    end
    unless self.class.ignored_attributes.include?(key)
      if self.class.aliases.has_key?(key)
        self.class.aliases[key].each do |aliased_key|
          send("#{aliased_key}=", value)
        end
      end

      if self.respond_to?("#{key}=", true)
        send("#{key}=", value)
      end
    end
  end
  changed.clear
  self
end

#new_record?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/cistern/attributes.rb', line 194

def new_record?
  !identity
end

#read_attribute(name) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/cistern/attributes.rb', line 116

def read_attribute(name)
  options = self.class.attributes[name] || {}
  # record the attribute was accessed
  self.class.attributes[name.to_s.to_sym][:coverage_hits] += 1 rescue  nil

  attributes.fetch(name.to_s.to_sym, options[:default])
end

#requires(*args) ⇒ Object

check that the attributes specified in args exist and is not nil



199
200
201
202
203
204
205
206
# File 'lib/cistern/attributes.rb', line 199

def requires(*args)
  missing = missing_attributes(args)
  if missing.length == 1
    raise(ArgumentError, "#{missing.first} is required for this operation")
  elsif missing.any?
    raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation")
  end
end

#requires_one(*args) ⇒ Object



208
209
210
211
212
213
# File 'lib/cistern/attributes.rb', line 208

def requires_one(*args)
  missing = missing_attributes(args)
  if missing.length == args.length
    raise(ArgumentError, "#{missing[0...-1].join(", ")} or #{missing[-1]} are required for this operation")
  end
end

#write_attribute(name, value) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cistern/attributes.rb', line 124

def write_attribute(name, value)
  options = self.class.attributes[name] || {}

  transform = Cistern::Attributes.transforms[options[:squash] ? :squash : :none] ||
    Cistern::Attributes.default_transform

  parser = Cistern::Attributes.parsers[options[:type]] ||
    options[:parser] ||
    Cistern::Attributes.default_parser

  transformed = transform.call(name, value, options)

  new_value = parser.call(transformed, options)
  attribute = name.to_s.to_sym

  previous_value = attributes[attribute]

  attributes[attribute] = new_value

  changed!(attribute, previous_value, new_value)

  new_value
end