Module: Dieses::Application::Sheet::DSL

Included in:
Dieses::Application::Sheet
Defined in:
lib/dieses/application/sheet.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/dieses/application/sheet.rb', line 30

def self.extended(klass)
  super

  klass.extend Support::ClassAttribute

  klass.define :proto
  klass.define :variants, default: {}
  klass.define :param,    default: Param.new
  klass.define :style,    default: Param.new
end

Instance Method Details

#sheet(name, desc, default: Undefined) ⇒ Object



10
11
12
# File 'lib/dieses/application/sheet.rb', line 10

def sheet(name, desc, default: Undefined)
  Sheets.register name, klass: self, desc: desc, default: default
end

#variant(key, desc, **kwargs) ⇒ Object



14
15
16
# File 'lib/dieses/application/sheet.rb', line 14

def variant(key, desc, **kwargs)
  variants key.to_sym => Proto.new(name: key, desc: desc, param: Param.new(**kwargs))
end

#variate(**kwargs, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dieses/application/sheet.rb', line 18

def variate(**kwargs, &block)
  kwargs.transform_values! { |value| [*value] }

  builder = proc { |keys, values| Proto.(**keys.zip(values).to_h) }
  variants = [nil].product(*kwargs.values).map { |_, *values| builder.call(kwargs.keys, values) }
  variants.each do |variant|
    variant.instance_exec(&block)
    self.variant variant.name, variant.desc, **variant.to_h
  end
  variants
end