class SimpleShipping::Abstract::Builder

Kind of an abstract class which should be used to create model builders. Model builder “knows” how to represent its model for a its service. This class provides only common skeleton for subclasses.

Public Class Methods

build(model, opts = {}) click to toggle source

Parameters:

model - kind of {Abstract::Model}
opts - hash of options. Every builder can have its own specific set of options

Returns:

Hash which can be used by Savon to build a part of SOAP request.
# File lib/simple_shipping/abstract/builder.rb, line 13
def self.build(model, opts = {})
  raise(ValidationError.new(model)) unless model.valid?

  builder = self.new(model, opts)
  builder.validate
  builder.build
end
new(model = nil, opts = {}) click to toggle source
# File lib/simple_shipping/abstract/builder.rb, line 32
def initialize(model = nil, opts = {})
  self.default_opts ||= {}
  @opts  = default_opts.merge(opts)
  @model = model
end
set_default_opts(opts = {}) click to toggle source

Allows to set default option values is subclasses.

# File lib/simple_shipping/abstract/builder.rb, line 22
def self.set_default_opts(opts = {})
  self.default_opts = opts
end

Public Instance Methods

build() click to toggle source

Should be implemented by subclasses. But by default returns empty hash.

# File lib/simple_shipping/abstract/builder.rb, line 27
def build; {}; end
validate() click to toggle source

Should be implemented by subclass if subclass needs to do some validation.

# File lib/simple_shipping/abstract/builder.rb, line 30
def validate; end

Private Instance Methods

validate_inclusion_of(option, enumeration) click to toggle source

Raises {ValidationError} if option has invalid value.

# File lib/simple_shipping/abstract/builder.rb, line 40
def validate_inclusion_of(option, enumeration)
  unless enumeration.has_key?(@opts[option])
    raise ValidationError.new("#{option} has an unavailable value(#{@opts[option]}). Available values are #{enumeration.keys.inspect}") 
  end
end