class RemoteResource::ScopeEvaluator

Attributes

scope[R]

Public Class Methods

new(scope = nil) click to toggle source
# File lib/remote_resource/scope_evaluator.rb, line 5
def initialize(scope = nil)
  @scope = normalize_scope(scope)
end

Public Instance Methods

evaluate_on(target_object) click to toggle source
# File lib/remote_resource/scope_evaluator.rb, line 9
def evaluate_on(target_object)
  scope = {}
  @scope.each_pair do |attr_key, target_method|
    scope[attr_key.to_sym] = target_object.send(target_method.to_sym)
  end
  scope
end

Private Instance Methods

eval_attribute_scope(target_object) click to toggle source

Internal: Returns a hash where the values of the scope have been evaluated on the provided target_object.

# File lib/remote_resource/scope_evaluator.rb, line 28
def eval_attribute_scope(target_object)
  scope = {}
  @scope.each_pair do |attr_key, target_method|
    scope[attr_key.to_sym] = target_object.send(target_method.to_sym)
  end
  scope
end
normalize_scope(scope) click to toggle source

Internal: Normalizes the scope argument. Always returns the scope as a Hash, despite it being able to be specified as a Symbol, Array, or Hash. An undefined scope returns an empty Hash.

# File lib/remote_resource/scope_evaluator.rb, line 39
def normalize_scope(scope)
  if ! scope
    scope = {}
  elsif scope.is_a? Symbol
    scope = { scope => scope }
  elsif scope.is_a? Array
    scope = {}.tap do |hash|
      scope.each { |method| hash[method.to_sym] = method.to_sym }
    end
  end
  scope
end
should_eval_attribute_scope?() click to toggle source

Internal: Returns a Boolean indicating whether or not the scope should be looked up (evaluated on) the target_object. This allows direct values for the scope to be used, rather then references to methods on the target object.

# File lib/remote_resource/scope_evaluator.rb, line 22
def should_eval_attribute_scope?
  @scope.values.all? { |scope_value| scope_value.is_a? Symbol }
end