Class: Blueprints::BlueprintNameProxy

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

Overview

Acts as a proxy to buildables with regexp names. Used for caching purposes. Remembers name used and always uses it later. Allows building and demolishing it’s buildable.

Instance Method Summary collapse

Constructor Details

#initialize(name, buildable) ⇒ BlueprintNameProxy

Initializes new instance of proxy.

Parameters:

  • name (Symbol)

    Name of buildable that this proxy uses.

  • buildable (Blueprints::Buildable)

    Buildable itself, that can later be built of demolished.



7
8
9
10
11
12
13
14
15
# File 'lib/blueprints/blueprint_name_proxy.rb', line 7

def initialize(name, buildable)
  @buildable = buildable
  @name = name

  match_data = buildable.name.match(name.to_s)
  names = match_data.names.collect(&:to_sym) if match_data.respond_to?(:names)
  names = (0...match_data.captures.size).collect { |ind| :"arg#{ind}" } if names.blank?
  @options = Hash[names.zip(match_data.captures)]
end

Instance Method Details

#build(environment, options = {}) ⇒ Object

Allows building buildable. Merges regexp match data into options. If named regexp captures are used (Ruby 1.9 only), it will merge those names with appropriate values into options, otherwise options will be named arg0..n.

Parameters:

  • environment (Object)

    Context to build buildable object in.

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

    List of options to build this buildable with.



22
23
24
25
26
27
# File 'lib/blueprints/blueprint_name_proxy.rb', line 22

def build(environment, options = {})
  options[:options] ||= {}
  options[:options].merge!(@options)
  options.merge!(:name => @name)
  @buildable.build(environment, options)
end

#demolish(environment) ⇒ Object

Allows demolishing buildable. Uses remembered name to determine name of variable to call destroy on.

Parameters:

  • environment (Object)

    Eval context that this buildable was built in.



31
32
33
# File 'lib/blueprints/blueprint_name_proxy.rb', line 31

def demolish(environment)
  @buildable.demolish(environment, @name)
end