Module: Cistern::Attributes::InstanceMethods

Included in:
Model
Defined in:
lib/cistern/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attributesObject



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

def attributes
  @attributes ||= {}
end

#attributes=(attributes) ⇒ Object



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

def attributes=(attributes)
  @attributes = attributes
end

#changedObject



264
265
266
# File 'lib/cistern/attributes.rb', line 264

def changed
  @changes ||= {}
end

#dirty?Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/cistern/attributes.rb', line 256

def dirty?
  changed.any?
end

#dirty_attributesObject



260
261
262
# File 'lib/cistern/attributes.rb', line 260

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

#dumpObject



118
119
120
# File 'lib/cistern/attributes.rb', line 118

def dump
  Marshal.dump(attributes)
end

#dupObject



169
170
171
172
173
# File 'lib/cistern/attributes.rb', line 169

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

#identityObject



175
176
177
178
179
# File 'lib/cistern/attributes.rb', line 175

def identity
  key = self.class.instance_variable_get('@identity')

  public_send(key) if key
end

#identity=(new_identity) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/cistern/attributes.rb', line 181

def identity=(new_identity)
  key = self.class.instance_variable_get('@identity')

  if key
    public_send("#{key}=", new_identity)
  else
    fail ArgumentError, 'Identity not specified'
  end
end

#merge_attributes(new_attributes = {}) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/cistern/attributes.rb', line 191

def merge_attributes(new_attributes = {})
  protected_methods  = (Cistern::Model.instance_methods - [:service, :identity, :collection])
  ignored_attributes = self.class.ignored_attributes
  class_attributes   = self.class.attributes
  class_aliases      = self.class.aliases

  new_attributes.each do |_key, value|
    string_key = _key.is_a?(String) ? _key : _key.to_s
    symbol_key = case _key
                 when String
                   _key.to_sym
                 when Symbol
                   _key
                 else
                   string_key.to_sym
          end

    # find nested paths
    value.is_a?(::Hash) && class_attributes.each do |name, options|
      if options[:squash] && options[:squash].first == string_key
        send("#{name}=", symbol_key => value)
      end
    end

    next if ignored_attributes.include?(symbol_key)

    if class_aliases.key?(symbol_key)
      class_aliases[symbol_key].each do |aliased_key|
        send("#{aliased_key}=", value)
      end
    end

    assignment_method = "#{string_key}="

    if !protected_methods.include?(symbol_key) && self.respond_to?(assignment_method, true)
      send(assignment_method, value)
    end
  end

  changed.clear

  self
end

#new_record?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/cistern/attributes.rb', line 235

def new_record?
  identity.nil?
end

#read_attribute(name) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cistern/attributes.rb', line 122

def read_attribute(name)
  key = name.to_s.to_sym
  options = self.class.attributes[key]
  default = options[:default]

  # record the attribute was accessed
  if defined?(Cistern::Coverage) && options[:coverage_hits]
    options[:coverage_hits] += 1
  end

  default = Marshal.load(Marshal.dump(default)) unless default.nil?

  attributes.fetch(key, default)
end

#requires(*args) ⇒ Object

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



240
241
242
243
244
245
246
247
# File 'lib/cistern/attributes.rb', line 240

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

#requires_one(*args) ⇒ Object



249
250
251
252
253
254
# File 'lib/cistern/attributes.rb', line 249

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

#write_attribute(name, value) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cistern/attributes.rb', line 137

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