Sha256: da1c6ef569fe26a6687d2492a7bdefa98c9aac71a9e83cf86aa02598c1053d15

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 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)
			removed_attributes = @attributes.keys - attributes.keys

			removed_attributes.each do |key|
				@attributes.delete key
			end

			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
			@unsaved_values.add key
		end

		def [](key)
			@attributes[key]
		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

		def respond_to?(name, include_all = false)
			return true if name.to_s.end_with? '='

			@attributes.has_key?(name.to_s) || super
		end

		protected

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

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

				self[attribute_name] = args[0]
			else
				self[name]
			end
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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