Module: Shale::Builder::ClassMethods

Extended by:
T::Generic, T::Sig
Defined in:
lib/shale/builder.rb

Overview

Class methods provided by ‘Shale::Builder`

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#builder_methods_moduleObject (readonly)

Contains overridden getter methods for attributes with complex types (so that they accept a block for building) : Module



86
87
88
# File 'lib/shale/builder.rb', line 86

def builder_methods_module
  @builder_methods_module
end

Instance Method Details

#alias_attribute(new_name, old_name) ⇒ Object

Create an alias for the getter and setter of an attribute. : (Symbol new_name, Symbol old_name) -> void



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/shale/builder.rb', line 165

def alias_attribute(new_name, old_name)
  attr = attributes.fetch(old_name)
  (attr.aliases ||= []) << new_name

  builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
    def #{new_name}
      #{old_name}
    end

    def #{new_name}=(val)
      self.#{old_name} = val
    end
  RUBY
end

#attribute(name, shale_mapper, collection: false, default: nil, doc: nil, return_type: nil, setter_type: nil, **kwargs, &block) ⇒ Object

: ( | String | Symbol name, | Class shale_mapper, | ?collection: bool, | ?default: Proc?, | ?doc: String?, | ?return_type: untyped, | ?setter_type: untyped, | **Object kwargs | ) ?{ -> void } -> void



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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/shale/builder.rb', line 112

def attribute(
  name,
  shale_mapper,
  collection: false,
  default: nil,
  doc: nil,
  return_type: nil,
  setter_type: nil,
  **kwargs,
  &block
)
  super(name, shale_mapper, collection:, default:, **kwargs, &block)
  if (attr_def = attributes[name.to_sym])
    attr_def.doc = doc
    attr_def.return_type = return_type
    attr_def.setter_type = setter_type
  end
  return unless shale_mapper < ::Shale::Mapper

  if collection
    @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
      def #{name}
        return super unless block_given?

        arr = self.#{name} ||= []
        object = #{shale_mapper}.new
        yield(object)
        arr << object
        object
      end
    RUBY
    return
  end

  @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
    def #{name}
      return super unless block_given?

      object = #{shale_mapper}.new
      yield(object)
      self.#{name} = object
    end

    def memo_#{name}
      object = self.#{name} || #{shale_mapper}.new
      yield(object)
      self.#{name} = object
    end
  RUBY
end

#attributesObject



100
# File 'lib/shale/builder.rb', line 100

def attributes; end

#build {|body| ... } ⇒ Object

: { (instance arg0) -> void } -> instance

Yields:

  • (body)


89
90
91
92
93
94
# File 'lib/shale/builder.rb', line 89

def build(&_block)
  body = new
  yield(body)

  body
end

#builder_attributesObject

Returns a hash of shale attributes that are handled by a shale builder. The result gets memoized.

: -> Hash[Symbol, Shale::Attribute]



202
203
204
# File 'lib/shale/builder.rb', line 202

def builder_attributes
  @builder_attributes ||= builder_attributes!
end

#builder_attributes!Object

Returns a hash of shale attributes that are handled by a shale builder. Always constructs a new hash.

: -> Hash[Symbol, Shale::Attribute]



210
211
212
213
214
# File 'lib/shale/builder.rb', line 210

def builder_attributes!
  attributes.select do |_, attr|
    attr.builder?
  end
end

#inherited(subclass) ⇒ Object

: (Class subclass) -> void



78
79
80
81
# File 'lib/shale/builder.rb', line 78

def inherited(subclass)
  super
  Builder.prepare_mod(subclass)
end

#mapper_attributesObject

Returns a hash of shale attributes that are handled by a shale mapper. The result gets memoized.

: -> Hash[Symbol, Shale::Attribute]



184
185
186
# File 'lib/shale/builder.rb', line 184

def mapper_attributes
  @mapper_attributes ||= mapper_attributes!
end

#mapper_attributes!Object

Returns a hash of shale attributes that are handled by a shale mapper. Always constructs a new hash.

: -> Hash[Symbol, Shale::Attribute]



192
193
194
195
196
# File 'lib/shale/builder.rb', line 192

def mapper_attributes!
  attributes.select do |_, attr|
    attr.mapper?
  end
end

#new(**props) ⇒ Object



97
# File 'lib/shale/builder.rb', line 97

def new(**props); end