Class: Blueprints::EvalContext

Inherits:
Object
  • Object
show all
Defined in:
lib/blueprints/eval_context.rb

Instance Method Summary collapse

Instance Method Details

#build(*blueprints) ⇒ Object

Builds blueprints by delegating to root namespace.

Parameters:

  • blueprints (Array<String, Symbol>)

    Names of buildables.

Returns:

  • Result of last buildable.



26
27
28
# File 'lib/blueprints/eval_context.rb', line 26

def build(*blueprints)
  Namespace.root.build(blueprints)
end

#copy_instance_variables(target) ⇒ Object

Copy instance variables to another object.

Parameters:

  • target

    Object to copy instance variables to.



5
6
7
8
9
# File 'lib/blueprints/eval_context.rb', line 5

def copy_instance_variables(target)
  instance_variables.each do |iv_name|
    target.instance_variable_set(iv_name, instance_variable_get(iv_name))
  end
end

#instance_eval(context, options, &block) ⇒ Object

Sets options and attributes and evaluates block against self.

Parameters:

  • context (Blueprints::Context)

    Context of buildable object. Used to extract attributes.

  • options (Hash)

    Options hash, merged into attributes.



14
15
16
17
18
19
20
21
# File 'lib/blueprints/eval_context.rb', line 14

def instance_eval(context, options, &block)
  options = normalize_hash(options)
  define_singleton_method(:options) { options }
  attributes = normalize_hash(context.attributes).merge(options)
  define_singleton_method(:attributes) { attributes }

  super(&block)
end

#normalize_hash(hash) ⇒ Hash

Normalizes attributes hash by evaluating all Proc and Blueprints::Dependency objects against itself.

Parameters:

  • hash (Hash)

    Attributes hash.

Returns:

  • (Hash)

    Normalized hash.



33
34
35
36
37
38
39
40
41
# File 'lib/blueprints/eval_context.rb', line 33

def normalize_hash(hash)
  hash.each_with_object({}) do |(attr, value), normalized|
    normalized[attr] = if value.respond_to?(:to_proc) and not Symbol === value
                         instance_exec(&value)
                       else
                         value
                       end
  end
end