Class: ColonizeRepo
- Inherits:
-
Service
- Object
- Service
- ColonizeRepo
- Defined in:
- lib/assets/lib/colonize_repo.rb
Overview
Creates clerq repository sources in current work directory Warning! Change work dierectory before calling for the serice Usage
node = Clerq.node_repository.assembly
Dir.chdir(Clerq.src) { ColonizeRepo.(node) }
Constant Summary collapse
- TEMPLATE =
<<~EOF # <%= @object.title %> {{id: <%= @object.id %><%= ", parent: " + @object.parent.id if @object.parent %>, order_index: <%= @object.items.map(&:id).join(' ') %>}} <%= @object.body %> <% for n in @object.items -%> <% next unless n.items.empty? -%> ## <%= n.title %> {{id: <%= n.id %>}} <%= n.body %> <% end %> EOF
Instance Method Summary collapse
- #call ⇒ Object
- #filename(node) ⇒ Object
- #folder(node) ⇒ Object
-
#initialize(node, on_create_dir = nil, on_create_file = nil) ⇒ ColonizeRepo
constructor
A new instance of ColonizeRepo.
- #source(node) ⇒ Object
-
#text(node) ⇒ Object
TODO replace to services.
-
#write(node) ⇒ Object
TODO callback? and calback for ReadNode.()?.
Constructor Details
#initialize(node, on_create_dir = nil, on_create_file = nil) ⇒ ColonizeRepo
Returns a new instance of ColonizeRepo.
35 36 37 38 39 |
# File 'lib/assets/lib/colonize_repo.rb', line 35 def initialize(node, on_create_dir = nil, on_create_file = nil) @node = node @on_create_dir = on_create_dir @on_create_file = on_create_file end |
Instance Method Details
#call ⇒ Object
14 15 16 |
# File 'lib/assets/lib/colonize_repo.rb', line 14 def call write(@node) end |
#filename(node) ⇒ Object
57 58 59 |
# File 'lib/assets/lib/colonize_repo.rb', line 57 def filename(node) "#{node.id} #{node.title}.md" end |
#folder(node) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/assets/lib/colonize_repo.rb', line 47 def folder(node) dir = '' n = node while n != @node.root && n.parent != @node.root dir = File.join("#{n.parent.id} #{n.parent.title}", dir) n = n.parent end dir end |
#source(node) ⇒ Object
41 42 43 44 45 |
# File 'lib/assets/lib/colonize_repo.rb', line 41 def source(node) fld = folder(node) src = filename(node) fld.empty? ? src : File.join(fld, src) end |
#text(node) ⇒ Object
TODO replace to services
62 63 64 |
# File 'lib/assets/lib/colonize_repo.rb', line 62 def text(node) RenderErb.(erb: TEMPLATE, object: node) end |
#write(node) ⇒ Object
TODO callback? and calback for ReadNode.()?
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/assets/lib/colonize_repo.rb', line 19 def write(node) dir = folder(node) src = source(node) txt = text(node) unless dir.empty? || Dir.exist?(dir) Dir.mkdir(dir) @on_create_dir.call(dir) if @on_create_dir end File.write(src, txt) @on_create_file.call(src) if @on_create_file node.items.reject{|n| n.items.empty?}.each{|n| write(n)} end |