Class: Lookbook::Param
- Inherits:
-
Object
- Object
- Lookbook::Param
- Defined in:
- lib/lookbook/param.rb
Instance Attribute Summary collapse
- #description ⇒ Object readonly
- #name ⇒ Object readonly
- #options ⇒ Object readonly
- #value_default ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
- #cast_value ⇒ Object
- #hint ⇒ Object
-
#initialize(name:, input: nil, description: nil, value_type: nil, value_default: nil, value: nil, options: {}) ⇒ Param
constructor
A new instance of Param.
- #input ⇒ Object
- #input_options ⇒ Object
- #input_partial ⇒ Object
- #label ⇒ Object
- #value ⇒ Object
- #value_type ⇒ Object
Constructor Details
#initialize(name:, input: nil, description: nil, value_type: nil, value_default: nil, value: nil, options: {}) ⇒ Param
Returns a new instance of Param.
5 6 7 8 9 10 11 12 13 |
# File 'lib/lookbook/param.rb', line 5 def initialize(name:, input: nil, description: nil, value_type: nil, value_default: nil, value: nil, options: {}) @name = name @input = input @description = description @value_type = value_type @value_default = value_default @value = value @options = end |
Instance Attribute Details
#description ⇒ Object (readonly)
3 4 5 |
# File 'lib/lookbook/param.rb', line 3 def description @description end |
#name ⇒ Object (readonly)
3 4 5 |
# File 'lib/lookbook/param.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
3 4 5 |
# File 'lib/lookbook/param.rb', line 3 def @options end |
#value_default ⇒ Object (readonly)
3 4 5 |
# File 'lib/lookbook/param.rb', line 3 def value_default @value_default end |
Class Method Details
.from_tag(tag, value: nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/lookbook/param.rb', line 58 def self.from_tag(tag, value: nil) new( name: tag.name, input: tag.input || tag..input, description: tag.description || tag..description, value_type: tag.value_type || tag..value_type, value_default: tag.value_default, options: tag., value: value ) end |
Instance Method Details
#cast_value ⇒ Object
52 53 54 55 56 |
# File 'lib/lookbook/param.rb', line 52 def cast_value raise ArgumentError.new("Cannot cast param '#{name}' without a value set") if value.nil? StringValueCaster.call(value, value_type) end |
#hint ⇒ Object
19 20 21 |
# File 'lib/lookbook/param.rb', line 19 def hint .hint end |
#input ⇒ Object
23 24 25 |
# File 'lib/lookbook/param.rb', line 23 def input @input || guess_input end |
#input_options ⇒ Object
41 42 43 44 45 46 |
# File 'lib/lookbook/param.rb', line 41 def return @_input_options if @_input_options = .except([*methods, :name, :value_default, :description]) @_input_options ||= Store.new(input_config..merge()) end |
#input_partial ⇒ Object
48 49 50 |
# File 'lib/lookbook/param.rb', line 48 def input_partial input_config.partial end |
#label ⇒ Object
15 16 17 |
# File 'lib/lookbook/param.rb', line 15 def label .label || name.titleize end |
#value ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/lookbook/param.rb', line 27 def value val = @value || value_default if value_type == "datetime" formatter = (input == "datetime-local") ? "%Y-%m-%dT%T" : "%Y-%m-%d" StringValueCaster.call(val, "datetime")&.strftime(formatter) else val end end |
#value_type ⇒ Object
37 38 39 |
# File 'lib/lookbook/param.rb', line 37 def value_type @value_type || guess_value_type end |