Sha256: 9d7577b87fdfb44d8754283005ebf8757c8cac44d8f37ab8b1bd5462c14823e9
Contents?: true
Size: 1.63 KB
Versions: 1
Compression:
Stored size: 1.63 KB
Contents
module Harvesting module Models class Base attr_accessor :attributes attr_reader :harvest_client def initialize(attrs, opts = {}) @models = {} @attributes = attrs.dup @harvest_client = opts[:client] || Harvesting::Client.new(opts) end def self.attributed(*attribute_names) attribute_names.each do |attribute_name| define_method(attribute_name) do @attributes[__method__.to_s] end end end def self.modeled(opts = {}) opts.each do |attribute_name, model| attribute_name_string = attribute_name.to_s Harvesting::Models::Base.send :define_method, attribute_name_string do @models[attribute_name_string] ||= model.new(@attributes[attribute_name_string] || {}, client: harvest_client) end end end def save id.nil? ? create : update end def create @harvest_client.create(self) end def update @harvest_client.update(self) end def delete @harvest_client.delete(self) end def to_hash @attributes end def fetch self.class.new(@harvest_client.get(path), client: @harvest_client) end # Retrieves an instance of the object by ID # # @param id [Integer] the id of the object to retrieve # @param opts [Hash] options to pass along to the `Harvesting::Client` # instance def self.get(id, opts = {}) client = opts[:client] || Harvesting::Client.new(opts) self.new({ 'id' => id }, opts).fetch end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
harvesting-0.3.0 | lib/harvesting/models/base.rb |