Module: Prefactory

Defined in:
lib/prefactory/version.rb,
lib/rspec/core/prefactory.rb

Overview

Copyright © 2014, VMware, Inc. All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rspec/core/prefactory.rb', line 68

def self.included(base)
  base.extend RSpecAroundAll

  # Wrap outermost describe block in a transaction, so before(:all) data is rolled back at the end of this suite.
  base.before(:all) do
    clear_prefactory_map
    clear_prefactory_memoizations
  end
  base.around(:all) do |group|
    ActiveRecord::Base.with_disposable_transaction { group.run_examples }
  end
  base.after(:all) do
    clear_prefactory_memoizations
    clear_prefactory_map
  end

  # Wrap each example in a transaction, instead of using Rails' transactional
  # fixtures, which does not support itself being wrapped in an outermost transaction.
  base.around(:each) do |example|
    clear_prefactory_memoizations
    ActiveRecord::Base.with_disposable_transaction { example.run }
    clear_prefactory_memoizations
  end

  # Wrap each ExampleGroup in a transaction, so group-level before(:all) settings
  # are scoped only to the group.
  base.instance_eval do
    def describe_with_transaction(*args, &block)
      modified_block = proc do
        instance_eval do
          before(:all) { clear_prefactory_memoizations }
          around(:all) do |group|
            ActiveRecord::Base.with_disposable_transaction { group.run_examples }
          end
          after(:all) { clear_prefactory_memoizations }
        end
        instance_eval(&block)
      end
      describe_without_transaction(*args, &modified_block)
    end

    def before_with_detect_before_all(*args, &block)
      before_all_context = (args.first == :all)
      modified_block = proc do
        @before_all_context = before_all_context
        instance_eval(&block)
        @before_all_context = nil
      end
      before_without_detect_before_all(*args, &modified_block)
    end

    class << self
      alias_method_chain :describe, :transaction
      alias_method :context, :describe
      alias_method_chain :before, :detect_before_all
    end

    def set!(key, *args, &set_block)
      before(:all) do
        modified_block = proc { instance_eval(&set_block) } if set_block
        prefactory_add(key, *args, &modified_block)
      end
    end

  end

  # allow shorthand access to a prefabricated object
  # e.g. with prefactory_add(:active_user), calling the method active_user will return the (memoized) object,
  # by invoking the 'prefactory' method.
  base.class_eval do
    def method_missing(key, *args, &block)
      if prefactory_lookup(key)
        prefactory(key)
      else
        super
      end
    end
  end

end

Instance Method Details

#in_before_all?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rspec/core/prefactory.rb', line 44

def in_before_all?
  @before_all_context
end

#prefactory(key) ⇒ Object

instantiate, or access an already-instantiated-and-memoized, prefabricated object.



33
34
35
36
37
38
39
40
41
42
# File 'lib/rspec/core/prefactory.rb', line 33

def prefactory(key)
  lookup = prefactory_lookup(key)
  return nil unless lookup.present?
  @prefactory_memo ||= {}
  begin
    @prefactory_memo[key] ||= lookup[:class].find(lookup[:id])
  rescue
  end
  @prefactory_memo[key]
end

#prefactory_add(key, *args, &block) ⇒ Object

prefabricate an object. Can be passed any block that returns a class accessible by Klass.find(id), or, if no block is passed, invokes create(key, options) to use e.g. a FactoryGirl factory of that key name.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rspec/core/prefactory.rb', line 50

def prefactory_add(key, *args, &block)
  raise "prefactory_add can only be used in a before(:all) context.  Change to a before(:all) or set!, or use let/let! instead." unless in_before_all?
  result = nil
  clear_prefactory_memoizations
  if block
    result = yield
  else
    result = create(key, *args) if respond_to?(:create)
  end
  if result.present?
    @prefactory[key] = { :class => result.class, :id => result.id }
  else
    warn "WARNING: Failed to add #{key} to prefactory: block result not present"
  end
  clear_prefactory_memoizations
  result
end

#prefactory_lookup(key) ⇒ Object



28
29
30
# File 'lib/rspec/core/prefactory.rb', line 28

def prefactory_lookup(key)
  @prefactory[key]
end