Sha256: aa9431e2af72425c8bde7b0d6d1b2b82c71b038b0e62df0b699f6904b36e6b0b

Contents?: true

Size: 1.53 KB

Versions: 30

Compression:

Stored size: 1.53 KB

Contents

module ActiveRemote
  module ScopeKeys
    extend ActiveSupport::Concern

    included do
      include PrimaryKey

      class_attribute :_scope_keys
      self._scope_keys = []
    end

    module ClassMethods

      ##
      # Allows you to define, at a class level, what keys should be
      # used as identifiers when making remote calls. For instance,
      #
      # class Tag < ActiveRemote::Base
      #   scope_key :user_guid
      # end
      #
      # When #scope_keys is called on Tag, it will return the primary
      # key in addition to :user_guid as the scope keys.
      #
      def scope_key(*keys)
        self._scope_keys += keys.map(&:to_s)
      end

      ##
      # Used to define what keys are required when making remote
      # persistence or refresh calls.
      #
      def scope_keys
        [ primary_key.to_s ] + _scope_keys
      end
    end

    ##
    # Instance level access to the scope key of the current class
    #
    def scope_keys
      @scope_keys ||= self.class.scope_keys
    end

    ##
    # Instance level hash of scope keys and their corresponding
    # values. For instance,
    #
    # class Tag < ActiveRemote::Base
    #   scope_key :user_guid
    # end
    #
    # would return this hash:
    #
    # {
    #   :guid      => tag[:guid],
    #   :user_guid => tag[:user_guid]
    # }
    #
    # This hash is used when accessing or modifying a remote object
    # like when calling #update in the persistence module, for example.
    def scope_key_hash
      attributes.slice(*scope_keys)
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
active_remote-2.3.2 lib/active_remote/scope_keys.rb
active_remote-2.3.1 lib/active_remote/scope_keys.rb
active_remote-2.3.0 lib/active_remote/scope_keys.rb
active_remote-2.2.0 lib/active_remote/scope_keys.rb
active_remote-2.1.1 lib/active_remote/scope_keys.rb
active_remote-2.1.0 lib/active_remote/scope_keys.rb
active_remote-2.1.0.rc2 lib/active_remote/scope_keys.rb
active_remote-2.1.0.rc1 lib/active_remote/scope_keys.rb
active_remote-2.1.0.beta2 lib/active_remote/scope_keys.rb
active_remote-2.1.0.beta1 lib/active_remote/scope_keys.rb