yampla 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/yampla +14 -10
- data/lib/yampla/build.rb +3 -0
- data/lib/yampla/version.rb +1 -1
- data/spec/build_spec.rb +6 -2
- metadata +1 -1
data/bin/yampla
CHANGED
@@ -25,8 +25,8 @@ class OptionParser
|
|
25
25
|
EOS
|
26
26
|
|
27
27
|
opt :yaml, "Set data YAML file", :type => :string
|
28
|
-
opt :index, "Set index template file", :
|
29
|
-
opt :item, "Set item template file", :
|
28
|
+
opt :index, "Set index template file", :type => :string
|
29
|
+
opt :item, "Set item template file", :type => :string
|
30
30
|
opt :out, "Output files", :default => 'all' # :index or :items
|
31
31
|
opt :dir, "Output directory", :default => 'out'
|
32
32
|
end
|
@@ -37,21 +37,25 @@ opts = OptionParser.parse!
|
|
37
37
|
files = Dir['*']
|
38
38
|
|
39
39
|
yaml = opts[:yaml] || files.detect { |f| f.match /\w+\.(yaml|yml)$/ }
|
40
|
+
abort "YAML file not found." unless yaml
|
40
41
|
|
41
|
-
|
42
|
+
index = opts[:index] || files.detect { |f| f.match /^index_template/ }
|
43
|
+
item = opts[:item] || files.detect { |f| f.match /^item_template/ }
|
42
44
|
|
43
45
|
ya = Yampla::Build.new(yaml)
|
44
|
-
ya.set_template(:index, opts[:index])
|
45
|
-
ya.set_template(:items, opts[:item])
|
46
46
|
|
47
|
-
case
|
48
|
-
when 'all'
|
47
|
+
case
|
48
|
+
when [index, item].all? && opts[:out] == 'all'
|
49
|
+
ya.set_template(:index, index)
|
50
|
+
ya.set_template(:items, item)
|
49
51
|
ya.save(:index, dir:opts[:dir])
|
50
52
|
ya.save(:items, dir:opts[:dir])
|
51
|
-
when
|
53
|
+
when index && %w(all index).include?(opts[:out])
|
54
|
+
ya.set_template(:index, index)
|
52
55
|
ya.save(:index, dir:opts[:dir])
|
53
|
-
when
|
56
|
+
when item && %w(all item).include?(opts[:out])
|
57
|
+
ya.set_template(:items, item)
|
54
58
|
ya.save(:items, dir:opts[:dir])
|
55
59
|
else
|
56
|
-
|
60
|
+
abort "Any template file not found."
|
57
61
|
end
|
data/lib/yampla/build.rb
CHANGED
@@ -3,6 +3,7 @@ require "hashie"
|
|
3
3
|
require "liquid"
|
4
4
|
|
5
5
|
class Yampla::Build
|
6
|
+
class TemplateError < StandardError; end
|
6
7
|
attr_reader :data
|
7
8
|
def initialize(yaml)
|
8
9
|
@template = {}
|
@@ -30,10 +31,12 @@ class Yampla::Build
|
|
30
31
|
end
|
31
32
|
|
32
33
|
def build_index(template, name=nil)
|
34
|
+
raise TemplateError, 'Template for index not set.' unless template
|
33
35
|
Liquid::Template.parse(template).render("#{name || 'items'}" => @data)
|
34
36
|
end
|
35
37
|
|
36
38
|
def build_items(template, name=nil)
|
39
|
+
raise TemplateError, 'Template for items not set.' unless template
|
37
40
|
@data.each_with_object({}) do |item, h|
|
38
41
|
h[item.id] = Liquid::Template.parse(template).render("#{name || 'item'}" => item)
|
39
42
|
end
|
data/lib/yampla/version.rb
CHANGED
data/spec/build_spec.rb
CHANGED
@@ -61,7 +61,9 @@ describe Yampla::Build do
|
|
61
61
|
context "for index" do
|
62
62
|
context "w/o template" do
|
63
63
|
subject { Yampla::Build.new(@yml).run(:index) }
|
64
|
-
it
|
64
|
+
it "raise no template error" do
|
65
|
+
expect { should }.to raise_error(Yampla::Build::TemplateError)
|
66
|
+
end
|
65
67
|
end
|
66
68
|
context "w/ simple template" do
|
67
69
|
subject { Yampla::Build.new(@yml).run(:index, template:~<<-EOS) }
|
@@ -93,7 +95,9 @@ describe Yampla::Build do
|
|
93
95
|
context "for each item" do
|
94
96
|
context "w/o template" do
|
95
97
|
subject { Yampla::Build.new(@yml).run(:items) }
|
96
|
-
it
|
98
|
+
it "raise no template error" do
|
99
|
+
expect { should }.to raise_error(Yampla::Build::TemplateError)
|
100
|
+
end
|
97
101
|
end
|
98
102
|
context "w/ simple template" do
|
99
103
|
subject { Yampla::Build.new(@yml).run(:items, template:~<<-EOS) }
|