class Toil::Arguments
Public Class Methods
new(args, &blk)
click to toggle source
# 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
Public Instance Methods
[](key)
click to toggle source
# File lib/toil/arguments.rb, line 41 def [](key) @attributes[key] end
arg(value = nil, &blk)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/toil/arguments.rb, line 27 def attribute(key, value = nil, &blk) @attributes || arg({}) @attributes.__set__(key, value, &blk) end
attributes_at()
click to toggle source
# 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)
click to toggle source
# File lib/toil/arguments.rb, line 37 def dup(&blk) self.class.new(@args, &blk) end
method_missing(m, *args, &blk)
click to toggle source
# File lib/toil/arguments.rb, line 13 def method_missing(m, *args, &blk) attribute(m, args.first, &blk) end
to_a(*overrides)
click to toggle source
# 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
Also aliased as: to_ary
to_h(overrides = {})
click to toggle source
# File lib/toil/arguments.rb, line 58 def to_h(overrides = {}) @attributes.to_h(overrides) end
Also aliased as: to_hash
Private Instance Methods
__arg__(arg, &blk)
click to toggle source
# File lib/toil/arguments.rb, line 65 def __arg__(arg, &blk) return DynamicValue.new(&blk) if block_given? case arg when Attributes arg.dup when DynamicValue arg when Hash Attributes.new(arg) else DynamicValue.new(arg) end end
__array__(args)
click to toggle source
# File lib/toil/arguments.rb, line 80 def __array__(args) args.is_a?(Array) ? args : [args] end
__find_attributes__()
click to toggle source
# File lib/toil/arguments.rb, line 84 def __find_attributes__ @attributes ||= @args.reverse.find { |v| v.is_a?(Attributes) } end
__merge_or_override__(override, arg)
click to toggle source
# File lib/toil/arguments.rb, line 88 def __merge_or_override__(override, arg) return arg.to_h(override) if override.is_a?(Hash) && arg.is_a?(Attributes) override end