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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/shale/builder.rb', line 159

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

  builder_methods_module.class_eval "    def \#{new_name}\n      \#{old_name}\n    end\n\n    def \#{new_name}=(val)\n      self.\#{old_name} = val\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1

#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
# 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 "      def \#{name}                           # def clients\n        return super unless block_given?    #   return super unless block_given?\n                                            #\n        arr = self.\#{name} ||= []           #   arr = self.clients ||= []\n        object = \#{shale_mapper}.new                #   object = Client.new\n        yield(object)                       #   yield(object)\n        arr << object                       #   arr << object\n        object                              #   object\n      end                                   # end\n    RUBY\n    return\n  end\n\n  @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1\n    def \#{name}                                   # def amount\n      return super unless block_given?            #   return super unless block_given?\n                                                  #\n      object = \#{shale_mapper}.new                        #   object = Amount.new\n      yield(object)                               #   yield(object)\n      self.\#{name} = object                       #   self.amount = object\n    end                                           # end\n  RUBY\nend\n", __FILE__, __LINE__ + 1

#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]



196
197
198
# File 'lib/shale/builder.rb', line 196

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]



204
205
206
207
208
# File 'lib/shale/builder.rb', line 204

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]



178
179
180
# File 'lib/shale/builder.rb', line 178

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]



186
187
188
189
190
# File 'lib/shale/builder.rb', line 186

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