Class: Microstation::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/microstation/template.rb

Constant Summary collapse

EMPTY_ARRAY =
[].freeze
EMPTY_HASH =
{}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(template, output_dir: nil, app: nil, name: nil) ⇒ Template

Returns a new instance of Template.



16
17
18
# File 'lib/microstation/template.rb', line 16

def initialize(template, output_dir: nil, app: nil, name: nil)
  @changer = Changer.new(template, output_dir: output_dir, app: app, name: name)
end

Instance Method Details

#change_template_text_in_cells(drawing, locals = {}) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/microstation/template.rb', line 55

def change_template_text_in_cells(drawing, locals = {})
  drawing.scan_cells do |c|
    c.text_elements do |text|
      new_text = update_liquid_text(text, locals)
      text.replace new_text if new_text != text.to_s
    end
  end
end

#change_template_text_normal(drawing, locals = {}) ⇒ Object



48
49
50
51
52
53
# File 'lib/microstation/template.rb', line 48

def change_template_text_normal(drawing, locals = {})
  drawing.scan_text do |text|
    new_text = update_liquid_text(text, locals)
    text.replace new_text if new_text != text.to_s
  end
end

#normalize_hash(scope) ⇒ Object



74
75
76
77
78
# File 'lib/microstation/template.rb', line 74

def normalize_hash(scope)
  scope = scope.to_h if scope.respond_to?(:to_h)
  scope = scope.transform_keys(&:to_s) if scope.is_a? Hash
  scope
end

#render(name: nil, output_dir: nil, locals: EMPTY_HASH, tagsets: EMPTY_ARRAY) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/microstation/template.rb', line 24

def render(name: nil, output_dir: nil, locals: EMPTY_HASH, tagsets: EMPTY_ARRAY)
  return if locals == EMPTY_HASH && tagsets == EMPTY_ARRAY
  @changer.run(name: name, output_dir: output_dir) do |drawing|
    locals = normalize_hash(locals)
    update_text(drawing, locals)
    update_tagsets(drawing, tagsets)
  end
end

#run(update = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/microstation/template.rb', line 33

def run(update = {})
  @changer.run do |drawing|
    locals = normalize_hash(update)
    return if locals == {}

    update_text(drawing, locals)
    update_tagsets(drawing, tagsets)
  end
end

#templateObject



20
21
22
# File 'lib/microstation/template.rb', line 20

def template
  @changer.template
end

#update_liquid_text(text, locals) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/microstation/template.rb', line 64

def update_liquid_text(text, locals)
  update_hash = normalize_hash(locals)
  compiled = ::Liquid::Template.parse(text.to_s)
  new_text = begin
               compiled.render(update_hash)
             rescue StandardError
               text
             end
end

#update_tagsets(drawing, ts_arg) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/microstation/template.rb', line 80

def update_tagsets(drawing, ts_arg)
  return if ts_arg == []
  return if ts_arg == {}

  ts_arg = [ts_arg] if ts_arg.instance_of?(Hash)
  ts_arg.each do |hash_pair|
    drawing.update_tagset(hash_pair.keys[0], hash_pair.values[0])
  end
end

#update_text(drawing, locals = {}) ⇒ Object



43
44
45
46
# File 'lib/microstation/template.rb', line 43

def update_text(drawing, locals = {})
  change_template_text_normal(drawing, locals)
  change_template_text_in_cells(drawing, locals)
end