Method: RSpecSystem::Helper#initialize

Defined in:
lib/rspec-system/helper.rb

#initialize(opts, clr, &block) ⇒ Helper

This method is abstract.

Override, but make sure you call super(opts, clr, &block)

Setup the helper object.

Here we establish laziness detection, provide the default :node setting and handle been called as a block automatically for the consumer. This is the main setup for the magic that is behind these helper objects.

This initialize method is usually not overridden for simple cases, but can be overridden for the purposes of munging options and providing defaults.



55
56
57
58
59
60
61
62
63
64
65
66
67
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
# File 'lib/rspec-system/helper.rb', line 55

def initialize(opts, clr, &block)
  dn = default_node

  # This is a test for the context of how this command was called
  #
  # If clr is Class or Object then it could be called as a subject, and it
  # should lazy execute its action.
  lazy = nil
  if [Class, Object].include?(clr.class) # presumes being used as a subject
    lazy = true
  elsif clr.is_a? RSpec::Core::ExampleGroup # anything inside an 'it'
    lazy = false
  else
    # We presume everything else wants results immediately
    lazy = false
  end

  # Merge defaults and such
  @opts = {
    :node => opts[:n] || dn,
    :n => opts[:node] || dn,
    :timeout => opts[:timeout] || 0,
    :lazy => lazy,
  }.merge(opts)

  # Try to figure out :node using the node helper if a string is passed
  if @opts[:node].is_a? String
    @opts[:n] = @opts[:node] = get_node_by_name(@opts[:node])
  end

  # Try to lookup result_data now, unless we are lazy
  result_data unless @opts[:lazy]

  # If called as a block, yield the result as a block
  if block_given?
    yield(self)
  end
end