Sha256: 4967e22988ce5ce19b5d30718eb84d60ae8c350818326badbad686d3cb324fa8

Contents?: true

Size: 1.86 KB

Versions: 7

Compression:

Stored size: 1.86 KB

Contents

require 'gecko/helpers/association_helper'
require 'gecko/helpers/inspection_helper'
require 'gecko/helpers/serialization_helper'
require 'gecko/helpers/validation_helper'

module Gecko
  module Record
    class Base
      include Virtus.model
      include  Gecko::Helpers::AssociationHelper
      include Gecko::Helpers::InspectionHelper
      include Gecko::Helpers::SerializationHelper
      include Gecko::Helpers::ValidationHelper

      # Set up the default attributes associated with all records
      attribute :id,          Integer,    readonly: true
      attribute :updated_at,  DateTime,   readonly: true
      attribute :created_at,  DateTime,   readonly: true

      # Overrides the default Virtus functionality to store:
      # - The Gecko::Client used to create the object
      # - a raw copy of the attributes for the association helpers to read from
      #
      # @return [undefined]
      #
      # @api private
      def initialize(client, attributes={})
        super(attributes)
        @client   = client
      end

      # Whether the record has been persisted
      #
      # @example
      #   variant.persisted? #=> true
      #
      # @return <Boolean>
      #
      # @api public
      def persisted?
        !!id
      end

      # Save a record
      #
      # @param [Hash] opts the options to save the record with
      # @option opts [Hash] :idempotency_key A unique identifier for this action
      #
      # @return <Gecko::Record::Base>
      #
      # @api public
      def save(opts = {})
        @client.adapter_for(self.class.demodulized_name).save(self, opts)
      end

      # Return the demodulized class name
      #
      # @example
      #   Gecko::Record::Product.demodulized_name #=> "Product"
      #
      # @return [String]
      #
      # @api private
      def self.demodulized_name
        self.name.split('::').last
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gecko-ruby-0.9.1 lib/gecko/record/base.rb
gecko-ruby-0.9.0 lib/gecko/record/base.rb
gecko-ruby-0.8.0 lib/gecko/record/base.rb
gecko-ruby-0.7.1 lib/gecko/record/base.rb
gecko-ruby-0.7.0 lib/gecko/record/base.rb
gecko-ruby-0.6.0 lib/gecko/record/base.rb
gecko-ruby-0.5.0 lib/gecko/record/base.rb