Module: Attribution

Defined in:
lib/attribution.rb,
lib/attribution/id.rb,
lib/attribution/util.rb,
lib/attribution/model.rb,
lib/attribution/version.rb,
lib/attribution/timestamps.rb,
lib/attribution/validations.rb

Defined Under Namespace

Modules: ClassMethods, ID, Model, Timestamps, Util, Validations

Constant Summary collapse

BOOLEAN_TRUE_STRINGS =
['y','yes','t','true']
VERSION =
"0.8.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



8
9
10
# File 'lib/attribution.rb', line 8

def self.included(cls)
  cls.extend(ClassMethods)
end

Instance Method Details

#attributes(*associations) ⇒ Hash Also known as: to_h

Returns the attributes of this instance and their values.

Returns:

  • (Hash)

    the attributes of this instance and their values



20
21
22
23
24
25
# File 'lib/attribution.rb', line 20

def attributes(*associations)
  self.class.attribute_names.inject({}) do |attrs, attr|
    attrs[attr] = send(attr)
    attrs
  end
end

#attributes=(attributes) ⇒ Object

Parameters:

  • attributes (String, Hash)

    The attributes and their values



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/attribution.rb', line 29

def attributes=(attributes)
  attributes = case attributes
  when String then JSON.parse(attributes)
  when Hash then attributes
  else {}
  end.with_indifferent_access

  attributes.each do |k,v|
    setter = "#{k}="
    if respond_to?(setter)
      send(setter, v)
    end
  end
end

#initialize(attributes = {}) ⇒ Object



12
13
14
15
16
17
# File 'lib/attribution.rb', line 12

def initialize(attributes={})
  self.class.attribute_names.each do |attr|
    instance_variable_set("@#{attr}", nil)
  end
  self.attributes = attributes
end