Module: Tainbox::InstanceMethods

Defined in:
lib/tainbox/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#as_json(*args) ⇒ Object



48
49
50
# File 'lib/tainbox/instance_methods.rb', line 48

def as_json(*args)
  attributes.as_json(*args)
end

#attribute_provided?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/tainbox/instance_methods.rb', line 44

def attribute_provided?(attribute)
  tainbox_provided_attributes.include?(attribute.to_sym)
end

#attributesObject



17
18
19
20
21
# File 'lib/tainbox/instance_methods.rb', line 17

def attributes
  self.class.tainbox_attributes.map do |attribute|
    [attribute, send(attribute)] if respond_to?(attribute, true)
  end.compact.to_h
end

#attributes=(attributes) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tainbox/instance_methods.rb', line 23

def attributes=(attributes)
  if attributes.respond_to?(:to_h)
    attributes = attributes.to_h
  else
    raise ArgumentError, 'Attributes can only be assigned via objects which respond to #to_h'
  end

  attributes = attributes.symbolize_keys
  self.class.tainbox_attributes.each do |attribute|

    if attributes.has_key?(attribute)
      method_name = "#{attribute}="
      send(method_name, attributes[attribute]) if respond_to?(method_name, true)

    else
      method_name = "tainbox_set_default_#{attribute}"
      send(method_name) if respond_to?(method_name, true)
    end
  end
end

#initialize(*args) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/tainbox/instance_methods.rb', line 8

def initialize(*args)
  if self.class.tainbox_initializer_suppressed?
    super
  else
    attributes = (args.length >= 1) ? args.first : {}
    self.attributes = attributes
  end
end