Module: TestProf::LetItBe
- Defined in:
- lib/test_prof/recipes/rspec/let_it_be.rb
Overview
Just like ‘let`, but persist the result for the whole group. NOTE: Experimental and magical, for more control use `before_all`.
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- PREFIX =
Use uniq prefix for instance variables to avoid collisions We want to use the power of Ruby’s unicode support) And we love cats!)
RUBY_ENGINE == "jruby" ? "@__jruby_is_not_cat_friendly__" : "@😸"
Class Method Summary collapse
- .config ⇒ Object
- .configure {|config| ... } ⇒ Object
- .define_let_it_be_alias(name, **default_args) ⇒ Object
- .modifiers ⇒ Object
- .module_for(group) ⇒ Object
- .wrap_with_modifiers(mods, &block) ⇒ Object
Instance Method Summary collapse
- #define_let_it_be_methods(identifier, **modifiers) ⇒ Object
- #let_it_be(identifier, **options, &block) ⇒ Object
Class Method Details
.config ⇒ Object
28 29 30 |
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 28 def config @config ||= Configuration.new end |
.configure {|config| ... } ⇒ Object
32 33 34 |
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 32 def configure yield config end |
.define_let_it_be_alias(name, **default_args) ⇒ Object
78 79 80 81 82 |
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 78 def self.define_let_it_be_alias(name, **default_args) define_method(name) do |identifier, **, &blk| let_it_be(identifier, **default_args.merge(), &blk) end end |
.modifiers ⇒ Object
36 37 38 |
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 36 def modifiers @modifiers ||= {} end |
.module_for(group) ⇒ Object
53 54 55 56 57 |
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 53 def module_for(group) modules[group] ||= begin Module.new.tap { |mod| group.prepend(mod) } end end |
.wrap_with_modifiers(mods, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 40 def wrap_with_modifiers(mods, &block) return block if mods.empty? validate_modifiers! mods -> { record = instance_eval(&block) mods.inject(record) do |rec, (k, v)| LetItBe.modifiers.fetch(k).call(rec, v) end } end |
Instance Method Details
#define_let_it_be_methods(identifier, **modifiers) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 98 def define_let_it_be_methods(identifier, **modifiers) let_accessor = LetItBe.wrap_with_modifiers(modifiers) do instance_variable_get(:"#{PREFIX}#{identifier}") end LetItBe.module_for(self).module_eval do define_method(identifier) do # Trying to detect the context (couldn't find other way so far) if /\(:context\)/.match?(@__inspect_output) instance_variable_get(:"#{PREFIX}#{identifier}") else # Fallback to let definition super() end end end let(identifier, &let_accessor) end |
#let_it_be(identifier, **options, &block) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 84 def let_it_be(identifier, **, &block) initializer = proc do instance_variable_set(:"#{PREFIX}#{identifier}", instance_exec(&block)) end if within_before_all? within_before_all(&initializer) else before_all(&initializer) end define_let_it_be_methods(identifier, **) end |