Class: BinData::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_params = nil) ⇒ AcceptedParameters

Returns a new instance of AcceptedParameters.



9
10
11
12
13
14
15
# File 'lib/bindata/params.rb', line 9

def initialize(ancestor_params = nil)
  @mandatory = ancestor_params ? ancestor_params.mandatory : []
  @optional  = ancestor_params ? ancestor_params.optional  : []
  @default   = ancestor_params ? ancestor_params.default   : Hash.new
  @mutually_exclusive = ancestor_params ?
                                 ancestor_params.mutually_exclusive : []
end

Instance Method Details

#allObject



54
55
56
# File 'lib/bindata/params.rb', line 54

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

#default(args = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/bindata/params.rb', line 35

def default(args = {})
  if not args.empty?
    ensure_valid_names(args.keys)
    args.each_pair do |param, value|
      @default[param.to_sym] = value
    end
  end
  @default.dup
end

#mandatory(*args) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/bindata/params.rb', line 17

def mandatory(*args)
  if not args.empty?
    ensure_valid_names(args)
    @mandatory.concat(args.collect { |arg| arg.to_sym })
    @mandatory.uniq!
  end
  @mandatory.dup
end

#mutually_exclusive(*args) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/bindata/params.rb', line 45

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.dup
end

#optional(*args) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/bindata/params.rb', line 26

def optional(*args)
  if not args.empty?
    ensure_valid_names(args)
    @optional.concat(args.collect { |arg| arg.to_sym })
    @optional.uniq!
  end
  @optional.dup
end