Class: Microstation::Template
- Inherits:
-
Object
- Object
- Microstation::Template
- Defined in:
- lib/microstation/template.rb
Instance Method Summary collapse
- #__run__(context, locals = {}, file) ⇒ Object
- #add_binding(scope, locals = {}, &block) ⇒ Object
- #add_binding_erb(object) ⇒ Object
- #close ⇒ Object
-
#initialize(template, app = nil) ⇒ Template
constructor
A new instance of Template.
- #normalize_hash(scope) ⇒ Object
- #render(context, locals = {}, output) ⇒ Object
- #update_tagset(drawing, name, values) ⇒ Object
Constructor Details
#initialize(template, app = nil) ⇒ Template
Returns a new instance of Template.
9 10 11 12 |
# File 'lib/microstation/template.rb', line 9 def initialize(template,app = nil) # @app = app || Microstation::App.new @template = template end |
Instance Method Details
#__run__(context, locals = {}, file) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/microstation/template.rb', line 57 def __run__(context,locals={},file) Microstation.run do |app| app.new_drawing(file,@template) do |drawing| scope, = add_binding(context,locals) .each do |,values| = drawing.(.to_s).first require 'pry' binding.pry .update(values) if end drawing.scan_text do |text| # binding.pry if text =~ /usi_west|usi_east/ compiled = ::Liquid::Template.parse(text.to_s) new_text = compiled.render(scope) rescue text #binding.pry if new_text != text.to_s text.replace(new_text) end end end #file end end |
#add_binding(scope, locals = {}, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/microstation/template.rb', line 26 def add_binding(scope,locals={},&block) = locals.delete(:tagsets) nlocals = normalize_hash(locals) nscope = normalize_hash(scope) nlocals = locals.merge(nscope) nlocals['yield'] = block.nil? ? '' : yield nlocals['content'] = nlocals['yield'] [nlocals,] end |
#add_binding_erb(object) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/microstation/template.rb', line 49 def add_binding_erb(object) class << object def get_binding binding end end end |
#close ⇒ Object
14 15 16 |
# File 'lib/microstation/template.rb', line 14 def close @app.quit end |
#normalize_hash(scope) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/microstation/template.rb', line 36 def normalize_hash(scope) scope = scope.to_h if scope.respond_to?(:to_h) if scope.kind_of? Hash scope = scope.map_k{|k| k.to_s} end scope end |
#render(context, locals = {}, output) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/microstation/template.rb', line 18 def render(context,locals={}, output) temp = Tempfile.new('working') temp.close __run__(context,locals,temp.path) FileUtils.mv(temp,output) puts "Printed drawing #{output}" end |
#update_tagset(drawing, name, values) ⇒ Object
44 45 46 47 |
# File 'lib/microstation/template.rb', line 44 def (drawing,name,values) = drawing.(name) .update(values) end |