Class: V::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/v/arguments.rb

Defined Under Namespace

Classes: Slot

Instance Method Summary collapse

Constructor Details

#initialize(op) {|_self| ... } ⇒ Arguments

Returns a new instance of Arguments.

Yields:

  • (_self)

Yield Parameters:

  • _self (V::Arguments)

    the object that the method was called on



5
6
7
8
9
10
11
# File 'lib/v/arguments.rb', line 5

def initialize(op, &args)
  @op, @first_argument = op.to_sym, nil

  @slots = []
  yield self if block_given?
  @slots.freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(argument, *defaults) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/v/arguments.rb', line 13

def method_missing(argument, *defaults)
  options = Hash === defaults.last ? defaults.pop : {}
  @first_argument = argument if options[:first]

  slot = Slot.new argument, defaults, options
  @slots << slot

  slot
end

Instance Method Details

#%(args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/v/arguments.rb', line 27

def %(args)
  opts = Hash === args.last ? args.pop : {}
  opts[@first_argument] = args.shift if @first_argument

  op_args = @slots.inject([@op]) { |ca, slot|
    if String === slot
      ca << slot
    elsif key = slot.key(opts)
      if slot.standalone? and opts[key]
        ca << slot
      else
        value = opts[key]

        if slot.defaults_to? value then ca
        else
          ca << slot.to_s % quote(value)
        end
      end
    else
      ca
    end
  }.concat args.map { |arg| quote arg }

  op_args * ' '
end

#<<(string) ⇒ Object



23
24
25
# File 'lib/v/arguments.rb', line 23

def <<(string)
  @slots << string
end

#inspectObject



53
54
55
# File 'lib/v/arguments.rb', line 53

def inspect
  @slots.inspect
end