Class: AppKernel::FunctionApplication::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/appkernel/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, *args) ⇒ Arguments

Returns a new instance of Arguments.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/appkernel/function.rb', line 57

def initialize(app, *args)
  fun = app.function
  @app = app
  @canonical = {}
  @required = Set.new(fun.options.values.select {|o| o.required?})      
  @optorder = fun.indexed_options  

  for arg in args
    if (arg.is_a?(Hash))
      arg.each do |k,v|
        if opt = fun.options[k.to_sym]
          set opt, v
        else
          raise FunctionCallError, "#{fun.name}: unknown option :#{k}"
        end 
      end
    elsif opt = @optorder.shift
      set opt, arg
    end
  end
  for opt in fun.options.values
    if @canonical[opt.name].nil? && !opt.default.nil?
      @canonical[opt.name] = opt.default
      @required.delete opt
    end
  end
  for opt in @required
    app.errors[opt.name] = "missing required option '#{opt.name}'"
  end
  for name in fun.options.keys
    @canonical[name] = nil if @canonical[name].nil?
  end        
end

Instance Attribute Details

#canonicalObject (readonly)

Returns the value of attribute canonical.



55
56
57
# File 'lib/appkernel/function.rb', line 55

def canonical
  @canonical
end

Instance Method Details

#set(opt, value) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/appkernel/function.rb', line 91

def set(opt, value)
  resolved = opt.resolve(@app, value)      
  if !resolved.nil?
    @canonical[opt.name] = resolved
    @required.delete opt
  elsif !value.nil? && opt.required? 
    @required.delete opt
    @required.delete opt
    @app.errors[opt.name] = "no such value '#{value}' for required option '#{opt.name}'"          
  end
end