Module: Map::Options

Defined in:
lib/map/options.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments


44
45
46
# File 'lib/map/options.rb', line 44

def arguments
  @arguments
end

Class Method Details

.for(arg) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/map/options.rb', line 5

def for(arg)
  options =
    case arg
      when Hash
        arg
      when Array
        parse(arg)
      when String, Symbol
        {arg => true}
      else
        raise(ArgumentError, arg.inspect) unless arg.respond_to?(:to_hash)
        arg.to_hash
    end

  unless options.is_a?(Options)
    options = Map.for(options)
    options.extend(Options)
  end

  raise unless options.is_a?(Map)

  options
end

.parse(arg) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/map/options.rb', line 29

def parse(arg)
  case arg
    when Array
      arguments = arg
      arguments.extend(Arguments) unless arguments.is_a?(Arguments)
      options = arguments.options
    when Hash
      options = arg
      options = Options.for(options)
    else
      raise(ArgumentError, "`arg` should be an Array or Hash")
  end
end

Instance Method Details

#del_opt(opts) ⇒ Object Also known as: delopt


98
99
100
101
102
103
# File 'lib/map/options.rb', line 98

def del_opt(opts)
  [ opts ].flatten.each do |opt|
    return delete(opt) if has_key?(opt)
  end
  nil
end

#del_opts(*opts) ⇒ Object Also known as: delopts, delopts!


106
107
108
109
# File 'lib/map/options.rb', line 106

def del_opts(*opts)
  opts.flatten.map{|opt| delopt(opt)}
  opts
end

#get_opt(opts, options = {}) ⇒ Object Also known as: getopt


67
68
69
70
71
72
73
74
# File 'lib/map/options.rb', line 67

def get_opt(opts, options = {})
  options = Map.for(options.is_a?(Hash) ? options : {:default => options})
  default = options[:default]
  [ opts ].flatten.each do |opt|
    return fetch(opt) if has_key?(opt)
  end
  default
end

#get_opts(*opts) ⇒ Object Also known as: getopts


77
78
79
# File 'lib/map/options.rb', line 77

def get_opts(*opts)
  opts.flatten.map{|opt| getopt(opt)}
end

#has_opt(opts) ⇒ Object Also known as: hasopt, hasopt?, has_opt?


82
83
84
85
86
87
# File 'lib/map/options.rb', line 82

def has_opt(opts)
  [ opts ].flatten.each do |opt|
    return true if has_key?(opt)
  end
  false
end

#has_opts(*opts) ⇒ Object Also known as: hasopts?, has_opts?


92
93
94
# File 'lib/map/options.rb', line 92

def has_opts(*opts)
  opts.flatten.all?{|opt| hasopt(opt)}
end

#popObject


46
47
48
49
# File 'lib/map/options.rb', line 46

def pop
  arguments.pop if arguments.last.object_id == object_id
  self
end

#pop!Object


55
56
57
58
# File 'lib/map/options.rb', line 55

def pop!
  arguments.pop if arguments.last.object_id == object_id
  self
end

#popped?Boolean

Returns:

  • (Boolean)

51
52
53
# File 'lib/map/options.rb', line 51

def popped?
  !(arguments.last.object_id == object_id)
end

#set_opt(opts, value = nil) ⇒ Object Also known as: setopt, setopt!


113
114
115
116
117
118
# File 'lib/map/options.rb', line 113

def set_opt(opts, value = nil)
  [ opts ].flatten.each do |opt|
    return self[opt]=value
  end
  return value
end

#set_opts(opts) ⇒ Object Also known as: setopts, setopts!


122
123
124
125
# File 'lib/map/options.rb', line 122

def set_opts(opts)
  opts.each{|key, value| setopt(key, value)}
  opts
end