Class: BinData::AcceptedParametersMixin::AcceptedParameters

Inherits:
Object
  • Object
show all
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.

Instance Method Summary collapse

Constructor Details

#initialize(ancestor_parameters = nil) ⇒ AcceptedParameters

Returns a new instance of AcceptedParameters.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bindata/params.rb', line 51

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

Instance Method Details

#allObject



100
101
102
# File 'lib/bindata/params.rb', line 100

def all
  (@mandatory + @optional + @default.keys).uniq
end

#default(args = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/bindata/params.rb', line 81

def default(args = {})
  if not args.empty?
    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



65
66
67
68
69
70
71
# File 'lib/bindata/params.rb', line 65

def mandatory(*args)
  if not args.empty?
    @mandatory.concat(to_syms(args))
    @mandatory.uniq!
  end
  @mandatory
end

#mutually_exclusive(*args) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/bindata/params.rb', line 91

def mutually_exclusive(*args)
  arg1, arg2 = args
  if arg1 != nil && arg2 != nil
    @mutually_exclusive.push([arg1.to_sym, arg2.to_sym])
    @mutually_exclusive.uniq!
  end
  @mutually_exclusive
end

#optional(*args) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/bindata/params.rb', line 73

def optional(*args)
  if not args.empty?
    @optional.concat(to_syms(args))
    @optional.uniq!
  end
  @optional
end