Class: Skroutz::Resource

Inherits:
Object
  • Object
show all
Includes:
Associations
Defined in:
lib/skroutz/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UrlHelpers

#base_path

Constructor Details

#initialize(attributes, client) ⇒ Resource

Returns a new instance of Resource.



8
9
10
11
# File 'lib/skroutz/resource.rb', line 8

def initialize(attributes, client)
  @attributes = attributes
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *arguments) ⇒ Object (protected)

rubocop: disable all



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/skroutz/resource.rb', line 44

def method_missing(method_symbol, *arguments) # rubocop: disable all
  method_name = method_symbol.to_s

  if method_name =~ /(=|\?)$/
    case $1
    when '='
      attributes[$`] = arguments.first
    when '?'
      !!attributes[$`]
    end
  elsif attributes.include?(method_name)
    return attributes[method_name]
  else
    super
  end
end

Instance Attribute Details

#attributesObject Also known as: to_hash

Returns the value of attribute attributes.



4
5
6
# File 'lib/skroutz/resource.rb', line 4

def attributes
  @attributes
end

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/skroutz/resource.rb', line 4

def client
  @client
end

Instance Method Details

#inspectObject



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

def inspect
  if attributes.present?
    inspection = attributes.map { |k, v| "#{k}: #{attribute_for_inspect(v)}" }.join(', ')
  else
    inspection = 'not initialized'
  end

  "#<#{self.class} #{inspection}>"
end

#resourceObject



13
14
15
# File 'lib/skroutz/resource.rb', line 13

def resource
  @resource ||= self.class.to_s.demodulize.tableize.singularize
end

#resource_prefixObject



17
18
19
# File 'lib/skroutz/resource.rb', line 17

def resource_prefix
  @resource_prefix ||= resource.pluralize
end