Module: Onceler::BasicHelpers::ClassMethods

Defined in:
lib/onceler/basic_helpers.rb

Instance Method Summary collapse

Instance Method Details

#after(*args, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/onceler/basic_helpers.rb', line 80

def after(*args, &block)
  scope = args.first
  case scope
  when :record, :reset
    onceler(:create).hooks[:after][scope] << block
  else
    super
  end
end

#around(*args, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/onceler/basic_helpers.rb', line 90

def around(*args, &block)
  scope = args.first
  case scope
  when *once_scopes
    around_once(&block)
  when :once_and_each
    around_once(&block)
    around(:each, &block)
  else
    super(*args, &block)
  end
end

#around_once(&block) ⇒ Object



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

def around_once(&block)
  onceler(:create).add_around(block)
  add_onceler_hooks!
end

#before(*args, &block) ⇒ Object



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

def before(*args, &block)
  scope = args.first
  case scope
  when :record, :reset
    onceler(:create).hooks[:before][scope] << block
  when *once_scopes
    before_once(&block)
  else
    super(*args, &block)
  end
end

#before_once(&block) ⇒ Object



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

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

#let_once(name, &block) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
# 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
  add_onceler_hooks!
  @current_let_once = name
  define_method(name) { onceler[name] }
end

#once_scopesObject



41
42
43
# File 'lib/onceler/basic_helpers.rb', line 41

def once_scopes
  [:once]
end

#onceler(create_own = false) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/onceler/basic_helpers.rb', line 103

def onceler(create_own = false)
  if create_own
    @onceler ||= Recorder.new(self)
  else
    @onceler || parent_onceler
  end
end

#parent_oncelerObject



111
112
113
114
# File 'lib/onceler/basic_helpers.rb', line 111

def parent_onceler
  return unless superclass.respond_to?(:onceler)
  superclass.onceler
end

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

TODO NamedSubjectPreventSuper



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

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