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/configuration.rb,
lib/blueprints/root_namespace.rb,
lib/blueprints/extensions/rspec.rb,
lib/blueprints/convertable/fixtures.rb,
lib/generators/blueprints/model/model_generator.rb

Overview

Main namespace of blueprints. Contains methods for Blueprints setup.

Defined Under Namespace

Modules: Convertable, DescribeHelper, Extensions, Helper Classes: Blueprint, BlueprintNameProxy, BlueprintNotFoundError, Buildable, Configuration, Context, Converter, DemolishError, Dependency, Error, FixturesConverter, ModelGenerator, Namespace, Railtie, RootNamespace

Constant Summary collapse

VERSION =
'1.0.1'

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



78
79
80
81
82
83
84
85
86
# File 'lib/blueprints.rb', line 78

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)).start_with?(path) } }
  end
end

.configBlueprints::Configuration

Contains current configuration of blueprints

Returns:



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

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:



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/blueprints.rb', line 50

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.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/blueprints.rb', line 64

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.



99
100
101
102
103
104
# File 'lib/blueprints.rb', line 99

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.



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

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

.teardownObject

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



42
43
44
# File 'lib/blueprints.rb', line 42

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.



90
91
92
# File 'lib/blueprints.rb', line 90

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.



109
110
111
112
# File 'lib/blueprints.rb', line 109

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