Class: BinData::AcceptedParametersPlugin::AcceptedParameters
- Inherits:
-
Object
- Object
- BinData::AcceptedParametersPlugin::AcceptedParameters
- Defined in:
- lib/bindata/params.rb
Overview
BinData objects accept parameters when initializing. AcceptedParameters allow a BinData class to declaratively identify accepted parameters as mandatory, optional, default or mutually exclusive.
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #default(args = nil) ⇒ Object
-
#initialize(ancestor_parameters = nil) ⇒ AcceptedParameters
constructor
A new instance of AcceptedParameters.
- #mandatory(*args) ⇒ Object
- #mutually_exclusive(*args) ⇒ Object
- #optional(*args) ⇒ Object
Constructor Details
#initialize(ancestor_parameters = nil) ⇒ AcceptedParameters
Returns a new instance of AcceptedParameters.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/bindata/params.rb', line 42 def initialize(ancestor_parameters = nil) if ancestor_parameters @mandatory = ancestor_parameters.mandatory.dup @optional = ancestor_parameters.optional.dup @default = ancestor_parameters.default.dup @mutually_exclusive = ancestor_parameters.mutually_exclusive.dup else @mandatory = [] @optional = [] @default = Hash.new @mutually_exclusive = [] end end |
Class Method Details
.invalid_parameter_names ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/bindata/params.rb', line 118 def invalid_parameter_names @invalid_parameter_names ||= begin all_names = LazyEvaluator.instance_methods(true) allowed_names = [:name, :type] invalid_names = (all_names - allowed_names).uniq Hash[*invalid_names.collect { |key| [key.to_sym, true] }.flatten] end end |
Instance Method Details
#all ⇒ Object
94 95 96 |
# File 'lib/bindata/params.rb', line 94 def all (@mandatory + @optional + @default.keys).uniq end |
#default(args = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/bindata/params.rb', line 72 def default(args = nil) if args to_syms(args.keys) # call for side effect of validating names args.each_pair do |param, value| @default[param.to_sym] = value end end @default end |
#mandatory(*args) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/bindata/params.rb', line 56 def mandatory(*args) unless args.empty? @mandatory.concat(to_syms(args)) @mandatory.uniq! end @mandatory end |
#mutually_exclusive(*args) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bindata/params.rb', line 82 def mutually_exclusive(*args) arg1 = args.shift until args.empty? args.each do |arg2| @mutually_exclusive.push([arg1.to_sym, arg2.to_sym]) @mutually_exclusive.uniq! end arg1 = args.shift end @mutually_exclusive end |
#optional(*args) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/bindata/params.rb', line 64 def optional(*args) unless args.empty? @optional.concat(to_syms(args)) @optional.uniq! end @optional end |