Sha256: e84c803ffee80561d0a01f965400834ca7d05de159d66ff384d39b57590f07b4
Contents?: true
Size: 1.62 KB
Versions: 16
Compression:
Stored size: 1.62 KB
Contents
require 'set' require_relative './actions' module CurrencyCloud module Resource def self.included(base) base.extend(ClassMethods) end attr_reader :changed_attributes def initialize(attributes) @attributes = attributes @changed_attributes = Set.new self.accessors = valid_attributes end def inspect "#<#{self.class}:0x#{object_id.to_s(16)} #{@attributes.inspect}>" end private attr_reader :attributes def resource self.class.resource end def attributes=(new_values) @attributes = new_values.select { |k, _| valid_attributes.include?(k) } end def valid_attributes @attributes.keys end def changed? !@changed_attributes.empty? end def client self.class.client end def metaclass class << self; self; end end def accessors=(attributes) metaclass.instance_eval do attributes.each do |attribute| define_method(attribute) { @attributes[attribute] } define_method("#{attribute}=".to_sym) do |value| @attributes[attribute] = value @changed_attributes << attribute end end end end module ClassMethods def resource(resource = nil) @resource ||= resource end def actions(*actions) actions.each do |action| class_eval do action_module = CurrencyCloud::Actions.const_get(action.to_s.capitalize) extend(action_module) end end end def client @client ||= Client.new(resource) end end end end
Version data entries
16 entries across 16 versions & 1 rubygems