Class: Uniword::Builder::BaseBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/builder/base_builder.rb

Overview

Base class for builders that wrap a single model object.

Provides the shared initialize/from_model/build pattern used by most builders in the module. Subclasses must implement .default_model_class to specify the model type to create when none is provided.

Examples:

Subclass with defaults

class MyBuilder < BaseBuilder
  def self.default_model_class
    Wordprocessingml::MyModel
  end
end

builder = MyBuilder.new           # creates new MyModel
builder = MyBuilder.new(existing) # wraps existing model
builder = MyBuilder.from_model(m) # same as new(m)
model = builder.build             # returns @model

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model = nil) ⇒ BaseBuilder

Returns a new instance of BaseBuilder.



26
27
28
# File 'lib/uniword/builder/base_builder.rb', line 26

def initialize(model = nil)
  @model = model || self.class.default_model_class.new
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



24
25
26
# File 'lib/uniword/builder/base_builder.rb', line 24

def model
  @model
end

Class Method Details

.default_model_classObject

Raises:

  • (NotImplementedError)


38
39
40
41
# File 'lib/uniword/builder/base_builder.rb', line 38

def self.default_model_class
  raise NotImplementedError,
        "#{name} must implement .default_model_class"
end

.from_model(model) ⇒ Object



30
31
32
# File 'lib/uniword/builder/base_builder.rb', line 30

def self.from_model(model)
  new(model)
end

Instance Method Details

#buildObject



34
35
36
# File 'lib/uniword/builder/base_builder.rb', line 34

def build
  @model
end