Class: Geb::Partial
- Inherits:
-
Object
- Object
- Geb::Partial
- Defined in:
- lib/geb/partial.rb
Defined Under Namespace
Classes: PartialFileNotFound, PartialFileReadFailure
Constant Summary collapse
- PARTIAL_PATTERN =
partial pattern constant
/<%= partial: (?<path>.*?) %>/
- @@loaded_partials =
define a class level cache for loaded partial objects
{}
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
The content of the partial file.
-
#path ⇒ String
readonly
The path to the partial file.
Class Method Summary collapse
-
.expire_cache ⇒ Object
class method to expire the partial cache.
-
.load(partial_path) ⇒ Geb::Partial
class method to initialise a partial if it is not already loaded, otherwise, return the cached partial.
-
.process_partials(site_path, page_content) ⇒ Array
class method to process partials in a page.
Instance Method Summary collapse
-
#initialize(partial_path) ⇒ Geb::Partial
constructor
initialise a new partial object.
-
#partial_file_exists? ⇒ Boolean
check if the partial file exists.
Constructor Details
#initialize(partial_path) ⇒ Geb::Partial
initialise a new partial object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/geb/partial.rb', line 123 def initialize(partial_path) # set the partial path @path = partial_path @content = nil # check if the partial file exists raise PartialFileNotFound.new(partial_path) unless partial_file_exists?() Geb.log " - loading partial: #{@path}" # read the template file, raise an error if the file could not be read begin @content = File.read(partial_path) rescue => e raise PartialFileReadFailure.new(e.) end # begin end |
Instance Attribute Details
#content ⇒ String (readonly)
Returns the content of the partial file.
116 117 118 |
# File 'lib/geb/partial.rb', line 116 def content @content end |
#path ⇒ String (readonly)
Returns the path to the partial file.
112 113 114 |
# File 'lib/geb/partial.rb', line 112 def path @path end |
Class Method Details
.expire_cache ⇒ Object
class method to expire the partial cache
39 40 41 |
# File 'lib/geb/partial.rb', line 39 def self.expire_cache @@loaded_partials = {} end |
.load(partial_path) ⇒ Geb::Partial
class method to initialise a partial if it is not already loaded, otherwise, return the cached partial
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/geb/partial.rb', line 46 def self.load(partial_path) # initialise a return partial object return_partial = nil # check if the partial is already loaded if @@loaded_partials.key?(partial_path) # return the cached partial Geb.log " - using cached partial: #{partial_path}" return_partial = @@loaded_partials[partial_path] else # create a new partial object return_partial = Partial.new(partial_path) # add the partial to the cache @@loaded_partials[partial_path] = return_partial end # if else # return the partial object return return_partial end |
.process_partials(site_path, page_content) ⇒ Array
class method to process partials in a page
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/geb/partial.rb', line 79 def self.process_partials(site_path, page_content) # initialise a counter to count the number of partials found on the page partial_count = 0 # initialize return page content return_page_content = page_content.dup # scan the page content for partials return_page_content.gsub!(PARTIAL_PATTERN) do |match| # match the partial relative and full paths partial_file_path = match.match(PARTIAL_PATTERN)[:path].strip partial_file_full_path = File.join(site_path, partial_file_path) # load the partial object partial = Partial.load(partial_file_full_path) # increment the partial count partial_count += 1 # return the partial content partial.content end # page_content.scan(PARTIAL_PATTERN) do |match| # return the array of partial paths return partial_count, return_page_content end |
Instance Method Details
#partial_file_exists? ⇒ Boolean
check if the partial file exists
145 146 147 |
# File 'lib/geb/partial.rb', line 145 def partial_file_exists? return File.exist?(@path) end |