Module: Blueprints::Helper

Included in:
ActiveSupport::TestCase
Defined in:
lib/blueprints/helper.rb

Overview

A helper module that should be included in test framework. Adds methods build and demolish

Instance Method Summary collapse

Instance Method Details

#build(*names) ⇒ Object Also known as: build_blueprint

Builds one or more blueprints by their names. You can pass names as symbols or strings. You can also pass additional options hash which will be available by calling options in blueprint block. Returns result of last blueprint block.

Examples:

build :apple and :orange blueprints.

build :apple, :orange

build :apple blueprint with additional options.

build :apple => {:color => 'red'}

passing options to several blueprints.

build :pear, :apple => {:color => 'red'}, :orange => {:color => 'orange'}

Parameters:

  • names (Array<Symbol, String, Hash>)

    Names of blueprints/namespaces to build. Pass Hash if you want to pass additional options.

Returns:

  • Return value of last blueprint


14
15
16
# File 'lib/blueprints/helper.rb', line 14

def build(*names)
  Namespace.root.build(names, self)
end

#build!(*names) ⇒ Object #build!(count, *names) ⇒ Array Also known as: build_blueprint!

Same as Blueprints::Helper#build except that you can use it to build same blueprint several times.

Overloads:

  • #build!(count, *names) ⇒ Array

    Returns Array of return values of last blueprint, which is same size as count that you pass.

    Parameters:

    • count (Integer)

      Times to build passed blueprint

    Returns:

    • (Array)

      Array of return values of last blueprint, which is same size as count that you pass


26
27
28
29
30
31
32
# File 'lib/blueprints/helper.rb', line 26

def build!(*names)
  if names.first.is_a?(Integer)
    (0...names.shift).collect { build! *names }
  else
    Namespace.root.build(names, self, :rebuild => true)
  end
end

#build_attributes(name) ⇒ Hash

Returns attributes that are used to build blueprint.

Examples:

Setting and retrieving attributes.

# In blueprint.rb file
attributes(:name => 'apple').blueprint :apple do
  Fruit.build attributes
end

# In spec/test file
build_attributes :apple #=> {:name => 'apple'}

Parameters:

  • name (Symbol, String)

    Name of blueprint/namespace.

Returns:

  • (Hash)

    Normalized attributes of blueprint/namespace


53
54
55
56
57
# File 'lib/blueprints/helper.rb', line 53

def build_attributes(name)
  blueprint = Namespace.root[name]
  blueprint.build_parents(self)
  blueprint.normalized_attributes(self)
end

#build_with(strategy, *names) ⇒ Object

Same as Blueprints::Helper#build except it also allows you to pass strategy to use (#build always uses default strategy).

Parameters:

  • strategy (Symbol)

    Strategy to use when building blueprint/namespace.

  • names (Array<Symbol, String, Hash>)

    Names of blueprints/namespaces to build. Pass Hash if you want to pass additional options.

Returns:

  • Return value of last blueprint


38
39
40
# File 'lib/blueprints/helper.rb', line 38

def build_with(strategy, *names)
  Namespace.root.build(names, self, :strategy => strategy)
end

#d(*args) ⇒ Blueprints::Dependency Also known as: blueprint_dependency

Returns Blueprint::Dependency object that can be used to define dependencies on other blueprints.

Examples:

Building :post blueprint with different user.

build :post => {:user => d(:admin)}

Building :post blueprint by first building :user_profile with :name => 'John', then taking value of @profile and calling user on it.

build :post => {:user => d(:user_profile, :profile, :name => 'John').user}

Returns:

  • (Blueprints::Dependency)

    Dependency object that can be passed as option when building blueprint/namespace.

See Also:


66
67
68
# File 'lib/blueprints/helper.rb', line 66

def d(*args)
  Dependency.new(*args)
end

#demolish(*names) ⇒ Object Also known as: blueprint_demolish

Demolishes built blueprints (by default simply calls destroy method on result of blueprint, but can be customized).

Examples:

Demolish :apple and :pear blueprints

demolish :apple, :pear

Parameters:

  • names (Array<Symbol, String>)

    Names of blueprints/namespaces to demolish.


74
75
76
# File 'lib/blueprints/helper.rb', line 74

def demolish(*names)
  names.each { |name| Namespace.root[name].demolish(self) }
end