yampla 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
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", :default => "index_template.html"
29
- opt :item, "Set item template file", :default => "item_template.html"
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
- abort "YAML file not found" unless yaml
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 opts[:out]
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 'index'
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 'items'
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
- STDERR.puts "'out' option is wrong. 'all', 'index', 'items' are acceptable."
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
@@ -1,3 +1,3 @@
1
1
  module Yampla
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
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 { should eq "" }
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 { should == {'item1' => "", 'item2' => ""} }
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) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yampla
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: