Sha256: e6045920ae27f34758d1ad29be096856f15108202964527fa38734865ca317f3

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

# encoding: utf-8
require 'set'

module PagarMe
	class PagarMeObject
		def initialize(response = {})
			#init @attributes - which are the attributes in the object
			@attributes = {}

			# Values that were changed in the object but weren't saved
			@unsaved_values = Set.new

			# Methods that already existed
			@@existing_methods = Array.new

			#Update object
			update(response)
		end

		def self.build(attributes)
			self.new(attributes)
		end

		def update(attributes)
			attributes.each do |key, value|
				key = key.to_s

				@attributes[key] = Util.convert_to_pagarme_object(value)
				@unsaved_values.delete(key)
			end
		end

		def each(&block)
			@attributes.each(&block)
		end

		def []=(key,value)
			@attributes[key] = value
		end

		def [](key)
			@attributes[key.to_sym]
		end

		def to_hash_value(value, type)
			case value
			when PagarMeObject
				value.send type
			when Array
				value.map do |v|
					to_hash_value v, type
				end
			else
				value
			end
		end

		def unsaved_values
			Hash[@unsaved_values.map { |k| [k, to_hash_value(@attributes[k], 'unsaved_values')] }]
		end

		def to_hash
			Hash[@attributes.map { |k, v| [k, to_hash_value(v, 'to_hash')] }]
		end

		protected

		def method_missing(name, *args)
			name = name.to_s

			if name.end_with?('=')
				attribute_name = name[0...-1]

				@attributes[attribute_name] = args[0]
				@unsaved_values.add(attribute_name)
			else
				@attributes[name] || nil
			end
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pagarme-2.0.1 lib/pagarme/object.rb