Class: Scripting::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/scripting/options.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/scripting/options.rb', line 22

def method_missing(name, *args, &blk)
  name = name.to_s

  # predicate?
  if name =~/\?$/
    return !!self[name.chop]
  end

  # assignment=
  if name =~ /=$/
    name.chop!
  end

  if block_given?
    self[name] = blk
  elsif args.length > 0
    self[name] = args.first
  else
    self[name]
  end
end

Instance Method Details

#[](name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/scripting/options.rb', line 6

def [](name)
  ivar = "@#{name}"
  value = instance_variable_get(ivar)

  if value.respond_to? :call
    instance_eval &value
  else
    value
  end
end

#[]=(name, value) ⇒ Object



17
18
19
20
# File 'lib/scripting/options.rb', line 17

def []=(name, value)
  ivar = "@#{name}"
  instance_variable_set(ivar, value)
end

#describe(&blk) ⇒ Object



4
# File 'lib/scripting/options.rb', line 4

def describe &blk; instance_eval &blk; end