class Yorinter::Engine

Public Class Methods

new(document, params = {}) click to toggle source
# File lib/yorinter/engine.rb, line 3
def initialize document, params = {}, options = {}
  @document = document
  @params = params
  @engine = build_engine
  @options = options
end
register_engine(input, output) click to toggle source
# File lib/yorinter/engine.rb, line 115
def register_engine input, output
  @@engines ||= {}
  @@engines[[input, output]] = self
end
search_engine(input, output) click to toggle source
# File lib/yorinter/engine.rb, line 111
def search_engine input, output
  (@@engines || {})[[input, output]]
end

Public Instance Methods

binding_object() click to toggle source
# File lib/yorinter/engine.rb, line 40
def binding_object
  return @binding_object if @binding_object

  # Use document binding_object if it already has
  if @document.respond_to?(:binding_object) and @document.binding_object
    return @binding_object = @document.binding_object
  end

  this = self
  helper = @options[:helper]

  helper_wrapper = Object.new
  helper_wrapper.instance_eval @document.helper.to_s

  @binding_object = Object.new.instance_eval do
    @model  = this.model_binding_object
    @params = this.params_binding_object

    # Helper from params
    extend helper if helper

    # Helper from document
    helper_wrapper.singleton_class.constants.each do |const|
      extend helper_wrapper.singleton_class.const_get const
    end

    def get_binding
      binding
    end

    self
  end
end
build_engine() click to toggle source

Abstract methods

# File lib/yorinter/engine.rb, line 102
def build_engine
  raise 'Abstract method'
end
document_model() click to toggle source
# File lib/yorinter/engine.rb, line 10
def document_model
  @document.model || {}
end
document_options() click to toggle source
# File lib/yorinter/engine.rb, line 74
def document_options
  return @document_options if @document_options

  @document_options = (@document.options || {}).deep_symbolize_keys

  # Render title
  if @document_options[:title]
    @document_options[:title] = render_partial @document_options[:title]
  end

  # Render footer content
  if footer = @document_options[:footer]
    if footer[:content]
      footer[:content] = render_partial footer[:content]
    end
  end

  @document_options
end
endpoint_model() click to toggle source

Get model from endpoint

# File lib/yorinter/engine.rb, line 23
def endpoint_model
  if endpoint = model_endpoint
    JSON.parse Net::HTTP.get(URI(endpoint))
  else
    {}
  end
end
get_binding() click to toggle source
# File lib/yorinter/engine.rb, line 66
def get_binding
  binding
end
model_binding_object() click to toggle source
# File lib/yorinter/engine.rb, line 31
def model_binding_object
  model = document_model.deep_merge(endpoint_model)
  JSON.parse(model.to_json, object_class: OpenStruct)
end
model_endpoint() click to toggle source
# File lib/yorinter/engine.rb, line 14
def model_endpoint
  if @document.model_endpoint
    PrintableDocument.new(
      template: @document.model_endpoint
    ).render ErbEngine, @params
  end
end
params_binding_object() click to toggle source
# File lib/yorinter/engine.rb, line 36
def params_binding_object
  JSON.parse((@params || {}).to_json, object_class: OpenStruct)
end
render() click to toggle source
# File lib/yorinter/engine.rb, line 106
def render
  raise 'Abstract method'
end
render_partial(template) click to toggle source
# File lib/yorinter/engine.rb, line 94
def render_partial template
  PrintableDocument.new(
    template: template,
    binding_object: binding_object
  ).render ErbEngine, @params
end