Class: Uchi::Field::Select
- Inherits:
-
Field
- Object
- Field
- Uchi::Field::Select
- Defined in:
- app/components/uchi/field/select.rb
Defined Under Namespace
Instance Method Summary collapse
-
#flat_options ⇒ Object
Resolves and memoizes the options as a flat value => label Hash, keyed by the string representation of each option's value so #label_for can look up a label in constant time.
-
#grouped? ⇒ Boolean
Returns true if the configured options are grouped, ie.
-
#initialize(name) ⇒ Select
constructor
A new instance of Select.
-
#label_for(value) ⇒ Object
Returns the label to display for the given value, or nil if the value doesn't match any of the configured options.
-
#options(options = Configuration::Unset) ⇒ self, Hash
Sets or gets the options to choose between.
Constructor Details
#initialize(name) ⇒ Select
Returns a new instance of Select.
61 62 63 64 |
# File 'app/components/uchi/field/select.rb', line 61 def initialize(name) super @options = {} end |
Instance Method Details
#flat_options ⇒ Object
Resolves and memoizes the options as a flat value => label Hash, keyed by the string representation of each option's value so #label_for can look up a label in constant time. Memoized so that a Proc given to #options is only called once per field instance - see #resolved_options for details. The memo is cleared whenever #options is called with a new value.
When the same value appears in more than one group, the first
matching label wins - matching how a browser's <select> element
only ever selects the first option with a matching value.
45 46 47 48 49 50 51 52 53 |
# File 'app/components/uchi/field/select.rb', line 45 def @flat_options ||= .each_with_object({}) { |(key, value), flat| if value.is_a?(Hash) value.each { |group_key, group_label| flat[group_key.to_s] = group_label unless flat.key?(group_key.to_s) } else flat[key.to_s] = value unless flat.key?(key.to_s) end } end |
#grouped? ⇒ Boolean
Returns true if the configured options are grouped, ie. a non-empty Hash whose values are all themselves Hashes of options.
57 58 59 |
# File 'app/components/uchi/field/select.rb', line 57 def grouped? .present? && .values.all? { |value| value.is_a?(Hash) } end |
#label_for(value) ⇒ Object
Returns the label to display for the given value, or nil if the value doesn't match any of the configured options.
Values are compared using their string representation, matching how
a <select> element compares its options' values against the
persisted attribute value.
72 73 74 75 76 |
# File 'app/components/uchi/field/select.rb', line 72 def label_for(value) return if value.nil? [value.to_s] end |
#options(options = Configuration::Unset) ⇒ self, Hash
Sets or gets the options to choose between.
When called with an argument, sets the options and returns self for chaining. When called without arguments, returns the current options, resolved to a Hash.
112 113 114 115 116 117 118 119 |
# File 'app/components/uchi/field/select.rb', line 112 def ( = Configuration::Unset) return if == Configuration::Unset @options = @resolved_options = nil @flat_options = nil self end |