Class: Strict::Attributes::Dsl

Inherits:
BasicObject
Includes:
Dsl::Coercible, Dsl::Validatable
Defined in:
lib/strict/attributes/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

#initialize(attributes:) ⇒ Dsl

Returns a new instance of Dsl.



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

def initialize(attributes:)
  @__strict_dsl_internal_attributes = attributes.to_h { |attribute| [attribute.name, attribute] }
  @__strict_dsl_internal_inherited_attributes = @__strict_dsl_internal_attributes.dup
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



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

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

Instance Attribute Details

#__strict_dsl_internal_attributesObject (readonly)

Returns the value of attribute __strict_dsl_internal_attributes.



17
18
19
# File 'lib/strict/attributes/dsl.rb', line 17

def __strict_dsl_internal_attributes
  @__strict_dsl_internal_attributes
end

Class Method Details

.run(attributes: []) ⇒ Object



7
8
9
10
11
# File 'lib/strict/attributes/dsl.rb', line 7

def run(attributes: [], &)
  dsl = new(attributes: attributes)
  dsl.instance_eval(&)
  ::Strict::Attributes::Configuration.new(attributes: dsl.__strict_dsl_internal_attributes.values)
end

Instance Method Details

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

Returns:



45
46
47
48
# File 'lib/strict/attributes/dsl.rb', line 45

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

#strict_attributeObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/strict/attributes/dsl.rb', line 24

def strict_attribute(*, **)
  attribute = ::Strict::Attribute.make(*, **)
  name = attribute.name
  existing_attribute = __strict_dsl_internal_attributes[name]
  inherited_attribute = @__strict_dsl_internal_inherited_attributes[name]
  if existing_attribute && !existing_attribute.equal?(inherited_attribute)
    ::Kernel.raise ::ArgumentError, "Attribute #{attribute.name.inspect} is already declared"
  end

  __strict_dsl_internal_attributes[name] = attribute
  nil
end