Class: NitroKit::Choice
- Inherits:
-
Data
- Object
- Data
- NitroKit::Choice
- Defined in:
- app/components/nitro_kit/choice.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#disabled ⇒ Object
readonly
Returns the value of attribute disabled.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(label:, value:, disabled: false, id: nil, description: nil) ⇒ Choice
constructor
A new instance of Choice.
Constructor Details
#initialize(label:, value:, disabled: false, id: nil, description: nil) ⇒ Choice
Returns a new instance of Choice.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/components/nitro_kit/choice.rb', line 5 def initialize(label:, value:, disabled: false, id: nil, description: nil) unless disabled == true || disabled == false raise ArgumentError, "disabled must be true or false; received #{disabled.inspect}" end if label.nil? || label.to_s.strip.empty? raise ArgumentError, "choice label cannot be blank" end if value.nil? raise ArgumentError, "choice value cannot be nil" end if id && (!id.is_a?(String) || id.strip.empty?) raise ArgumentError, "choice id must be a non-blank String" end if description && (!description.is_a?(String) || description.strip.empty?) raise ArgumentError, "choice description must be a non-blank String" end super(label:, value:, disabled:, id:, description:) end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description
4 5 6 |
# File 'app/components/nitro_kit/choice.rb', line 4 def description @description end |
#disabled ⇒ Object (readonly)
Returns the value of attribute disabled
4 5 6 |
# File 'app/components/nitro_kit/choice.rb', line 4 def disabled @disabled end |
#id ⇒ Object (readonly)
Returns the value of attribute id
4 5 6 |
# File 'app/components/nitro_kit/choice.rb', line 4 def id @id end |
#label ⇒ Object (readonly)
Returns the value of attribute label
4 5 6 |
# File 'app/components/nitro_kit/choice.rb', line 4 def label @label end |
#value ⇒ Object (readonly)
Returns the value of attribute value
4 5 6 |
# File 'app/components/nitro_kit/choice.rb', line 4 def value @value end |
Class Method Details
.coerce(choice) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/components/nitro_kit/choice.rb', line 25 def self.coerce(choice) case choice when self choice when Hash attributes = choice.symbolize_keys unknown_keys = attributes.keys - %i[label value disabled id description] if unknown_keys.any? raise ArgumentError, "unknown choice attributes: #{unknown_keys.join(", ")}" end new( label: attributes.fetch(:label), value: attributes.fetch(:value), disabled: attributes.fetch(:disabled, false), id: attributes[:id], description: attributes[:description] ) when Array unless choice.length.between?(1, 2) raise ArgumentError, "choice arrays hold a label and an optional value; " \ "pass a Hash to declare disabled, id, or description" end new(label: choice.fetch(0), value: choice.fetch(1, choice.fetch(0))) else new(label: choice, value: choice) end end |