module Omise::Attributes

Public Class Methods

new(attributes = {}, options = {}) click to toggle source
# File lib/omise/attributes.rb, line 5
def initialize(attributes = {}, options = {})
  @attributes          = attributes
  @options             = options
  @expanded_attributes = {}
end

Public Instance Methods

[](key) click to toggle source
# File lib/omise/attributes.rb, line 34
def [](key)
  value = @attributes[key.to_s]
  if value.is_a?(Hash)
    Omise::Util.typecast(value)
  else
    value
  end
end
as_json(*) click to toggle source
# File lib/omise/attributes.rb, line 30
def as_json(*)
  @attributes
end
assign_attributes(attributes = {}) { || ... } click to toggle source
# File lib/omise/attributes.rb, line 15
def assign_attributes(attributes = {})
  cleanup!
  @attributes = attributes
  yield if block_given?
  self
end
attributes() click to toggle source
# File lib/omise/attributes.rb, line 11
def attributes
  @attributes
end
destroyed?() click to toggle source
# File lib/omise/attributes.rb, line 26
def destroyed?
  @attributes["deleted"]
end
key?(key) click to toggle source
# File lib/omise/attributes.rb, line 43
def key?(key)
  @attributes.key?(key.to_s)
end
location(id = nil) click to toggle source
# File lib/omise/attributes.rb, line 22
def location(id = nil)
  [@attributes["location"], id].compact.join("/")
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/omise/attributes.rb, line 69
def method_missing(method_name, *args, &block)
  if predicate?(method_name)
    !!self[method_name.to_s.chomp("?")]
  elsif key?(method_name)
    self[method_name]
  else
    super
  end
end
predicate?(method_name) click to toggle source
# File lib/omise/attributes.rb, line 47
def predicate?(method_name)
  method_name   = method_name.to_s
  question_mark = method_name.chars.last == "?"
  key           = method_name.chomp("?")

  if question_mark && key?(key)
    true
  else
    false
  end
end
respond_to_missing?(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/omise/attributes.rb, line 59
def respond_to_missing?(method_name, *args, &block)
  if predicate?(method_name)
    true
  elsif key?(method_name)
    true
  else
    super
  end
end

Private Instance Methods

cleanup!() click to toggle source
# File lib/omise/attributes.rb, line 105
def cleanup!
  @expanded_attributes = {}
end
expand_attribute(object, key, options = {}) click to toggle source
# File lib/omise/attributes.rb, line 97
def expand_attribute(object, key, options = {})
  if @attributes[key] && @attributes[key].is_a?(String)
    @expanded_attributes[key] ||= object.retrieve(@attributes[key], options)
  else
    self[key]
  end
end
list_attribute(klass, key) click to toggle source
# File lib/omise/attributes.rb, line 85
def list_attribute(klass, key)
  klass.new(@attributes[key], parent: self)
end
list_nested_resource(klass, key, options = {}) click to toggle source
# File lib/omise/attributes.rb, line 89
def list_nested_resource(klass, key, options = {})
  if @attributes.key?(key) && options.empty?
    return list_attribute(klass, key)
  end

  klass.new(nested_resource(key, options).get, parent: self)
end
lookup_attribute_value(*keys) click to toggle source
# File lib/omise/attributes.rb, line 81
def lookup_attribute_value(*keys)
  keys.each { |key| return self[key] if key?(key) }
end