Module: Uber::Builder::ClassMethods

Defined in:
lib/uber/builder.rb

Instance Method Summary collapse

Instance Method Details

#builds(proc = nil, &block) ⇒ Object

Adds a builder to the cell class. Builders are used in #cell to find out the concrete class for rendering. This is helpful if you frequently want to render subclasses according to different circumstances (e.g. login situations) and you don’t want to place these deciders in your view code.

Passes the model and options from #cell into the block.

Multiple build blocks are ORed, if no builder matches the building cell is used.

Example:

Consider two different user box cells in your app.

class AuthorizedUserBox < UserInfoBox
end

class AdminUserBox < UserInfoBox
end

Now you don’t want to have deciders all over your views - use a declarative builder.

UserInfoBox.build do |model, options|
  AuthorizedUserBox if options[:is_signed_in]
  AdminUserBox if model.admin?
end

In your view #cell will instantiate the right class for you now.



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

def builds(proc=nil, &block)
  builders << Uber::Options::Value.new(proc.nil? ? block : proc) # TODO: provide that in Uber::O:Value.
end

#class_builder(context = nil) ⇒ Object

Call this from your classes’ own ::build method to compute the concrete target class. The class_builder is cached, you can’t change the context once it’s set.



92
93
94
# File 'lib/uber/builder.rb', line 92

def class_builder(context=nil)
  @class_builder ||= Constant.new(self, context)
end