Class: Yorinter::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/yorinter/engine.rb

Direct Known Subclasses

ErbEngine, HamlEngine, PdfEngine

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, params = {}, options = {}) ⇒ Engine

Returns a new instance of Engine.



3
4
5
6
7
8
# File 'lib/yorinter/engine.rb', line 3

def initialize document, params = {}, options = {}
  @document = document
  @params = params
  @engine = build_engine
  @options = options
end

Class Method Details

.register_engine(input, output) ⇒ Object



115
116
117
118
# File 'lib/yorinter/engine.rb', line 115

def register_engine input, output
  @@engines ||= {}
  @@engines[[input, output]] = self
end

.search_engine(input, output) ⇒ Object



111
112
113
# File 'lib/yorinter/engine.rb', line 111

def search_engine input, output
  (@@engines || {})[[input, output]]
end

Instance Method Details

#binding_objectObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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_engineObject

Abstract methods



102
103
104
# File 'lib/yorinter/engine.rb', line 102

def build_engine
  raise 'Abstract method'
end

#document_modelObject



10
11
12
# File 'lib/yorinter/engine.rb', line 10

def document_model
  @document.model || {}
end

#document_optionsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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_modelObject

Get model from endpoint



23
24
25
26
27
28
29
# File 'lib/yorinter/engine.rb', line 23

def endpoint_model
  if endpoint = model_endpoint
    JSON.parse Net::HTTP.get(URI(endpoint))
  else
    {}
  end
end

#model_binding_objectObject



31
32
33
34
# 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_endpointObject



14
15
16
17
18
19
20
# 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_objectObject



36
37
38
# File 'lib/yorinter/engine.rb', line 36

def params_binding_object
  JSON.parse((@params || {}).to_json, object_class: OpenStruct)
end

#renderObject



106
107
108
# File 'lib/yorinter/engine.rb', line 106

def render
  raise 'Abstract method'
end

#render_partial(template) ⇒ Object



94
95
96
97
98
99
# File 'lib/yorinter/engine.rb', line 94

def render_partial template
  PrintableDocument.new(
    template: template,
    binding_object: binding_object
  ).render ErbEngine, @params
end