Class: Lti2Commons::SubstitutionSupport::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/lti2_commons/lib/lti2_commons/substitution_support.rb

Overview

Resolver resolves values by name from a variety of object sources. It’s useful for variable substitution.

The supported object sources are:

Hash      ::= value by key
Proc      ::= single-argument block evaluation
Method    ::= single-argument method obect evaluation
Resolver  ::= a nested resolver.  useful for scoping resolvers;
              i.e. a constant, global inner resolver, but a one-time outer-resolver
Object    ::= value by dynamic instrospection of any object's accessor

See the accompanying tester for numerous examples

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResolver

Returns a new instance of Resolver.



18
19
20
# File 'lib/lti2_commons/lib/lti2_commons/substitution_support.rb', line 18

def initialize
  @resolver_hash = Hash.new
end

Instance Attribute Details

#resolversObject

Returns the value of attribute resolvers.



16
17
18
# File 'lib/lti2_commons/lib/lti2_commons/substitution_support.rb', line 16

def resolvers
  @resolvers
end

Instance Method Details

#add_resolver(key, resolver) ⇒ Object

Add some type of resolver object_source to the Resolver

Parameters:

  • key (String)

    A dotted name with the leading zone indicating the object category

  • resolver (ObjectSource)

    a raw object_source for resolving



28
29
30
31
32
33
34
35
36
37
# File 'lib/lti2_commons/lib/lti2_commons/substitution_support.rb', line 28

def add_resolver(key, resolver)
  if resolver.is_a? Resolver
    # Resolvers themselves should always be generic
    key_sym = :*
  else
    key_sym = key.to_sym
  end
  @resolver_hash[key_sym] = [] unless @resolver_hash.key? key_sym
  @resolver_hash[key_sym] << resolver
end

#resolve(full_name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lti2_commons/lib/lti2_commons/substitution_support.rb', line 39

def resolve(full_name)
  full_name ||= ''
  zones = full_name.split('.')
  return full_name if zones[0].blank?
  category = zones[0].to_sym
  name = zones[1..-1].join('.')

  # Find any hits within category
  @resolver_hash.each_pair do |k, v|
    if k == category
      result = resolve_by_category(full_name, name, v)
      return result if result
    end
  end

  # Find any hits in global category
  resolvers = @resolver_hash[:*]
  result = resolve_by_category full_name, name, resolvers if resolvers
  return result if result

  "#{full_name}"
end

#to_sObject



62
63
64
# File 'lib/lti2_commons/lib/lti2_commons/substitution_support.rb', line 62

def to_s
  "Resolver for [#{@resolver_hash.keys}]"
end