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.

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

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



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.



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.



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