class Skroutz::Resource

Attributes

attributes[RW]
client[RW]
to_hash[RW]

Public Class Methods

new(attributes, client) click to toggle source
# File lib/skroutz/resource.rb, line 8
def initialize(attributes, client)
  @attributes = attributes
  @client = client
end

Public Instance Methods

inspect() click to toggle source
# 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
resource() click to toggle source
# File lib/skroutz/resource.rb, line 13
def resource
  @resource ||= self.class.to_s.demodulize.tableize.singularize
end
resource_prefix() click to toggle source
# File lib/skroutz/resource.rb, line 17
def resource_prefix
  @resource_prefix ||= resource.pluralize
end

Protected Instance Methods

method_missing(method_symbol, *arguments) click to toggle source
Calls superclass method
# 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
respond_to_missing?(method, include_priv = false) click to toggle source
Calls superclass method
# File lib/skroutz/resource.rb, line 33
def respond_to_missing?(method, include_priv = false)
  method_name = method.to_s
  if attributes.nil?
    super
  elsif attributes.include?(method_name.sub(/[=\?]\Z/, ''))
    true
  else
    super
  end
end

Private Instance Methods

attribute_for_inspect(value) click to toggle source

Taken from ActiveRecord::AttributeMethods#attribute_for_inspect

# File lib/skroutz/resource.rb, line 64
def attribute_for_inspect(value) # rubocop: disable all
  if value.is_a?(String) && value.length > 50
    "#{value[0, 50]}...".inspect
  elsif value.is_a?(Date) || value.is_a?(Time)
    %("#{value.to_s(:db)}")
  elsif value.is_a?(Array) && value.size > 10
    inspected = value.first(10).inspect
    %(#{inspected[0...-1]}, ...])
  else
    value.inspect
  end
end