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)

Returns the value of attribute builder_methods_module.



81
82
83
# File 'lib/shale/builder.rb', line 81

def builder_methods_module
  @builder_methods_module
end

Instance Method Details

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



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
# File 'lib/shale/builder.rb', line 108

def attribute(name, type, collection: false, default: nil, doc: nil, **kwargs, &block)
  super(name, type, collection:, default:, **kwargs, &block)
  attributes[name.to_sym]&.doc = doc # add doc to the attribute
  return unless type < ::Shale::Mapper

  if collection
    @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
      def #{name}                           # def clients
        return super unless block_given?    #   return super unless block_given?
                                            #
        arr = self.#{name} ||= []           #   arr = self.clients ||= []
        object = #{type}.new                #   object = Client.new
        yield(object)                       #   yield(object)
        arr << object                       #   arr << object
        object                              #   object
      end                                   # end
    RUBY
    return
  end

  @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
    def #{name}                                   # def amount
      return super unless block_given?            #   return super unless block_given?
                                                  #
      object = #{type}.new                        #   object = Amount.new
      yield(object)                               #   yield(object)
      self.#{name} = object                       #   self.amount = object
    end                                           # end
  RUBY
end

#attributesObject



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

def attributes; end

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

Yields:

  • (body)


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

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

  body
end

#inherited(subclass) ⇒ Object



73
74
75
76
# File 'lib/shale/builder.rb', line 73

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

#new(**props) ⇒ Object



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

def new(**props); end