Class: Opto::Group
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
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #all_true?(conditions) ⇒ Boolean
- #any_true?(conditions) ⇒ Boolean
-
#build_option(args = {}) ⇒ Opto::Option
Initialize a new Option to this group.
-
#errors ⇒ Hash
Collect validation errors from members.
-
#initialize(*options) ⇒ Opto::Group
constructor
Initialize a new Option Group.
- #normalize_ifs(ifs) ⇒ Object
-
#option(option_name) ⇒ Opto::Option
Find a member by name @param option_name.
-
#options_with_errors ⇒ Array
Enumerate over all the options that are not valid.
-
#run ⇒ Object
Runs outputters for all valid non-skipped options.
-
#to_a(with_errors: false, with_value: false) ⇒ Array<Hash>
Convert Group to an Array of Hashes (by calling .to_h on each member).
-
#to_h(values_only: false, with_values: false, with_errors: false) ⇒ Hash
Convert a Group to a hash that has { option_name => option_value }.
-
#valid? ⇒ Boolean
Are all options valid? (Option value passes validation).
-
#value_of(option_name) ⇒ option_value, NilClass
Get a value of a member by option name.
Constructor Details
#initialize(*options) ⇒ Opto::Group
Initialize a new Option Group. You can also pass in :defaults.
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(*) if .size > 0 if .last.kind_of?(Hash) && .last[:defaults] @defaults = .pop[:defaults] end @options = case .first when NilClass [] when Hash .first.map {|k,v| Option.new({name: k.to_s, group: self}.merge(v))} when ::Array .first.map {|opt| opt.kind_of?(Opto::Option) ? opt : Option.new(opt.merge(group: self)) } else raise TypeError, "Invalid type #{.first.class} for Opto::Group.new" end else @options = [] end end |
Instance Attribute Details
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
14 15 16 |
# File 'lib/opto/group.rb', line 14 def defaults @defaults end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/opto/group.rb', line 14 def @options end |
Instance Method Details
#all_true?(conditions) ⇒ 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
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
86 87 88 89 |
# File 'lib/opto/group.rb', line 86 def build_option(args={}) << Option.new(args.merge(group: self)) .last end |
#errors ⇒ Hash
Collect validation errors from members
52 53 54 |
# File 'lib/opto/group.rb', line 52 def errors Hash[*.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
94 95 96 |
# File 'lib/opto/group.rb', line 94 def option(option_name) .find { |opt| opt.name == option_name } end |
#options_with_errors ⇒ Array
Enumerate over all the options that are not valid
58 59 60 |
# File 'lib/opto/group.rb', line 58 def .reject(&:valid?) end |
#run ⇒ Object
Runs outputters for all valid non-skipped options
79 80 81 |
# File 'lib/opto/group.rb', line 79 def run .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)
64 65 66 |
# File 'lib/opto/group.rb', line 64 def to_a(with_errors: false, with_value: false) .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 }
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[*.flat_map {|opt| [opt.name, opt.value]}] else Hash[*.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)
46 47 48 |
# File 'lib/opto/group.rb', line 46 def valid? .all? {|o| o.valid? } end |
#value_of(option_name) ⇒ option_value, NilClass
Get a value of a member by option name
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 |