Class: Strict::Methods::Dsl

Inherits:
BasicObject
Includes:
Dsl::Coercible, Dsl::Validatable
Defined in:
lib/strict/methods/dsl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dsl::Validatable

#AllOf, #AnyOf, #Anything, #ArrayOf, #Boolean, #HashOf, #RangeOf

Methods included from Dsl::Coercible

#ToArray, #ToHash

Constructor Details

#initializeDsl

Returns a new instance of Dsl.



22
23
24
25
26
# File 'lib/strict/methods/dsl.rb', line 22

def initialize
  @__strict_dsl_internal_parameters = {}
  @__strict_dsl_internal_returns = ::Strict::Return.make
  @__strict_dsl_internal_returns_declared = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/strict/methods/dsl.rb', line 46

def method_missing(name, *, **)
  if respond_to_missing?(name)
    strict_parameter(name, *, **)
  else
    super
  end
end

Instance Attribute Details

#__strict_dsl_internal_parametersObject (readonly)

Returns the value of attribute __strict_dsl_internal_parameters.



20
21
22
# File 'lib/strict/methods/dsl.rb', line 20

def __strict_dsl_internal_parameters
  @__strict_dsl_internal_parameters
end

#__strict_dsl_internal_returnsObject

Returns the value of attribute __strict_dsl_internal_returns.



20
21
22
# File 'lib/strict/methods/dsl.rb', line 20

def __strict_dsl_internal_returns
  @__strict_dsl_internal_returns
end

Class Method Details

.runObject



7
8
9
10
11
12
13
14
# File 'lib/strict/methods/dsl.rb', line 7

def run(&)
  dsl = new
  dsl.instance_eval(&)
  ::Strict::Methods::Configuration.new(
    parameters: dsl.__strict_dsl_internal_parameters.values,
    returns: dsl.__strict_dsl_internal_returns
  )
end

Instance Method Details

#respond_to_missing?(method_name, _include_private = nil) ⇒ Boolean

Returns:



54
55
56
57
# File 'lib/strict/methods/dsl.rb', line 54

def respond_to_missing?(method_name, _include_private = nil)
  first_letter = method_name.to_s.each_char.first
  first_letter.eql?(first_letter.downcase)
end

#returnsObject



28
29
30
31
32
33
34
# File 'lib/strict/methods/dsl.rb', line 28

def returns(*, **)
  ::Kernel.raise ::ArgumentError, "Return value is already declared" if @__strict_dsl_internal_returns_declared

  self.__strict_dsl_internal_returns = ::Strict::Return.make(*, **)
  @__strict_dsl_internal_returns_declared = true
  nil
end

#strict_parameterObject



36
37
38
39
40
41
42
43
44
# File 'lib/strict/methods/dsl.rb', line 36

def strict_parameter(*, **)
  parameter = ::Strict::Parameter.make(*, **)
  if __strict_dsl_internal_parameters.key?(parameter.name)
    ::Kernel.raise ::ArgumentError, "Parameter #{parameter.name.inspect} is already declared"
  end

  __strict_dsl_internal_parameters[parameter.name] = parameter
  nil
end