module Afterpay::Representable
Public Class Methods
included(base)
click to toggle source
# File lib/afterpay/representable.rb, line 7 def self.included(base) base.extend Forwardable base.def_delegators :as_json, :to_json end
Public Instance Methods
as_json()
click to toggle source
# File lib/afterpay/representable.rb, line 12 def as_json instance_values.reduce({}) do |mem, (key, value)| mem.merge!(camelize(key) => represent(value)) end end
Private Instance Methods
camelize(value)
click to toggle source
# File lib/afterpay/representable.rb, line 20 def camelize(value) value.gsub(/_(\w)/) { Regexp.last_match(1).upcase } end
instance_values()
click to toggle source
# File lib/afterpay/representable.rb, line 36 def instance_values Hash[ instance_variables.map do |name| [ name.to_s[1..-1], instance_variable_get(name) ] end ] end
non_primitive?(value)
click to toggle source
# File lib/afterpay/representable.rb, line 32 def non_primitive?(value) value.is_a? Afterpay::Components::Base end
represent(value)
click to toggle source
# File lib/afterpay/representable.rb, line 24 def represent(value) return value.as_json if non_primitive?(value) return value.map { |item| represent(item) } if value.is_a?(Array) value end