Class: AppKernel::Function::Options
- Inherits:
-
Object
- Object
- AppKernel::Function::Options
- Defined in:
- lib/appkernel/curry.rb,
lib/appkernel/function.rb
Defined Under Namespace
Classes: Option
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #add(name, modifiers) ⇒ Object
- #canonicalize(args, errors, augment = true) ⇒ Object
- #curry(parent, params) ⇒ Object
- #ingest(o) ⇒ Object
-
#initialize ⇒ Options
constructor
A new instance of Options.
Constructor Details
#initialize ⇒ Options
Returns a new instance of Options.
137 138 139 140 141 142 143 |
# File 'lib/appkernel/function.rb', line 137 def initialize = {} @indexed = [] @required = [] @defaults = [] @presets = {} end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
135 136 137 |
# File 'lib/appkernel/function.rb', line 135 def end |
Instance Method Details
#add(name, modifiers) ⇒ Object
145 146 147 |
# File 'lib/appkernel/function.rb', line 145 def add(name, modifiers) ingest Option.new(name, modifiers) end |
#canonicalize(args, errors, augment = true) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/appkernel/function.rb', line 162 def canonicalize(args, errors, augment = true) @presets.dup.tap do |canonical| indexed = @indexed.dup for arg in args case arg when Hash resolved = {} for k,v in arg if opt = [k] resolved[k] = opt.resolve(v) else raise OptionsError, "unknown option '#{k}'" end end canonical.merge! resolved else if opt = indexed.shift canonical[opt.name] = opt.resolve arg else raise ArgumentError, "too many arguments" end end end if augment for k in @defaults canonical[k] = [k].default unless canonical[k] end canonical.reject! {|k,v| v.nil?} for k in @required - canonical.keys errors.add(k, "missing required option '#{k}'") end end end end |
#curry(parent, params) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/appkernel/curry.rb', line 11 def curry(parent, params) @presets.merge! parent.canonicalize([params], nil, false) applied, unapplied = parent..values.partition {|o| @presets.has_key?(o.name)} unapplied.each do |option| ingest option end applied.each do |option| if option.required? && @presets[option.name].nil? raise AppKernel::OptionsError, "required option '#{option.name}' may not be nil" end end end |
#ingest(o) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/appkernel/function.rb', line 149 def ingest(o) [o.name] = o if o.index @indexed[o.index] = o @indexed.compact! end @required << o.name if o.required? @defaults << o.name if o.default? if o.default? && o.type raise IllegalOptionError, "option '#{o.name}' is not a #{o.type}" unless o.default.kind_of?(o.type) end end |