Class: Lookbook::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/lookbook/param.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = options
end

Instance Attribute Details

#descriptionObject (readonly)



3
4
5
# File 'lib/lookbook/param.rb', line 3

def description
  @description
end

#nameObject (readonly)



3
4
5
# File 'lib/lookbook/param.rb', line 3

def name
  @name
end

#optionsObject (readonly)



3
4
5
# File 'lib/lookbook/param.rb', line 3

def options
  @options
end

#value_defaultObject (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.options.input,
    description: tag.description || tag.options.description,
    value_type: tag.value_type || tag.options.value_type,
    value_default: tag.value_default,
    options: tag.options,
    value: value
  )
end

Instance Method Details

#cast_valueObject

Raises:

  • (ArgumentError)


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

#hintObject



19
20
21
# File 'lib/lookbook/param.rb', line 19

def hint
  options.hint
end

#inputObject



23
24
25
# File 'lib/lookbook/param.rb', line 23

def input
  @input || guess_input
end

#input_optionsObject



41
42
43
44
45
46
# File 'lib/lookbook/param.rb', line 41

def input_options
  return @_input_options if @_input_options

  runtime_options = options.except([*methods, :name, :value_default, :description])
  @_input_options ||= Store.new(input_config.options.merge(runtime_options))
end

#input_partialObject



48
49
50
# File 'lib/lookbook/param.rb', line 48

def input_partial
  input_config.partial
end

#labelObject



15
16
17
# File 'lib/lookbook/param.rb', line 15

def label
  options.label || name.titleize
end

#valueObject



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_typeObject



37
38
39
# File 'lib/lookbook/param.rb', line 37

def value_type
  @value_type || guess_value_type
end