smallcage 0.1.5 → 0.1.6

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/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.1.6 2009-09-07
2
+
3
+ * use _local.smc for local machine settings. This file overwrite _dir.smc.
4
+ * erase statistics line when quiet option is on.
5
+
1
6
  == 0.1.5 2009-08-15
2
7
 
3
8
  * use @erbout for ERB output buffer variable name.
@@ -26,7 +26,7 @@ module SmallCage::Commands
26
26
  save_list(urilist)
27
27
  elapsed = Time.now - start
28
28
 
29
- puts "-- #{rendered} files. #{"%.3f" % elapsed} sec. #{"%.3f" % (elapsed/rendered)} sec/file."
29
+ puts "-- #{rendered} files. #{"%.3f" % elapsed} sec. #{"%.3f" % (elapsed/rendered)} sec/file." unless @opts[:quiet]
30
30
  end
31
31
 
32
32
  def render_smc_files
@@ -1,13 +1,14 @@
1
1
  module SmallCage
2
2
  class Loader
3
3
  DEFAULT_TEMPLATE = "default"
4
- DIR_PROP_FILE = "_dir.smc"
4
+ DIR_PROP_FILE = "_dir.smc"
5
+ LOCAL_PROP_FILE = "_local.smc"
5
6
  MAX_DEPTH = 100
6
7
 
7
8
  attr_reader :root, :target, :erb_base
8
9
 
9
10
  def initialize(target)
10
- target = Pathname.new(target.to_s.strip.gsub(%r{(.+)/$}, '\1'))
11
+ target = Pathname.new(target.to_s.strip.chomp('/'))
11
12
  target = real_target(target)
12
13
 
13
14
  @target = target # absolute
@@ -21,101 +22,104 @@ module SmallCage
21
22
 
22
23
  # return root dir Pathname object.
23
24
  def self.find_root(path, depth = MAX_DEPTH)
24
- unless path.exist?
25
- raise "Not found: " + path.to_s
26
- end
25
+ raise "Not found: #{path}" unless path.exist?
27
26
  d = path.realpath
28
-
29
- if d.file?
30
- d = d.parent
31
- end
32
-
33
- i = 0
27
+ d = d.parent if d.file?
34
28
  loop do
35
- if d.join("_smc").directory?
36
- return d
37
- end
38
- break if d.root?
29
+ return d if d.join("_smc").directory?
30
+ break if d.root? || (depth -= 1) <= 0
39
31
  d = d.parent
40
-
41
- i += 1
42
- break if depth <= i
43
32
  end
44
-
45
- raise "Root not found: " + path
33
+ raise "Root not found: #{path}"
46
34
  end
47
35
 
48
36
  def load(path)
49
- unless path.exist?
50
- raise "Not found: " + path.to_s
51
- end
37
+ raise "Not found: #{path}" unless path.exist?
52
38
 
53
39
  docpath = SmallCage::DocumentPath.new(@root, path)
54
40
 
55
- result = {}
56
41
  if path.file?
57
- unless docpath.smc?
58
- raise "Path is not smc file: " + docpath.to_s
59
- end
42
+ return load_smc_file(docpath)
43
+ else
44
+ return load_dir_prop(docpath)
45
+ end
46
+ end
60
47
 
61
- path_smc = docpath.path
62
- path_out = docpath.outfile.path
63
- uri_smc = docpath.uri
64
- uri_out = docpath.outuri
65
- source_path = path_smc
48
+ def load_smc_file(docpath)
49
+ raise "Path is not smc file: #{docpath}" unless docpath.smc?
66
50
 
67
- result["dirs"] = load_dirs(path)
68
- result["template"] = DEFAULT_TEMPLATE
69
- else # directory
70
- path_smc = nil
71
- path_out = path
72
- uri_smc = nil
73
- uri_out = docpath.uri
74
- uri_out += "/" unless uri_out =~ %r{/$}
75
- source_path = path + DIR_PROP_FILE
76
-
77
- if source_path.file?
78
- path_smc = source_path
79
- uri_smc = SmallCage::DocumentPath.to_uri(@root, source_path)
80
- end
81
- end
51
+ result = create_base_smc_object(docpath.outfile.path, docpath.path,
52
+ docpath.outuri, docpath.uri)
82
53
 
83
- add_smc_method(path_out, path_smc)
84
- add_smc_method(uri_out, uri_smc)
54
+ result["template"] = DEFAULT_TEMPLATE
55
+ result["dirs"] = load_dirs(docpath.path)
56
+
57
+ return result.merge(load_yaml(docpath.path))
58
+ end
59
+ private :load_smc_file
60
+
61
+ def load_dir_prop(docpath)
62
+ path_smc = nil
63
+ uri_smc = nil
64
+ uri_out = docpath.uri
65
+ uri_out += "/" unless uri_out[-1] == ?/
66
+
67
+ dir_prop_file = docpath.path + DIR_PROP_FILE
68
+ local_prop_file = docpath.path + LOCAL_PROP_FILE
69
+ if dir_prop_file.file?
70
+ path_smc = dir_prop_file
71
+ uri_smc = SmallCage::DocumentPath.to_uri(@root, dir_prop_file)
72
+ end
73
+
74
+ result = create_base_smc_object(docpath.path, path_smc,
75
+ uri_out, uri_smc)
85
76
 
86
- result["path"] = path_out
87
- result["uri"] = uri_out
77
+ result.merge!(load_yaml(dir_prop_file)) if dir_prop_file.file?
78
+ result.merge!(load_yaml(local_prop_file)) if local_prop_file.file?
79
+
80
+ return result
81
+ end
82
+ private :load_dir_prop
83
+
84
+ def create_base_smc_object(path_out, path_smc, uri_out, uri_smc)
85
+ result = {}
88
86
  result["arrays"] = []
89
87
  result["strings"] = []
90
88
  result["body"] = nil
89
+ result["path"] = add_smc_method(path_out, path_smc)
90
+ result["uri"] = add_smc_method(uri_out, uri_smc )
91
+ return result
92
+ end
93
+ private :create_base_smc_object
91
94
 
92
- # target is directory and _dir.smc is not exist.
93
- return result unless source_path.exist?
95
+ def load_yaml(path)
96
+ result = {}
94
97
 
95
- source = source_path.read
98
+ source = path.read
96
99
  return result if source.strip.empty?
97
-
98
100
  begin
99
101
  obj = YAML.load_stream(source)
100
102
  return result if obj.nil?
101
103
  rescue => e
102
- raise "Can't load file: #{source_path} / #{e}"
104
+ raise "Can't load file: #{path} / #{e}"
103
105
  end
104
-
105
106
  obj.documents.each do |o|
106
- if o.is_a? Hash
107
- result = result.merge(o)
108
- elsif o.is_a? Array
107
+ case o
108
+ when Hash
109
+ result.merge!(o)
110
+ when Array
111
+ result["arrays"] ||= []
109
112
  result["arrays"] << o
110
113
  else
114
+ result["strings"] ||= []
111
115
  result["strings"] << o.to_s
112
116
  end
113
117
  end
114
-
115
- result["body"] = result["strings"][0] if result["body"].nil?
118
+ result["body"] ||= result["strings"][0] if result["strings"]
116
119
 
117
120
  return result
118
121
  end
122
+ private :load_yaml
119
123
 
120
124
  def load_dirs(path)
121
125
  result = []
@@ -138,6 +142,7 @@ module SmallCage
138
142
  each_smc_file do |path|
139
143
  next if path.directory?
140
144
  next if path.basename.to_s == DIR_PROP_FILE
145
+ next if path.basename.to_s == LOCAL_PROP_FILE
141
146
  obj = load(path)
142
147
  yield obj
143
148
  end
@@ -276,8 +281,10 @@ module SmallCage
276
281
  def obj.smc
277
282
  return @__smallcage.nil? ? nil : @__smallcage[:smc]
278
283
  end
284
+
285
+ return obj
279
286
  end
280
287
  private :add_smc_method
281
288
 
282
289
  end
283
- end
290
+ end
@@ -2,7 +2,7 @@ module SmallCage #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 5
5
+ TINY = 6
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -0,0 +1,4 @@
1
+ a: 123
2
+ b: 345
3
+ --- |
4
+ dirsmc
@@ -0,0 +1,3 @@
1
+ b: 567
2
+ --- |
3
+ localsmc
@@ -0,0 +1,4 @@
1
+ a: 123
2
+ b: 345
3
+ --- |
4
+ dirsmc
@@ -0,0 +1,2 @@
1
+ z: 999
2
+ no_string_test: true
@@ -0,0 +1,8 @@
1
+ only_local_smc: true
2
+ ---
3
+ - only
4
+ - local
5
+ - smc
6
+ - array
7
+ --- |
8
+ strings
@@ -0,0 +1,6 @@
1
+ title: dummy
2
+ --- |
3
+ foo
4
+ bar
5
+ foo
6
+ bar
data/spec/loader_spec.rb CHANGED
@@ -5,6 +5,7 @@ describe SmallCage::Loader do
5
5
 
6
6
  before do
7
7
  @docroot = Pathname.new(File.dirname(__FILE__) + "/data/htdocs1")
8
+ @docroot3 = Pathname.new(File.dirname(__FILE__) + "/data/htdocs3")
8
9
  end
9
10
 
10
11
  it "should load path value which is instance of Pathname" do
@@ -85,5 +86,26 @@ describe SmallCage::Loader do
85
86
  dirs[0]["strings"][0].should == "BODYBODYBODY"
86
87
  dirs[0]["body"].should == "BODYBODYBODY"
87
88
  end
89
+
90
+ it "should load _local.smc" do
91
+ ldr = SmallCage::Loader.new(@docroot3)
92
+ obj = ldr.load(@docroot3 + "a/b/c/index.html.smc")
93
+
94
+ dirs = obj["dirs"]
95
+ dirs[0]["a"].should == 123
96
+ dirs[0]["b"].should == 567
97
+ dirs[0]["body"].strip.should == "localsmc"
98
+ dirs[0]["strings"][0].strip.should == "localsmc"
99
+
100
+ dirs[2]["a"].should == 123
101
+ dirs[2]["b"].should == 345
102
+ dirs[2]["z"].should == 999
103
+ dirs[2]["body"].should == "dirsmc"
104
+
105
+ dirs[3]["only_local_smc"].should be_true
106
+ dirs[3]["arrays"][0][2].should == "smc"
107
+ dirs[3]["body"].should == "strings"
108
+
109
+ end
88
110
 
89
- end
111
+ end
@@ -3,13 +3,12 @@ require 'smallcage'
3
3
 
4
4
  describe "smallcage" do
5
5
 
6
- docroot = Pathname.new(File.dirname(__FILE__) + "/data/htdocs1")
7
-
8
6
  it "show version" do
9
7
  # puts "------- version:" + SmallCage::VERSION::STRING
10
8
  end
11
9
 
12
10
  it "should update not docroot directory" do
11
+ docroot = Pathname.new(File.dirname(__FILE__) + "/data/htdocs1")
13
12
  path = docroot + "a/b/"
14
13
 
15
14
  opts = { :command => "update", :path => path.to_s, :quiet => true }
@@ -36,5 +35,24 @@ describe "smallcage" do
36
35
  end
37
36
 
38
37
  end
38
+
39
+ it "should not publish _dir.smc and _local.smc" do
40
+ root = Pathname.new(File.dirname(__FILE__) + "/data/htdocs3")
41
+
42
+ opts = { :command => "update", :path => root.to_s, :quiet => true }
43
+
44
+ begin
45
+ SmallCage::Runner.run(opts)
46
+
47
+ out = root + "_dir"
48
+ out.file?.should be_false
49
+
50
+ out = root + "_local"
51
+ out.file?.should be_false
52
+ ensure
53
+ SmallCage::Runner.run({:command => "clean", :path => root.to_s, :quiet => true })
54
+ end
55
+
56
+ end
39
57
 
40
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smallcage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - SAITO Toshihiro
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2009-08-15 00:00:00 +09:00
14
+ date: 2009-09-07 00:00:00 +09:00
15
15
  default_executable:
16
16
  dependencies: []
17
17
 
@@ -115,6 +115,12 @@ files:
115
115
  - spec/data/htdocs2/a/b/c/test.html
116
116
  - spec/data/htdocs2/a/b/test.html
117
117
  - spec/data/htdocs2/a/test.html.smc
118
+ - spec/data/htdocs3/_dir.smc
119
+ - spec/data/htdocs3/_local.smc
120
+ - spec/data/htdocs3/a/b/_dir.smc
121
+ - spec/data/htdocs3/a/b/_local.smc
122
+ - spec/data/htdocs3/a/b/c/_local.smc
123
+ - spec/data/htdocs3/a/b/c/index.html.smc
118
124
  - spec/document_path_spec.rb
119
125
  - spec/export_spec.rb
120
126
  - spec/import_spec.rb