Class: Blueprints::Dependency

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

Overview

Class for defining blueprint dependencies.

Instance Method Summary collapse

Constructor Details

#d(name, options = {}) ⇒ Dependency #d(name, instance_variable_name, options = {}) ⇒ Dependency

Initializes new Blueprints::Dependency object.

Examples:

Build blueprint ‘blueprint’ and returns value of @blueprint instance variable.

Blueprints::Dependency.new(:blueprint)

Build blueprint ‘blueprint’ and returns value of @value instance variable.

Blueprints::Dependency.new(:blueprint, value)

Build blueprint ‘blueprint’ with options and returns value of @value instance variable.

Blueprints::Dependency.new(:blueprint, :option => true)

Register called methods

d = Blueprints::Dependency.new(:blueprint).name.size

Overloads:

  • #d(name, options = {}) ⇒ Dependency

    Use result of blueprint/namespace name and pass options when building.

    Parameters:

    • name (Symbol, String)

      Name of blueprint/namespace.

    • options (Hash) (defaults to: {})

      Options to pass when building blueprint/namespace.

  • #d(name, instance_variable_name, options = {}) ⇒ Dependency

    Build blueprint/namespace with options and use differently names instance variable as result.

    Parameters:

    • name (Symbol, String)

      Name of blueprint/namespace.

    • instance_variable_name (Symbol, String)

      Name of instance variable to use as a result.

    • options (Hash) (defaults to: {})

      Options to pass when building blueprint/namespace.



23
24
25
26
27
28
# File 'lib/blueprints/dependency.rb', line 23

def initialize(name, *args)
  @name     = name
  @options  = args.extract_options!
  @iv_name  = (args.first || @name).to_s.gsub('.', '_')
  @registry = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Blueprints::Dependency

Catches all missing methods to later replay when asking for value.

Returns:



44
45
46
47
# File 'lib/blueprints/dependency.rb', line 44

def method_missing(method, *args, &block)
  @registry << [method, args, block]
  self
end

Instance Method Details

#to_procProc

Returns block that builds blueprint (if necessary) takes instance variable for this dependency and calls all methods from method registry.

Returns:

  • (Proc)

    Proc that can be called to return value for this dependency.



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

def to_proc
  name, options, registry, variable_name = @name, @options, @registry, @iv_name
  Proc.new do
    build name => options
    registry.inject(instance_variable_get(:"@#{variable_name}")) do |value, (method, args, block)|
      value.send(method, *args, &block)
    end
  end
end