Module: Seatbelt::Synthesizer

Included in:
Seatbelt::Synthesizers::Document, Seatbelt::Synthesizers::Mongoid
Defined in:
lib/seatbelt/synthesizers/synthesizer.rb

Overview

Public: The Synthesizer base module which provides attribute based synthesizing between a proxy and an implementation object.

To create a Synthesizer include this module in a plain Ruby class and implement the #synthesizable_attributes method.

Example

class BookSynthesizer
  include Seatbelt::Synthesizer

  def synthesizable_attributes
    [:title, :publisher]
  end
end

class Book
  include Seatbelt:Document
  include Seatbelt::Ghost

  attribute :title,     String
  attribute :publisher, String
  attribute :author,    String

  api_method :sell
end

class ImplementationBook < ActiveRecord::Base
  include Seatbelt::Gate

  synthesize :from    => "Book",
             :adapter => "BookSynthesizer"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#synthesizable_objectObject (readonly)

Returns the value of attribute synthesizable_object.



39
40
41
# File 'lib/seatbelt/synthesizers/synthesizer.rb', line 39

def synthesizable_object
  @synthesizable_object
end

Instance Method Details

#initialize(from_klass, synthesizable_object) ⇒ Object

Public: Initializes the Synthesizer.

from_klass - The API Class synthesizable_object - The implementation class instance



45
46
47
48
# File 'lib/seatbelt/synthesizers/synthesizer.rb', line 45

def initialize(from_klass, synthesizable_object)
  @klass                = from_klass
  @synthesizable_object = synthesizable_object
end

#synthesizeObject

Public: Defines the attribute based synthesize mechanism by redefining the getter and setter methods of the implementation class instance.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/seatbelt/synthesizers/synthesizer.rb', line 53

def synthesize
  unless self.respond_to?(:synthesizable_attributes)
    raise Seatbelt::Errors::SynthesizeableAttributesNotImplementedError
  end
  if @synthesizable_object.class.respond_to?(:synthesize_map)
    if @synthesizable_object.class.synthesize_map.empty?
      __synthesize_without_map
    else
      __synthesize_with_map
    end
  else
    __synthesize_without_map
  end
end