lib/pagarme/object.rb in pagarme-2.0.0 vs lib/pagarme/object.rb in pagarme-2.0.1

- old
+ new

@@ -20,24 +20,13 @@ def self.build(attributes) self.new(attributes) end def update(attributes) - removed = Set.new(@attributes.keys - attributes.keys) - added = Set.new(attributes.keys - @attributes.keys) - - instance_eval do - remove_attribute(removed) - add_attribute(added) - end - - removed.each do |key| - @attributes.delete(key) - @unsaved_values.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 @@ -74,60 +63,20 @@ Hash[@attributes.map { |k, v| [k, to_hash_value(v, 'to_hash')] }] end protected - def metaclass - class << self; self; end - end - - def remove_attribute(keys) - metaclass.instance_eval do - keys.each do |key| - key_sym = :"#{key}=" - - if !@@existing_methods.include?(key) - remove_method(key) if method_defined?(key) - end - - remove_method(key_sym) if method_defined?(key_sym) - end - end - end - - def add_attribute(keys) - metaclass.instance_eval do - keys.each do |key| - key_set = "#{key}=" - - if method_defined?(key) - @@existing_methods.push(key) - else - define_method(key) { @attributes[key] } - end - - define_method(key_set) do |value| - @attributes[key] = value - @unsaved_values.add(key) - end - end - end - end - def method_missing(name, *args) - if name.to_s.end_with?('=') - attr = name.to_s[0...-1].to_sym - add_attribute([attr]) + name = name.to_s - begin - m = method(name) - rescue NameError - raise NoMethodError.new("O atributo #{name} nao e permitido.") - end + if name.end_with?('=') + attribute_name = name[0...-1] - m.call *args + @attributes[attribute_name] = args[0] + @unsaved_values.add(attribute_name) else - @attributes[name] if @attributes.has_key?(name) + @attributes[name] || nil end end end end +