Class: Lti2Commons::SubstitutionSupport::Resolver

Inherits:
Object
  • Object
show all
Defined in:
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.



20
21
22
# File 'lib/lti2_commons/substitution_support.rb', line 20

def initialize
  @resolver_hash = Hash.new
end

Instance Attribute Details

#resolversObject

Returns the value of attribute resolvers.



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

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



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

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

#resolve(full_name) ⇒ Object



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

def resolve(full_name)
  zones = full_name.split('.')
  category = zones[0].to_sym
  name = zones[1..-1].join('.')
  
  # Find any hits within category
  @resolver_hash.each_pair {|k, v| 
    if k == category
      result = resolve_by_category full_name, name, v
      return result if result
    end
  }     
  
  # Find any hits in global category
  resolvers = @resolver_hash[:*]
  result = resolve_by_category full_name, name, resolvers if resolvers
  return result if result        
  
  return "$#{full_name}"
end

#to_sObject



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

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