Class: BigcommerceOAuthAPI::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/bigcommerce-oauth-api/resource.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Resource

Returns a new instance of Resource.



5
6
7
# File 'lib/bigcommerce-oauth-api/resource.rb', line 5

def initialize(attributes)
  @attributes = HashWithIndifferentAccess.new(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/bigcommerce-oauth-api/resource.rb', line 33

def method_missing(method_sym, *arguments)
  if @attributes.include?(method_sym)
    attribute_name = method_sym.to_s.gsub(/(\?$)|(=$)/, '')
    self.instance_eval build_attribute_getter(attribute_name)
    send(method_sym, *arguments)
  else
    super(method_sym, *arguments)
  end
end

Instance Method Details

#==(other) ⇒ Object



17
18
19
# File 'lib/bigcommerce-oauth-api/resource.rb', line 17

def ==(other)
  self.to_h == other.to_h
end

#[](key) ⇒ Object



9
10
11
# File 'lib/bigcommerce-oauth-api/resource.rb', line 9

def [](key)
  @attributes[key.to_sym]
end

#[]=(key, value) ⇒ Object



13
14
15
# File 'lib/bigcommerce-oauth-api/resource.rb', line 13

def []=(key, value)
  @attributes[key.to_sym] = value
end

#build_attribute_getter(attribute_name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bigcommerce-oauth-api/resource.rb', line 43

def build_attribute_getter(attribute_name)
  "def #{attribute_name}
    if @attributes[:#{attribute_name}] && @attributes[:#{attribute_name}].is_a?(Hash)
      memoize(:#{attribute_name}) do
        self.class.new(@attributes[:#{attribute_name}])
      end
    else
      @attributes[:#{attribute_name}]
    end
  end"
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/bigcommerce-oauth-api/resource.rb', line 21

def eql?(other)
  self == other
end

#marshal_dumpObject



55
56
57
# File 'lib/bigcommerce-oauth-api/resource.rb', line 55

def marshal_dump
  {}.merge(@attributes.to_h)
end

#marshal_load(attributes) ⇒ Object



59
60
61
# File 'lib/bigcommerce-oauth-api/resource.rb', line 59

def marshal_load(attributes)
  send :initialize, attributes
end

#respond_to?(method_name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/bigcommerce-oauth-api/resource.rb', line 25

def respond_to?(method_name, include_all = false)
  super(method_name, include_all) ? true : @attributes.include?(method_name.to_s.gsub(/(\?$)|(=$)/, '').to_sym)
end

#to_hObject



29
30
31
# File 'lib/bigcommerce-oauth-api/resource.rb', line 29

def to_h
  @attributes.to_h
end