class Genit::Fragment

Replace each fragment in a page.

Public Class Methods

new(file, working_dir) click to toggle source

Public: Constructor.

file - Full String filename of the page. working_dir - The String working directory, where live the project.

# File lib/genit/documents/fragment.rb, line 11
def initialize file, working_dir
  @page = HtmlDocument.open_fragment file
  @working_dir = working_dir
  HtmlDocument.genit_tags_from(@page).each do |tag|
    case tag['class']
      when 'fragment'
        @file = tag['file']
        error "Incomplete #{tag}" unless tag.key?('file')
        unless File.exists?(File.join(@working_dir, FRAGMENTS_DIR, @file))
          error "No such file #{tag}"
        end
        replace_fragment 
    end
  end
end

Public Instance Methods

to_html() click to toggle source

Public: Get the page in html format.

Returns the html code of the page as a String.

# File lib/genit/documents/fragment.rb, line 30
def to_html
  @page.to_html
end

Private Instance Methods

content() click to toggle source
# File lib/genit/documents/fragment.rb, line 45
def content
  full_path = File.join(@working_dir, FRAGMENTS_DIR, @file)
  HtmlDocument.open_as_string(full_path)
end
css_rule() click to toggle source
# File lib/genit/documents/fragment.rb, line 41
def css_rule
  "genit.fragment[file='#{@file}']"
end
replace_fragment() click to toggle source
# File lib/genit/documents/fragment.rb, line 36
def replace_fragment
  builder = Builder.new(@page)
  @page = builder.replace(css_rule, content)
end