Module: Onceler::BasicHelpers::ClassMethods

Defined in:
lib/onceler/basic_helpers.rb

Instance Method Summary collapse

Instance Method Details

#before(*args, &block) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/onceler/basic_helpers.rb', line 61

def before(*args, &block)
  if once_scope?(args.first)
    before_once(&block)
  else
    super(*args, &block)
  end
end

#before_once(&block) ⇒ Object



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

def before_once(&block)
  onceler(:create) << block
end

#create_onceler!Object



77
78
79
80
# File 'lib/onceler/basic_helpers.rb', line 77

def create_onceler!
  add_onceler_hooks!
  Recorder.new(parent_onceler)
end

#let_once(name, &block) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
# File 'lib/onceler/basic_helpers.rb', line 15

def let_once(name, &block)
  raise ArgumentError, "wrong number of arguments (0 for 1)" if name.nil?
  raise "#let or #subject called without a block" if block.nil?
  onceler(:create)[name] = block
  @current_let_once = name
  define_method(name) { onceler[name] }
end

#method_added(method_name) ⇒ Object

make sure we have access to subsequently added methods when recording (not just ‘lets’). note that this really only works for truly functional methods with no external dependencies. e.g. methods that add stubs or set instance variables will not work while recording



87
88
89
90
91
92
93
94
# File 'lib/onceler/basic_helpers.rb', line 87

def method_added(method_name)
  return if method_name == @current_let_once
  return if !@onceler
  proxy = onceler.helper_proxy ||= new
  onceler.helper_methods[method_name] ||= Proc.new do |*args|
    proxy.send method_name, *args
  end
end

#once_scope?(scope) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/onceler/basic_helpers.rb', line 34

def once_scope?(scope)
  scope == :once
end

#onceler(create_own = false) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/onceler/basic_helpers.rb', line 69

def onceler(create_own = false)
  if create_own
    @onceler ||= create_onceler!
  else
    @onceler || parent_onceler
  end
end

#subject_once(name = nil, &block) ⇒ Object

TODO NamedSubjectPreventSuper



24
25
26
27
28
# File 'lib/onceler/basic_helpers.rb', line 24

def subject_once(name = nil, &block)
  name ||= :subject
  let_once(name, &block)
  alias_method :subject, name if name != :subject
end