YamlFrontMattersTemplate coverage
arnaud sellenet
committed Jun 17, 2014
commit 3df7d84e27a99850d8b401af6a4ba1988987000e
Showing 2
changed files with
74 additions
and 14 deletions
locomotive/steam/loaders/yml/utils/yaml_front_matters_template.rb b/lib/locomotive/steam/loaders/yml/utils/yaml_front_matters_template.rb
+35
-14
| @@ | @@ -5,16 +5,31 @@ module Locomotive |
| # YAML Front-matters for HAML/Liquid templates | |
| class YAMLFrontMattersTemplate | |
| - | attr_accessor :filepath, :attributes, :raw_source, :line_offset |
| + | attr_reader :filepath |
| def initialize(filepath) | |
| - | self.filepath = filepath |
| - | self.line_offset = 0 |
| + | @filepath = filepath |
| + | @line_offset = 0 |
| + | @parsed = false |
| + | end |
| + | |
| + | def attributes |
| + | self.fetch_attributes_and_raw_source |
| + | @attributes |
| + | end |
| - | self.fetch_attributes_and_raw_source(File.read(self.filepath)) |
| + | def raw_source |
| + | self.fetch_attributes_and_raw_source |
| + | @raw_source |
| + | end |
| + | |
| + | def line_offset |
| + | self.fetch_attributes_and_raw_source |
| + | @line_offset |
| end | |
| def source | |
| + | self.fetch_attributes_and_raw_source |
| return @source if @source | |
| @source = if self.filepath.ends_with?('.haml') | |
| @@ | @@ -24,21 +39,27 @@ module Locomotive |
| end | |
| end | |
| - | protected |
| + | def data |
| + | @data ||= File.read(filepath) |
| + | end |
| - | def fetch_attributes_and_raw_source(data) |
| + | protected |
| + | def parsed? |
| + | @parsed |
| + | end |
| + | def fetch_attributes_and_raw_source |
| + | return if @parsed |
| if data =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)(.*)/m | |
| - | self.line_offset = $1.count("\n") + 1 |
| - | self.attributes = YAML.load($1) |
| - | self.raw_source = $3 |
| + | @line_offset = $1.count("\n") + 1 |
| + | @attributes = YAML.load($1) |
| + | @raw_source = $3 |
| else | |
| - | self.attributes = nil |
| - | self.raw_source = data |
| + | @attributes = {} |
| + | @raw_source = data |
| end | |
| - | |
| - | self.raw_source = self.raw_source.force_encoding('utf-8') |
| + | @raw_source.force_encoding('utf-8') |
| + | @parsed = true |
| end | |
| - | |
| end | |
| end | |
| end | |
spec/unit/loaders/utils/yaml_front_matters_template_spec.rb
+39
-0
| @@ | @@ -0,0 +1,39 @@ |
| + | require 'spec_helper' |
| + | |
| + | describe Locomotive::Steam::Utils::YAMLFrontMattersTemplate do |
| + | |
| + | let(:regular_content) do |
| + | <<YAMLRAW |
| + | %p Content |
| + | YAMLRAW |
| + | end |
| + | |
| + | let(:attributes_content) do |
| + | <<YAMLRAW |
| + | --- |
| + | data: value 1 |
| + | other: value 2 |
| + | --- |
| + | %p Content |
| + | YAMLRAW |
| + | end |
| + | |
| + | subject { Locomotive::Steam::Utils::YAMLFrontMattersTemplate.new('dummy_path.haml') } |
| + | before { subject.stub(data: content) } |
| + | |
| + | context 'regular data' do |
| + | let(:content) { regular_content } |
| + | its(:source) { should eql "<p>Content</p>\n" } |
| + | its(:line_offset) { should eql 0 } |
| + | its(:attributes) { should eql({}) } |
| + | end |
| + | |
| + | context 'data with attributes' do |
| + | let(:content) { attributes_content } |
| + | its(:source) { should eql "<p>Content</p>\n" } |
| + | its(:line_offset) { should eql 4 } |
| + | its(:attributes) { should eql 'data' => 'value 1', 'other' => 'value 2' } |
| + | end |
| + | |
| + | |
| + | end |