Module: Blueprints

Defined in:
lib/blueprints.rb,
lib/blueprints/errors.rb,
lib/blueprints/helper.rb,
lib/blueprints/context.rb,
lib/blueprints/version.rb,
lib/blueprints/blueprint.rb,
lib/blueprints/buildable.rb,
lib/blueprints/namespace.rb,
lib/blueprints/convertable.rb,
lib/blueprints/eval_context.rb,
lib/blueprints/configuration.rb,
lib/blueprints/root_namespace.rb,
lib/blueprints/convertable/fixtures.rb

Overview

Main namespace of blueprints. Contains methods for Blueprints setup.

Defined Under Namespace

Modules: Convertable, Extensions, Helper Classes: Blueprint, BlueprintNotFoundError, Buildable, Configuration, Context, Converter, DemolishError, Dependency, Error, EvalContext, FixturesConverter, Namespace, RootNamespace

Constant Summary collapse

VERSION =
'0.9.0'

Class Method Summary collapse

Class Method Details

.backtrace_cleanerActiveSupport::BacktraceCleaner

Returns backtrace cleaner that is used to extract lines from user application.

Returns:

  • (ActiveSupport::BacktraceCleaner)

    Backtrace cleaner



71
72
73
74
75
76
77
78
79
# File 'lib/blueprints.rb', line 71

def self.backtrace_cleaner
  @backtrace_cleaner ||= ActiveSupport::BacktraceCleaner.new.tap do |bc|
    root_sub        = /^#{config.root}[\\\/]/
    blueprints_path = File.expand_path(File.dirname(__FILE__))

    bc.add_filter { |line| line.sub(root_sub, '') }
    bc.add_silencer { |line| [blueprints_path, *Gem.path].any? { |path| File.expand_path(File.dirname(line)).starts_with?(path) } }
  end
end

.configBlueprints::Configuration

Contains current configuration of blueprints

Returns:



22
23
24
# File 'lib/blueprints.rb', line 22

def self.config
  @@config ||= Blueprints::Configuration.new
end

.enable {|config| ... } ⇒ Object

Enables blueprints support for RSpec or Test::Unit depending on whether ®Spec is defined or not. Yields Blueprints::Configuration object that you can use to configure blueprints.

Yields:

  • (config)

    Used to configure blueprints.

Yield Parameters:



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/blueprints.rb', line 43

def self.enable
  yield config if block_given?
  load
  extension = if defined? Cucumber
                'cucumber'
              elsif defined? Spec or defined? RSpec
                'rspec'
              else
                'test_unit'
              end
  require File.join(File.dirname(__FILE__), 'blueprints', 'extensions', extension)
end

.loadObject

Sets up configuration, clears database, runs scenarios that have to be prebuilt. Should be run before all test cases and before Blueprints#setup.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/blueprints.rb', line 57

def self.load
  return unless Namespace.root.empty?

  if_orm do
    DatabaseCleaner.clean_with :truncation
    DatabaseCleaner.strategy = (config.transactions ? :transaction : :truncation)
  end
  load_scenarios_files(config.filename)

  Namespace.root.prebuild(config.prebuild) if config.transactions
end

.most_used(options = {}) ⇒ Array<Array<String, Integer>>

Returns array of most used blueprints.

Parameters:

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

    Options on what blueprints to return.

Options Hash (options):

  • :count (Integer)

    Max amount of most used blueprints to return.

  • :at_least (Integer)

    Only blueprints that have at least specified number of uses will be returned.

Returns:

  • (Array<Array<String, Integer>>)

    List of most used blueprints together with their use counts.



92
93
94
95
96
97
# File 'lib/blueprints.rb', line 92

def self.most_used(options = {})
  blueprints = each_blueprint.collect { |blueprint| [blueprint.full_name, blueprint.uses] }.sort { |a, b| b[1] <=> a[1] }
  blueprints = blueprints.take(options[:count]) if options[:count]
  blueprints.reject! { |blueprint| blueprint[1] < options[:at_least] } if options[:at_least]
  blueprints
end

.setup(current_context) ⇒ Object

Setups variables from global context and starts transaction. Should be called before every test case.

Parameters:

  • current_context

    Object to copy instance variables for prebuilt blueprints/namespaces.



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

def self.setup(current_context)
  Namespace.root.setup
  Namespace.root.eval_context.copy_instance_variables(current_context)
  if_orm { DatabaseCleaner.start }
end

.teardownObject

Rollbacks transaction returning everything to state before test. Should be called after every test case.



35
36
37
# File 'lib/blueprints.rb', line 35

def self.teardown
  if_orm { DatabaseCleaner.clean }
end

.unusedArray<String>

Returns array of blueprints that have not been used until now.

Returns:

  • (Array<String>)

    List of unused blueprints.



83
84
85
# File 'lib/blueprints.rb', line 83

def self.unused
  each_blueprint.select { |blueprint| blueprint.uses.zero? }.collect(&:full_name)
end

.warn(message, blueprint) ⇒ Object

Warns a user (often about deprecated feature).

Parameters:

  • message (String)

    Message to output.

  • blueprint (Blueprints::Blueprint)

    Name of blueprint that this occurred in.



102
103
104
105
# File 'lib/blueprints.rb', line 102

def self.warn(message, blueprint)
  $stderr.puts("**WARNING** #{message}: '#{blueprint.name}'")
  $stderr.puts(backtrace_cleaner.clean(blueprint.backtrace(caller)).first)
end