class BinData::AcceptedParametersPlugin::AcceptedParameters
BinData objects accept parameters when initializing. AcceptedParameters allow a BinData class to declaratively identify accepted parameters as mandatory, optional, default or mutually exclusive.
Public Class Methods
new(ancestor_parameters = nil)
click to toggle source
# File lib/bindata/params.rb, line 43 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
Private Class Methods
invalid_parameter_names()
click to toggle source
# File lib/bindata/params.rb, line 118 def self.invalid_parameter_names unless defined? @invalid_names all_names = LazyEvaluator.instance_methods(true) + Kernel.methods allowed_names = [:name, :type] invalid_names = (all_names - allowed_names).uniq @invalid_names = Hash[*invalid_names.collect { |key| [key.to_sym, true] }.flatten] end @invalid_names end
Public Instance Methods
all()
click to toggle source
# File lib/bindata/params.rb, line 95 def all (@mandatory + @optional + @default.keys).uniq end
default(args = nil)
click to toggle source
# File lib/bindata/params.rb, line 73 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)
click to toggle source
# File lib/bindata/params.rb, line 57 def mandatory(*args) if not args.empty? @mandatory.concat(to_syms(args)) @mandatory.uniq! end @mandatory end
mutually_exclusive(*args)
click to toggle source
# File lib/bindata/params.rb, line 83 def mutually_exclusive(*args) arg1 = args.shift while not 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)
click to toggle source
# File lib/bindata/params.rb, line 65 def optional(*args) if not args.empty? @optional.concat(to_syms(args)) @optional.uniq! end @optional end
Private Instance Methods
ensure_valid_names(names)
click to toggle source
# File lib/bindata/params.rb, line 108 def ensure_valid_names(names) invalid_names = self.class.invalid_parameter_names names.each do |name| if invalid_names.include?(name) raise NameError.new("Rename parameter '#{name}' " + "as it shadows an existing method.", name) end end end
to_syms(args)
click to toggle source
# File lib/bindata/params.rb, line 102 def to_syms(args) syms = args.collect { |el| el.to_sym } ensure_valid_names(syms) syms end