Class: Microstation::Template
- Inherits:
-
Object
- Object
- Microstation::Template
- Defined in:
- lib/microstation/template.rb
Constant Summary collapse
- EMPTY_ARRAY =
[].freeze
- EMPTY_HASH =
{}.freeze
Instance Method Summary collapse
- #change_template_text_in_cells(drawing, locals = {}) ⇒ Object
- #change_template_text_normal(drawing, locals = {}) ⇒ Object
-
#initialize(template, output_dir: nil, app: nil, name: nil) ⇒ Template
constructor
A new instance of Template.
- #normalize_hash(scope) ⇒ Object
- #render(name: nil, output_dir: nil, locals: EMPTY_HASH, tagsets: EMPTY_ARRAY) ⇒ Object
- #run(update = {}) ⇒ Object
- #template ⇒ Object
- #update_liquid_text(text, locals) ⇒ Object
- #update_tagsets(drawing, ts_arg) ⇒ Object
- #update_text(drawing, locals = {}) ⇒ Object
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
56 57 58 59 60 61 62 63 |
# File 'lib/microstation/template.rb', line 56 def change_template_text_in_cells(drawing, locals = {}) drawing.scan_cells do |_model, 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
49 50 51 52 53 54 |
# File 'lib/microstation/template.rb', line 49 def change_template_text_normal(drawing, locals = {}) drawing.scan_text do |_model, text| new_text = update_liquid_text(text, locals) text.replace new_text if new_text != text.to_s end end |
#normalize_hash(scope) ⇒ Object
75 76 77 78 79 |
# File 'lib/microstation/template.rb', line 75 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 32 |
# 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 && == EMPTY_ARRAY @changer.run(name: name, output_dir: output_dir) do |drawing| locals = normalize_hash(locals) update_text(drawing, locals) (drawing, ) end end |
#run(update = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/microstation/template.rb', line 34 def run(update = {}) @changer.run do |drawing| locals = normalize_hash(update) return if locals == {} update_text(drawing, locals) (drawing, ) end end |
#template ⇒ Object
20 21 22 |
# File 'lib/microstation/template.rb', line 20 def template @changer.template end |
#update_liquid_text(text, locals) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/microstation/template.rb', line 65 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 text end end |
#update_tagsets(drawing, ts_arg) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/microstation/template.rb', line 81 def (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.(hash_pair.keys[0], hash_pair.values[0]) end end |
#update_text(drawing, locals = {}) ⇒ Object
44 45 46 47 |
# File 'lib/microstation/template.rb', line 44 def update_text(drawing, locals = {}) change_template_text_normal(drawing, locals) change_template_text_in_cells(drawing, locals) end |