Sha256: 97798efa189fd35a21bfb6cb5a42d4d0305da3b88acde55eed6bb2390eb0a005

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

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
        name.split('::').last
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gecko-ruby-0.12.3 lib/gecko/record/base.rb
gecko-ruby-0.12.2 lib/gecko/record/base.rb
gecko-ruby-0.12.1 lib/gecko/record/base.rb
gecko-ruby-0.12.0 lib/gecko/record/base.rb
gecko-ruby-0.11.1 lib/gecko/record/base.rb
gecko-ruby-0.11.0 lib/gecko/record/base.rb
gecko-ruby-0.10.0 lib/gecko/record/base.rb