Sha256: 66ed0975b87eeecf986d97d60d9d94cf3b76df0807d9caf2dea4187994b51511

Contents?: true

Size: 744 Bytes

Versions: 2

Compression:

Stored size: 744 Bytes

Contents

module Harvesting
  module Models
    class Base
      attr_accessor :attributes
      attr_reader :client

      def initialize(attrs, opts = {})
        @attributes = attrs.dup
        @client = opts[:client] || Harvesting::Client.new(opts)
      end

      def self.attributed(*attribute_names)
        attribute_names.each do |attribute_name|
          Harvesting::Models::Base.send :define_method, attribute_name.to_s do
            @attributes[__method__.to_s]
          end
        end
      end

      def save
        id.nil? ? create : update
      end

      def create
        @client.create(self)
      end

      def update
        @client.update(self)
      end

      def to_hash
        @attributes
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
harvesting-0.2.0 lib/harvesting/models/base.rb
harvesting-0.1.0 lib/harvesting/models/base.rb