Class: Foobara::TypeDeclarations::Dsl::Attributes

Inherits:
BasicObject
Defined in:
foobara-0.0.110/projects/type_declarations/src/dsl/attributes.rb

Overview

NOTE: when debugging stuff, it’s helpful to comment out the inheritance from BasicObject

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(attribute_name, *processor_symbols, **declaration, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'foobara-0.0.110/projects/type_declarations/src/dsl/attributes.rb', line 53

def method_missing(attribute_name, *processor_symbols, **declaration, &block)
  unless respond_to_missing?(attribute_name)
    # :nocov:
    return super
    # :nocov:
  end

  _disable_method_missing do
    declaration = declaration.dup

    if block
      # TODO: technically, current namespace might have an overridden :array type so instead we
      # should lookup :array he to see if we get BuiltinTypes[:array] or not (or "::array" or not)
      if processor_symbols.first == :array
        type, *processor_symbols = processor_symbols
      end
    else
      type, *processor_symbols = processor_symbols

      unless type
        if processor_symbols.empty? && !declaration.empty?
          type = :attributes
          declaration = { element_type_declarations: declaration }
        else
          ::Kernel.raise NoTypeGivenError, "Expected a type but attribute #{attribute_name} was declared " \
                                           "without a type. (Perhaps you didn't mean for #{attribute_name} " \
                                           "to be an attribute?)"
        end
      end
    end

    processor_symbols.each do |processor_symbol|
      case processor_symbol
      when ::String
        description = processor_symbol

        if declaration.key?(:description)
          # :nocov:
          ::Kernel.raise ArgumentError, "Expected only one description but " \
                                        "got #{description.inspect} and #{declaration[:description].inspect}"
          # :nocov:
        end

        declaration[:description] = description
      when ::Symbol
        declaration[processor_symbol] = true
      else
        # :nocov:
        ::Kernel.raise ArgumentError, "expected a Symbol, got #{processor_symbol.inspect}"
        # :nocov:
      end
    end

    if declaration.delete(:required)
      _add_to_required(attribute_name)
    end

    if declaration.key?(:default)
      default = declaration.delete(:default)

      _add_to_defaults(attribute_name, default)
    end

    if declaration.empty? && !block
      _add_attribute(attribute_name, type)
    else
      declaration = if block
                      attributes_declaration = Attributes.to_declaration(&block).merge(declaration)

                      if type == :array
                        {
                          type: :array,
                          element_type_declaration: attributes_declaration
                        }
                      else
                        attributes_declaration
                      end
                    else
                      declaration.merge(type:)
                    end

      _add_attribute(attribute_name, declaration)
    end

    _type_declaration

    AttributeCreated.new(attribute_name)
  end
end

Instance Attribute Details

#_method_missing_disabledObject

Returns the value of attribute _method_missing_disabled.



46
47
48
# File 'foobara-0.0.110/projects/type_declarations/src/dsl/attributes.rb', line 46

def _method_missing_disabled
  @_method_missing_disabled
end

Class Method Details

.to_declarationObject



41
42
43
# File 'foobara-0.0.110/projects/type_declarations/src/dsl/attributes.rb', line 41

def to_declaration(&)
  new._to_declaration(&)
end

Instance Method Details

#_to_declarationObject



48
49
50
51
# File 'foobara-0.0.110/projects/type_declarations/src/dsl/attributes.rb', line 48

def _to_declaration(&)
  instance_eval(&)
  _type_declaration
end

#respond_to_missing?(method_name, private = false) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'foobara-0.0.110/projects/type_declarations/src/dsl/attributes.rb', line 143

def respond_to_missing?(method_name, private = false)
  !_method_missing_disabled || super
end