Module: InsaneHook

Defined in:
lib/insane_hook.rb,
lib/insane_hook/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: CommandNotRunError

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/insane_hook.rb', line 7

def self.included(mod)
  mod.class_variable_set(:@@_command, {required: [], optional: []})
  mod.extend(ClassMethods)
  mod.define_singleton_method(:need, mod.instance_method(:_need))
  mod.define_singleton_method(:allow, mod.instance_method(:_allow))
  mod.define_singleton_method(:call, mod.instance_method(:_call))
  #mod.define_method(:result, mod.instance_method(:_result))
end

Instance Method Details

#_allow(key) ⇒ Object



35
36
37
38
39
40
# File 'lib/insane_hook.rb', line 35

def _allow(key)
  fail "#{key} is not a symbol" unless key.is_a? Symbol
  args = self.class_variable_get(:@@_command)
  args[:optional] << key
  self.class_variable_set(:@@_command, args)
end

#_call(&block) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/insane_hook.rb', line 42

def _call(&block)
  define_method(:call) do
    result(nil)
    instance_eval(&block)
    self
  end
end

#_need(key) ⇒ Object



28
29
30
31
32
33
# File 'lib/insane_hook.rb', line 28

def _need(key)
  fail "#{key} is not a symbol" unless key.is_a? Symbol
  args = self.class_variable_get(:@@_command)
  args[:required] << key
  self.class_variable_set(:@@_command, args)
end

#result(value = :_no_value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/insane_hook.rb', line 16

def result(value=:_no_value)
  if value == :_no_value
    if instance_variable_defined?(:@_result)
      instance_variable_get(:@_result)
    else
      raise CommandNotRunError
    end
  else
    instance_variable_set(:@_result, value)
  end
end