Class: Loom::Pattern::DefinitionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/loom/pattern/definition_context.rb

Overview

Pattern::DefinitionContext is the collection of facts, hooks, and parent contexts a pattern is defined along side of. The context includes all contexts of parent modules.

Constant Summary collapse

NilLetValueError =
Class.new Loom::LoomError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dsl_builder, parent_context = nil) ⇒ DefinitionContext

Returns a new instance of DefinitionContext.



11
12
13
14
15
16
17
18
19
20
# File 'lib/loom/pattern/definition_context.rb', line 11

def initialize(dsl_builder, parent_context=nil)
  @fact_map = dsl_builder.facts.dup
  @let_map = dsl_builder.let_map.dup

  @hooks = dsl_builder.hooks.dup
  @parent_context = parent_context

  @merged_fact_map = merged_fact_map
  @merged_let_map = merged_let_map
end

Instance Attribute Details

#fact_mapObject (readonly)

Returns the value of attribute fact_map.



22
23
24
# File 'lib/loom/pattern/definition_context.rb', line 22

def fact_map
  @fact_map
end

#hooksObject (readonly)

Returns the value of attribute hooks.



22
23
24
# File 'lib/loom/pattern/definition_context.rb', line 22

def hooks
  @hooks
end

#let_mapObject (readonly)

Returns the value of attribute let_map.



22
23
24
# File 'lib/loom/pattern/definition_context.rb', line 22

def let_map
  @let_map
end

Instance Method Details

#after_hooksObject



61
62
63
# File 'lib/loom/pattern/definition_context.rb', line 61

def after_hooks
  Hook.after_hooks merged_hooks.reverse
end

#before_hooksObject



57
58
59
# File 'lib/loom/pattern/definition_context.rb', line 57

def before_hooks
  Hook.before_hooks merged_hooks
end

#define_let_readers(scope_object, fact_set) ⇒ Object

TODO: #define_let_readers is a TERRIBLE name. Rename this method. Also consider moving the instance_exec call to inside RunContext, it’s a bit misplaced here.

The method is called by Reference#call with the Reference::RunContext instance. The “let” defined local declarations are added as method definitions on each RunContext instance. The @merged_let_map is the map of let definitions, merged from the associated module, up the namespace graph, allowing for inheriting and overriding let declarations.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/loom/pattern/definition_context.rb', line 40

def define_let_readers(scope_object, fact_set)
  @merged_let_map.each do |let_key, let_map_entry|
    Loom.log.debug { "evaluating let expression[:#{let_key}]" }
    value = scope_object.instance_exec fact_set, &let_map_entry.block
    value = value.nil? ? let_map_entry.default : value
    Loom.log.debug1(self) { "let[:#{let_key}] => #{value}" }

    if value.nil? || value.equal?(Loom::Facts::EMPTY)
      Loom.log.error "value of let expression[:#{let_key}] is nil"
      # TODO: I'm not sure what to do here, but raising an error isn't
      # very user friendly.... as I just learned.
#          raise NilLetValueError, "empty value for let[#{let_key}]"
    end
    scope_object.define_singleton_method(let_key) { value }
  end
end

#fact_set(host_fact_set) ⇒ Object

Merges the facts defined by the pattern context with the host fact_set



27
28
29
# File 'lib/loom/pattern/definition_context.rb', line 27

def fact_set(host_fact_set)
  host_fact_set.merge merged_fact_map
end