Class: Beni::DSL::DefinitionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/beni/dsl/definition_context.rb

Overview

Vocabulary inside a top-level toolchain <name> do … end block —the (version, sha256) pair and nothing else. Both fields are required exactly once; the check runs when the block returns.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ DefinitionContext

Returns a new instance of DefinitionContext.



17
18
19
20
21
# File 'lib/beni/dsl/definition_context.rb', line 17

def initialize(name)
  @name = name
  @version = nil
  @sha256 = nil
end

Class Method Details

.collect(name) ⇒ Object

Run block on a fresh context and return the collected ToolchainDefinition.



11
12
13
14
15
# File 'lib/beni/dsl/definition_context.rb', line 11

def self.collect(name, &)
  context = new(name)
  context.instance_exec(&)
  context.to_definition
end

Instance Method Details

#sha256(value) ⇒ Object

Raises:



29
30
31
32
33
# File 'lib/beni/dsl/definition_context.rb', line 29

def sha256(value)
  raise Error, "duplicate `sha256` in toolchain #{@name.inspect} definition" if @sha256

  @sha256 = value
end

#to_definitionObject

The collected definition; raises when the block left version or sha256 undeclared.



37
38
39
40
41
42
43
44
45
46
# File 'lib/beni/dsl/definition_context.rb', line 37

def to_definition
  version = @version
  sha256 = @sha256
  if version.nil? || sha256.nil?
    missing = [("version" if version.nil?), ("sha256" if sha256.nil?)].compact
    raise Error, "toolchain #{@name.inspect} definition missing #{missing.join(" and ")}"
  end

  ToolchainDefinition.new(name: @name, version: version, sha256: sha256)
end

#version(value) ⇒ Object

Raises:



23
24
25
26
27
# File 'lib/beni/dsl/definition_context.rb', line 23

def version(value)
  raise Error, "duplicate `version` in toolchain #{@name.inspect} definition" if @version

  @version = value
end