Sha256: 25b4627ef2bc45295f25151fb8a79df32db5e55c600b30417288a109653a49c9

Contents?: true

Size: 1.52 KB

Versions: 19

Compression:

Stored size: 1.52 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

19 entries across 19 versions & 1 rubygems

Version Path
active_remote-7.1.0 lib/active_remote/scope_keys.rb
active_remote-6.0.3 lib/active_remote/scope_keys.rb
active_remote-6.1.2 lib/active_remote/scope_keys.rb
active_remote-7.0.0 lib/active_remote/scope_keys.rb
active_remote-6.1.1 lib/active_remote/scope_keys.rb
active_remote-6.1.0 lib/active_remote/scope_keys.rb
active_remote-6.0.2 lib/active_remote/scope_keys.rb
active_remote-6.0.1 lib/active_remote/scope_keys.rb
active_remote-6.0.0.beta lib/active_remote/scope_keys.rb
active_remote-5.2.0 lib/active_remote/scope_keys.rb
active_remote-5.2.0.beta lib/active_remote/scope_keys.rb
active_remote-5.2.0.alpha lib/active_remote/scope_keys.rb
active_remote-5.0.1 lib/active_remote/scope_keys.rb
active_remote-5.1.1 lib/active_remote/scope_keys.rb
active_remote-5.1.0 lib/active_remote/scope_keys.rb
active_remote-5.0.0 lib/active_remote/scope_keys.rb
active_remote-5.1.0.rc1 lib/active_remote/scope_keys.rb
active_remote-5.0.0.rc1 lib/active_remote/scope_keys.rb
active_remote-5.0.0.pre lib/active_remote/scope_keys.rb