Class: TophatterMerchant::Resource

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/tophatter_merchant/resource.rb

Direct Known Subclasses

Metadata, Order, Product, Variation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Resource

Returns a new instance of Resource.



12
13
14
# File 'lib/tophatter_merchant/resource.rb', line 12

def initialize(hash)
  self.attributes = hash
end

Class Method Details

.attr_accessor(*vars) ⇒ Object



40
41
42
43
44
# File 'lib/tophatter_merchant/resource.rb', line 40

def attr_accessor(*vars)
  @attributes ||= {}
  vars.each { |var| @attributes[var.to_s] = true }
  super(*vars)
end

.attributesObject



46
47
48
49
# File 'lib/tophatter_merchant/resource.rb', line 46

def attributes
  @attributes ||= {}
  @attributes.keys
end

Instance Method Details

#attributesObject



16
17
18
# File 'lib/tophatter_merchant/resource.rb', line 16

def attributes
  self.class.attributes
end

#attributes=(hash) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/tophatter_merchant/resource.rb', line 20

def attributes=(hash)
  # Only honor valid attributes.
  hash.each do |key, value|
    if respond_to?("#{key}=")
      send("#{key}=", value)
    else
      TophatterMerchant.logger.warn "Invalid attribute #{key} specified for #{self.class.name}"
    end
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/tophatter_merchant/resource.rb', line 31

def persisted?
  id.present? # @TODO: Should this be internal_id?
end

#to_hObject



35
36
37
# File 'lib/tophatter_merchant/resource.rb', line 35

def to_h
  self.class.attributes.map { |key| [key, send(key)] }.to_h
end