Sha256: a5fd52dcdd9d5d53d179f3cb122a2506d48ff70699a8a697f7b8813ddb47f758
Contents?: true
Size: 1.95 KB
Versions: 1
Compression:
Stored size: 1.95 KB
Contents
module BraintreeRails module Attributes module ClassMethods def define_attributes(attributes) all_attributes = attributes.values.flatten.uniq attr_accessor(*all_attributes) singleton_class.send(:define_method, :attributes_for) { |action| attributes[action] } singleton_class.send(:define_method, :attributes) { all_attributes } end end module InstanceMethods def attributes self.class.attributes.inject({}) do |hash, attribute| if (value = self.send(attribute)).present? hash[attribute] = value.respond_to?(:attributes_for) ? value.attributes_for(:as_association) : value end hash end end def attributes_for(action) attributes_for_action = attributes.slice(*self.class.attributes_for(action)) attributes_for_action.slice!(*changed) unless action == :as_association attributes_for_action end def assign_attributes(hash) hash.each do |attribute, value| send("#{attribute}=", value) if respond_to?("#{attribute}=") end end def extract_values(obj) self.class.attributes.inject({}) do |hash, attr| hash[attr] = obj.send(attr) if obj.respond_to?(attr) hash end end def changed new_record? ? changed_for_new_record : changed_for_persisted end def changed_for_new_record attributes.map do |attribute, value| attribute if value.present? end.compact end def changed_for_persisted attributes.map do |attribute, value| attribute if value != raw_object.send(attribute) end.compact end end def self.included(receiver) receiver.extend ClassMethods receiver.send :include, InstanceMethods receiver.send :include, ::ActiveModel::Serializers::JSON receiver.send :include, ::ActiveModel::Serializers::Xml end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
braintree-rails-1.1.0 | lib/braintree_rails/attributes.rb |