Class: Samovar::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/samovar/options.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = "Options", key: :options) ⇒ Options

Returns a new instance of Options.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/samovar/options.rb', line 18

def initialize(title = "Options", key: :options)
  @title = title
  @ordered = []
  
  # We use this flag to option cache to improve parsing performance:
  @keyed = {}
  
  @key = key
  
  @defaults = {}
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



42
43
44
# File 'lib/samovar/options.rb', line 42

def defaults
  @defaults
end

#keyObject (readonly)

Returns the value of attribute key.



41
42
43
# File 'lib/samovar/options.rb', line 41

def key
  @key
end

#orderedObject (readonly)

Returns the value of attribute ordered.



39
40
41
# File 'lib/samovar/options.rb', line 39

def ordered
  @ordered
end

#titleObject (readonly)

Returns the value of attribute title.



38
39
40
# File 'lib/samovar/options.rb', line 38

def title
  @title
end

Class Method Details

.parse(*arguments, **options, &block) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/samovar/options.rb', line 10

def self.parse(*arguments, **options, &block)
  options = self.new(*arguments, **options)
  
  options.instance_eval(&block) if block_given?
  
  return options.freeze
end

Instance Method Details

#<<(option) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/samovar/options.rb', line 74

def << option
  @ordered << option
  option.flags.each do |flag|
    @keyed[flag.prefix] = option
    
    flag.alternatives.each do |alternative|
      @keyed[alternative] = option
    end
  end
  
  if default = option.default
    @defaults[option.key] = option.default
  end
end

#each(&block) ⇒ Object



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

def each(&block)
  @ordered.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/samovar/options.rb', line 60

def empty?
  @ordered.empty?
end

#freezeObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/samovar/options.rb', line 44

def freeze
  return self if frozen?
  
  @ordered.freeze
  @keyed.freeze
  @defaults.freeze
  
  @ordered.each(&:freeze)
  
  super
end

#initialize_dup(source) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/samovar/options.rb', line 30

def initialize_dup(source)
  super
  
  @ordered = @ordered.dup
  @keyed = @keyed.dup
  @defaults = @defaults.dup
end

#merge!(options) ⇒ Object



68
69
70
71
72
# File 'lib/samovar/options.rb', line 68

def merge!(options)
  options.each do |option|
    self << option
  end
end

#option(*arguments, **options, &block) ⇒ Object



64
65
66
# File 'lib/samovar/options.rb', line 64

def option(*arguments, **options, &block)
  self << Option.new(*arguments, **options, &block)
end

#parse(input, parent = nil, default = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/samovar/options.rb', line 89

def parse(input, parent = nil, default = nil)
  values = (default || @defaults).dup
  
  while option = @keyed[input.first]
    prefix = input.first
    result = option.parse(input)
    if result != nil
      values[option.key] = result
    end
  end
  
  return values
end

#to_sObject



103
104
105
# File 'lib/samovar/options.rb', line 103

def to_s
  @ordered.collect(&:to_s).join(' ')
end

#usage(rows) ⇒ Object



107
108
109
110
111
# File 'lib/samovar/options.rb', line 107

def usage(rows)
  @ordered.each do |option|
    rows << option
  end
end