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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ancestor_params = nil) ⇒ AcceptedParameters

Returns a new instance of AcceptedParameters.



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

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

Class Method Details

.invalid_parameter_namesObject



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

def self.invalid_parameter_names
  unless defined? @invalid_names
    all_names = LazyEvaluator.instance_methods(true) + Kernel.methods
    all_names.collect! { |name| name.to_s }
    allowed_names = ["type"]
    @invalid_names = all_names - allowed_names
  end
  @invalid_names
end

Instance Method Details

#allObject



64
65
66
# File 'lib/bindata/params.rb', line 64

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

#default(args = {}) ⇒ Object



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

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



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

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



55
56
57
58
59
60
61
62
# File 'lib/bindata/params.rb', line 55

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



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

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