Class: Toil::Arguments
- Inherits:
-
Object
show all
- Defined in:
- lib/toil/arguments.rb
Instance Method Summary
collapse
Constructor Details
#initialize(args, &blk) ⇒ Arguments
Returns a new instance of Arguments.
7
8
9
10
11
|
# File 'lib/toil/arguments.rb', line 7
def initialize(args, &blk)
@args = __array__(args).map { |v| __arg__(v) }
__find_attributes__
instance_eval(&blk) if block_given?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &blk) ⇒ Object
13
14
15
|
# File 'lib/toil/arguments.rb', line 13
def method_missing(m, *args, &blk)
attribute(m, args.first, &blk)
end
|
Instance Method Details
#[](key) ⇒ Object
41
42
43
|
# File 'lib/toil/arguments.rb', line 41
def [](key)
@attributes[key]
end
|
#arg(value = nil, &blk) ⇒ Object
17
18
19
|
# File 'lib/toil/arguments.rb', line 17
def arg(value = nil, &blk)
arg_at(@args.size, value, &blk)
end
|
#arg_at(index, value = nil, &blk) ⇒ Object
21
22
23
24
25
|
# File 'lib/toil/arguments.rb', line 21
def arg_at(index, value = nil, &blk)
@args[index] = __arg__(value, &blk)
__find_attributes__
@args[index]
end
|
#attribute(key, value = nil, &blk) ⇒ Object
27
28
29
30
|
# File 'lib/toil/arguments.rb', line 27
def attribute(key, value = nil, &blk)
@attributes || arg({})
@attributes.__set__(key, value, &blk)
end
|
#attributes_at ⇒ Object
32
33
34
35
|
# File 'lib/toil/arguments.rb', line 32
def attributes_at
@args.each_with_index { |v, i| return i if v == @attributes }
nil
end
|
#dup(&blk) ⇒ Object
37
38
39
|
# File 'lib/toil/arguments.rb', line 37
def dup(&blk)
self.class.new(@args, &blk)
end
|
#to_a(*overrides) ⇒ Object
Also known as:
to_ary
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/toil/arguments.rb', line 45
def to_a(*overrides)
@args.each_with_index.map do |a, i|
if overrides.size > i
__merge_or_override__(overrides[i], a)
elsif a.is_a?(Attributes)
a.to_h
else
a.call
end
end + Array(overrides[(@args.size)..-1])
end
|
#to_h(overrides = {}) ⇒ Object
Also known as:
to_hash
58
59
60
|
# File 'lib/toil/arguments.rb', line 58
def to_h(overrides = {})
@attributes.to_h(overrides)
end
|