class GMO::PG::Payload

Public Class Methods

decode(body) click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 10
def self.decode(body)
  Hash[URI.decode_www_form(body)]
end
encode(payload) click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 6
def self.encode(payload)
  URI.encode_www_form(payload)
end
new(attributes = {}) click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 14
def initialize(attributes = {})
  @attributes = {}
  attributes.each do |name, value|
    self[name] = value
  end
end

Private Class Methods

bind_attribute(param_name, attr_name, typecast: nil) click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 60
def self.bind_attribute(param_name, attr_name, typecast: nil)
  @bind_attributes << [param_name, attr_name, { typecast: typecast }]

  define_method(:"#{attr_name}")  { self[attr_name] }
  define_method(:"#{attr_name}=") { |value| self[attr_name] = value }
end
detect_attribute_name(name) click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 75
def self.detect_attribute_name(name)
  detect_bind_attribute(name)[1]
end
detect_bind_attribute(name) click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 67
def self.detect_bind_attribute(name)
  name = name.respond_to?(:to_sym) ? name.to_sym : name
  @bind_attributes.each do |(param_name, attr_name, options)|
    return [param_name, attr_name, options] if name == param_name || name == attr_name
  end
  [name, name, {}]
end
detect_param_name(name) click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 79
def self.detect_param_name(name)
  detect_bind_attribute(name)[0]
end
inherited(inherited) click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 54
def self.inherited(inherited)
  inherited.class_eval do
    @bind_attributes = []
  end
end

Public Instance Methods

[](name) click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 26
def [](name)
  param_name = self.class.detect_param_name(name)
  return unless @attributes.key?(param_name)
  @attributes[param_name].to_attribute
end
[]=(name, value) click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 21
def []=(name, value)
  param_name, _, options = self.class.detect_bind_attribute(name)
  @attributes[param_name] = Typecast.detect(options[:typecast]).new(value)
end
inspect() click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 44
def inspect
  '#<%s:%014x "%s">' % [
    self.class.name,
    object_id << 1, # @see http://stackoverflow.com/questions/2818602/in-ruby-why-does-inspect-print-out-some-kind-of-object-id-which-is-different
    self.class.encode(payload),
  ]
end
payload() click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 32
def payload
  @attributes.each_with_object({}) do |(param_name, value), payload|
    payload[param_name] = value.to_payload
  end
end
to_hash() click to toggle source
# File lib/gmo-pg/http_resource/payload.rb, line 38
def to_hash
  @attributes.each_with_object({}) do |(param_name, value), hash|
    hash[self.class.detect_attribute_name(param_name)] = value.to_attribute
  end
end