Class: Opto::Group

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/opto/group.rb

Overview

A group of Opto::Option instances. Members of Groups can see their relatives and their values. Such as ‘option.value_of(’another_option’)‘

Most Array instance methods are delegated, such as .map, .each, .find etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Opto::Group

Initialize a new Option Group. You can also pass in :defaults.

Parameters:

  • opts (Array<Hash,Opto::Option>, Hash, NilClass)

    An array of Option definition hashes or Option objects or a hash like { var_name: { opts } }.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/opto/group.rb', line 22

def initialize(*options)
  if options.size > 0
    if options.last.kind_of?(Hash) && options.last[:defaults]
      @defaults = options.pop[:defaults]
    end
    @options =
      case options.first
      when NilClass
        []
      when Hash
        options.first.map {|k,v| Option.new({name: k.to_s, group: self}.merge(v))}
      when ::Array
        options.first.map {|opt| opt.kind_of?(Opto::Option) ? opt : Option.new(opt.merge(group: self)) }
      else
        raise TypeError, "Invalid type #{options.first.class} for Opto::Group.new"
      end
  else
    @options = []
  end

end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



14
15
16
# File 'lib/opto/group.rb', line 14

def defaults
  @defaults
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/opto/group.rb', line 14

def options
  @options
end

Instance Method Details

#all_true?(conditions) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/opto/group.rb', line 110

def all_true?(conditions)
  normalize_ifs(conditions).all? { |s| s.call(self) == true }
end

#any_true?(conditions) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/opto/group.rb', line 106

def any_true?(conditions)
  normalize_ifs(conditions).any? { |s| s.call(self) == true }
end

#build_option(args = {}) ⇒ Opto::Option

Initialize a new Option to this group. Takes the same arguments as Opto::Option

Parameters:

  • option_definition (Hash)

Returns:



86
87
88
89
# File 'lib/opto/group.rb', line 86

def build_option(args={})
  options << Option.new(args.merge(group: self))
  options.last
end

#errorsHash

Collect validation errors from members

Returns:

  • (Hash)

    { option_name => { validator_name => “Too short” } }



52
53
54
# File 'lib/opto/group.rb', line 52

def errors
  Hash[*options_with_errors.flat_map {|o| [o.name, o.errors] }]
end

#normalize_ifs(ifs) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/opto/group.rb', line 114

def normalize_ifs(ifs)
  case ifs
  when NilClass
    []
  when ::Array
    ifs.map do |iff|
      lambda { |grp| grp.option(iff).true?  }
    end
  when Hash
    ifs.each_with_object([]) do |(k, v), arr|
      if v.kind_of?(Hash)
        if v.has_key?(:lt)
          arr << lambda { |grp| grp.value_of(k.to_s) < v[:lt] }
        end

        if v.has_key?(:lte)
          arr << lambda { |grp| grp.value_of(k.to_s) <= v[:lte] }
        end

        if v.has_key?(:gt)
          arr << lambda { |grp| grp.value_of(k.to_s) > v[:gt] }
        end

        if v.has_key?(:gte)
          arr << lambda { |grp| grp.value_of(k.to_s) > v[:gte] }
        end

        if v.has_key?(:eq)
          arr << lambda { |grp| grp.value_of(k.to_s) == v[:eq] }
        end

        if v.has_key?(:ne)
          arr << lambda { |grp| grp.value_of(k.to_s) != v[:ne] }
        end

        if v.has_key?(:start_with)
          arr << lambda { |grp| grp.value_of(k.to_s).to_s.start_with?(v[:start_with]) }
        end

        if v.has_key?(:end_with)
          arr << lambda { |grp| grp.value_of(k.to_s).to_s.end_with?(v[:end_with]) }
        end

        if v.has_key?(:contain)
          arr << lambda { |grp| grp.value_of(k.to_s).kind_of?(::Array) ? grp.value_of(k.to_s).include?(v[:contain]) : grp.value_of(k.to_s).to_s.include?(v[:contain]) }
        end

        if v.has_key?(:any_of)
          arr << lambda do |grp|
            if v[:any_of].kind_of?(String)
              arr = v[:any_of].split(",")
            elsif v[:any_of].kind_of?(::Array)
              arr = v[:any_of]
            else
              raise TypeError, "Invalid list for 'any_of'. Expected: Array or a comma separated string"
            end
            arr.include?(grp.value_of(k.to_s))
          end
        end
      else
        arr << lambda { |grp| grp.value_of(k.to_s) == v }
      end
    end
  when String, Symbol
    normalize_ifs([ifs])
  else
    raise TypeError, "Invalid syntax for conditional"
  end
end

#option(option_name) ⇒ Opto::Option

Find a member by name @param  option_name

Returns:



94
95
96
# File 'lib/opto/group.rb', line 94

def option(option_name)
  options.find { |opt| opt.name == option_name }
end

#options_with_errorsArray

Enumerate over all the options that are not valid

Returns:

  • (Array)


58
59
60
# File 'lib/opto/group.rb', line 58

def options_with_errors
  options.reject(&:valid?)
end

#runObject

Runs outputters for all valid non-skipped options



79
80
81
# File 'lib/opto/group.rb', line 79

def run
  options.reject(&:skip?).select(&:valid?).each(&:output)
end

#to_a(with_errors: false, with_value: false) ⇒ Array<Hash>

Convert Group to an Array of Hashes (by calling .to_h on each member)

Returns:

  • (Array<Hash>)


64
65
66
# File 'lib/opto/group.rb', line 64

def to_a(with_errors: false, with_value: false)
  options.map {|opt| opt.to_h(with_errors: with_errors, with_value: with_value) }
end

#to_h(values_only: false, with_values: false, with_errors: false) ⇒ Hash

Convert a Group to a hash that has { option_name => option_value }

Returns:

  • (Hash)


70
71
72
73
74
75
76
# File 'lib/opto/group.rb', line 70

def to_h(values_only: false, with_values: false, with_errors: false)
  if values_only
    Hash[*options.flat_map {|opt| [opt.name, opt.value]}]
  else
    Hash[*options.flat_map {|opt| [opt.name, opt.to_h(with_value: with_values, with_errors: with_errors).reject {|k,_| k==:name}]}]
  end
end

#valid?Boolean

Are all options valid? (Option value passes validation)

Returns:

  • (Boolean)


46
47
48
# File 'lib/opto/group.rb', line 46

def valid?
  options.all? {|o| o.valid? }
end

#value_of(option_name) ⇒ option_value, NilClass

Get a value of a member by option name

Parameters:

  • option_name (String)

Returns:

  • (option_value, NilClass)


101
102
103
104
# File 'lib/opto/group.rb', line 101

def value_of(option_name)
  opt = option(option_name)
  opt.nil? ? nil : opt.value
end