Class: Showcase::Options

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/showcase/options.rb

Direct Known Subclasses

Context

Defined Under Namespace

Classes: Context

Constant Summary collapse

DEFAULT_OMITTED =
Object.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context) ⇒ Options

Returns a new instance of Options.



6
7
8
9
10
# File 'lib/showcase/options.rb', line 6

def initialize(view_context)
  @view_context = view_context
  @options = []
  @order = [:name, :required, :type, :default, :description]
end

Instance Attribute Details

#contextsObject (readonly)

Showcase.options.define :stimulus do

  def value(name, ...)
    option("data-#{@controller}-#{name}-value", ...)
  end
end


18
19
20
# File 'lib/showcase/options.rb', line 18

def contexts
  @contexts
end

Class Method Details

.define(key, &block) ⇒ Object



21
22
23
# File 'lib/showcase/options.rb', line 21

def self.define(key, &block)
  contexts[key].class_eval(&block) # Lets users reopen an already defined context class.
end

Instance Method Details

#context(key, **options, &block) ⇒ Object

showcase.options.stimulus controller: :welcome do |o|

  o.value :greeting, default: "Hello"
end


28
29
30
31
# File 'lib/showcase/options.rb', line 28

def context(key, **options, &block)
  context = self.class.contexts.fetch(key)
  context.new(@view_context, @options, **options).tap { yield _1 if block_given? }
end

#each(&block) ⇒ Object



64
65
66
67
68
# File 'lib/showcase/options.rb', line 64

def each(&block)
  @options.each do |option|
    yield headers.index_with { option[_1] }
  end
end

#headersObject



60
61
62
# File 'lib/showcase/options.rb', line 60

def headers
  @headers ||= @order | @options.flat_map(&:keys).uniq.sort
end

#option(name, description = nil, required: false, type: nil, default: DEFAULT_OMITTED, **options, &block) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/showcase/options.rb', line 51

def option(name, description = nil, required: false, type: nil, default: DEFAULT_OMITTED, **options, &block)
  description ||= @view_context.capture(&block).remove(/^\s+/).html_safe if block

  type ||= type_from_default(default)
  default = default == DEFAULT_OMITTED ? nil : default.inspect

  @options << options.with_defaults(name: name, default: default, type: type, description: description, required: required)
end

#optional(*arguments, **keywords, &block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/showcase/options.rb', line 41

def optional(*arguments, **keywords, &block)
  if arguments.none?
    ActiveSupport::OptionMerger.new(self, required: false)
  else
    option(*arguments, **keywords, required: false, &block)
  end
end

#required(*arguments, **keywords, &block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/showcase/options.rb', line 33

def required(*arguments, **keywords, &block)
  if arguments.none?
    ActiveSupport::OptionMerger.new(self, required: true)
  else
    option(*arguments, **keywords, required: true, &block)
  end
end