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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bigcommerce-oauth-api/resource.rb', line 35

def method_missing(method_sym, *arguments)
  method_type = method_sym.to_s[-1] # last char
  if @attributes.include?(method_sym) || method_type == '='
    attribute_name = method_sym.to_s.gsub(/(\?$)|(=$)/, '')
    case method_type
      when '='
        self.instance_eval build_attribute_setter(attribute_name)
      else
        self.instance_eval build_attribute_getter(attribute_name)
    end
    send(method_sym, *arguments)
  else
    super(method_sym, *arguments)
  end
end

Instance Method Details

#==(other) ⇒ Object



19
20
21
# File 'lib/bigcommerce-oauth-api/resource.rb', line 19

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

#[](key) ⇒ Object



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

def [](key)
  value = memoize(key.to_sym) # get memoized value if present
  value || @attributes[key.to_sym]
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  @attributes[key.to_sym] = value
  unmemoize(key.to_sym) # protect against memoized data
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/bigcommerce-oauth-api/resource.rb', line 23

def eql?(other)
  self == other
end

#marshal_dumpObject



51
52
53
# File 'lib/bigcommerce-oauth-api/resource.rb', line 51

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

#marshal_load(attributes) ⇒ Object



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

def marshal_load(attributes)
  send :initialize, attributes
end

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

Returns:

  • (Boolean)


27
28
29
# File 'lib/bigcommerce-oauth-api/resource.rb', line 27

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



31
32
33
# File 'lib/bigcommerce-oauth-api/resource.rb', line 31

def to_h
  @attributes.to_h
end