valise 0.6 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ module Valise
2
+ module Debugging
3
+ def remark
4
+ end
5
+
6
+ class << self
7
+ attr_reader :remark_to
8
+
9
+ def enable(destination)
10
+ @remark_to = destination
11
+ class_eval do
12
+ def remark
13
+ self.class.remark_to.puts(yield)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/valise/errors.rb CHANGED
@@ -1,30 +1,34 @@
1
1
  module Valise
2
- class Error < ::Exception; end
2
+ module Errors
3
+ class Error < ::Exception; end
3
4
 
4
- class PathOutsideOfRoot < Error; end
5
- class PathNotInRoot < Error; end
6
- class PathNotFound < Error; end
7
- class ReadOnly < Error; end
8
- class VirtualSearchPath < Error; end
9
- class MalformedTree < Error; end
10
- class RootNotInSet < Error; end
5
+ class PathOutsideOfRoot < Error; end
6
+ class PathNotInRoot < Error; end
7
+ class PathNotFound < Error; end
8
+ class ReadOnly < Error; end
9
+ class NotFound < Error; end
10
+ class VirtualSearchPath < Error; end
11
+ class MalformedTree < Error; end
12
+ class RootNotInSet < Error; end
13
+ class UnderIndented < Error; end
11
14
 
12
- class WouldClobber < Error
13
- def initialize(item)
14
- @item = item
15
- super("Would clobber #{item.segments.inspect}")
15
+ class WouldClobber < Error
16
+ def initialize(item)
17
+ @item = item
18
+ super("Would clobber #{item.segments.inspect}")
19
+ end
20
+
21
+ attr_reader :item
16
22
  end
17
23
 
18
- attr_reader :item
19
- end
24
+ class CantPopulate < Error
25
+ def initialize(item, cause)
26
+ @item = item
27
+ @original_exception = cause
28
+ super("Couldn't populate #{item.segments.inspect}: #{cause.class}: #{cause.message}")
29
+ end
20
30
 
21
- class CantPopulate < Error
22
- def initialize(item, cause)
23
- @item = item
24
- @original_exception = cause
25
- super("Couldn't populate #{item.segments.inspect}: #{cause.class}: #{cause.message}")
31
+ attr_reader :item, :original_exception
26
32
  end
27
-
28
- attr_reader :item, :original_exception
29
33
  end
30
34
  end
@@ -106,7 +106,7 @@ module Valise
106
106
  if segments.empty?
107
107
  @value = result
108
108
  else
109
- raise "File globs can only be used as suffixes"
109
+ raise ArgumentError, "File globs can only be used as suffixes"
110
110
  end
111
111
  end
112
112
  end
@@ -61,7 +61,7 @@ module Valise
61
61
 
62
62
  def insert(item)
63
63
  if(File::exists?(item.full_path))
64
- raise WouldClobber.new(Item.new(item.stack.segments, self, nil))
64
+ raise Errors::WouldClobber.new(Item.new(item.stack.segments, self, nil))
65
65
  else
66
66
  write(item)
67
67
  return item
@@ -79,11 +79,11 @@ module Valise
79
79
  end
80
80
 
81
81
  def write(item)
82
- raise ReadOnly
82
+ raise Errors::ReadOnly
83
83
  end
84
84
 
85
85
  def insert(item)
86
- raise ReadOnly
86
+ raise Errors::ReadOnly
87
87
  end
88
88
  end
89
89
 
@@ -93,7 +93,7 @@ module Valise
93
93
  end
94
94
 
95
95
  def segments
96
- raise VirtualSearchPath, "does not have a real path"
96
+ raise Errors::VirtualSearchPath, "does not have a real path"
97
97
  end
98
98
 
99
99
  def present?(segments)
@@ -126,7 +126,7 @@ module Valise
126
126
  check_path = path[0..-2]
127
127
  until check_path.empty?
128
128
  if @files.has_key?(check_path)
129
- raise MalformedTree, "Tried to add items below #{path[0..-2]} which is not a directory"
129
+ raise Errors::MalformedTree, "Tried to add items below #{path[0..-2]} which is not a directory"
130
130
  end
131
131
  check_path.pop
132
132
  end
@@ -0,0 +1,64 @@
1
+ require 'valise/utils'
2
+ require 'valise/search-root'
3
+ require 'valise/item'
4
+
5
+ module Valise
6
+ class Set
7
+ class StemmedDefiner
8
+ include Unpath
9
+ def initialize(path, set)
10
+ @segments = unpath(path)
11
+ @target = set
12
+ end
13
+
14
+ def rw(name)
15
+ @target.add_search_root(
16
+ StemDecorator.new(@segments, SearchRoot.new(name)))
17
+ end
18
+
19
+ def ro(name)
20
+ @target.add_search_root(
21
+ StemDecorator.new(@segments, ReadOnlySearchRoot.new(name)))
22
+ end
23
+ end
24
+
25
+ class Definer
26
+ include Unpath
27
+ def initialize(set)
28
+ @target = set
29
+ end
30
+
31
+ def rw(name, path = nil)
32
+ @target.add_search_root(SearchRoot.new(name))
33
+ end
34
+
35
+ def ro(name, path = nil)
36
+ @target.add_search_root(ReadOnlySearchRoot.new(name))
37
+ end
38
+
39
+ def stemmed(path, &block)
40
+ definer = StemmedDefiner.new(path, @target)
41
+ definer.instance_eval(&block) unless block.nil?
42
+ return definer
43
+ end
44
+
45
+ def from_here(rel_path)
46
+ m = /(.*):\d+/.match(caller[0])
47
+ dir = ::File::dirname(::File::expand_path(m[1]))
48
+
49
+ unpath(dir) + unpath(rel_path)
50
+ end
51
+
52
+ def handle(path, serialization, merge_diff = nil)
53
+ @target.add_handler(unpath(path),
54
+ Valise::Serialization[serialization],
55
+ Valise::MergeDiff[merge_diff])
56
+ end
57
+
58
+ def defaults(name=nil, &block)
59
+ loc = DefinedDefaults.define(&block)
60
+ @target.add_search_root(loc)
61
+ end
62
+ end
63
+ end
64
+ end
data/lib/valise/set.rb ADDED
@@ -0,0 +1,148 @@
1
+ require 'valise/debugging'
2
+ require 'valise/errors'
3
+ require 'valise/search-root'
4
+ require 'valise/utils'
5
+ require 'valise/stack'
6
+ require 'valise/path-matcher'
7
+ require 'valise/stem-decorator'
8
+ require 'valise/set/definer'
9
+
10
+ module Valise
11
+ class Set
12
+ include Debugging
13
+ include Enumerable
14
+
15
+ def initialize
16
+ @search_roots = []
17
+ @merge_diff = PathMatcher.new
18
+ @serialization = PathMatcher.new
19
+ end
20
+
21
+ def inspect
22
+ @search_roots.inspect
23
+ end
24
+
25
+ def reverse
26
+ set = Set.new
27
+ set.search_roots = @search_roots.reverse
28
+ set
29
+ end
30
+
31
+ def define(&block)
32
+ definer = Definer.new(self)
33
+ definer.instance_eval(&block)
34
+ return self
35
+ end
36
+
37
+ def self.define(&block)
38
+ return self.new.define(&block)
39
+ end
40
+
41
+ def prepend_search_root(search_root)
42
+ @search_roots.unshift(search_root)
43
+ end
44
+
45
+ def add_search_root(search_root)
46
+ @search_roots << search_root
47
+ end
48
+
49
+ def add_handler(segments, serialization_class, merge_diff_class)
50
+ @merge_diff[segments] = merge_diff_class unless merge_diff_class.nil?
51
+ @serialization[segments] = serialization_class unless serialization_class.nil?
52
+ end
53
+
54
+ def +(other)
55
+ result = self.class.new
56
+ result.search_roots = @search_roots + other.search_roots
57
+ result.merge_handlers(*other.handler_lists)
58
+ result.merge_handlers(*handler_lists)
59
+ return result
60
+ end
61
+
62
+ attr_accessor :search_roots
63
+ protected :search_roots, :search_roots=
64
+
65
+ include Unpath
66
+ def get(path)
67
+ return Stack.new(path, self,
68
+ merge_diff(path),
69
+ serialization(path))
70
+ end
71
+
72
+ def merge_diff(path)
73
+ @merge_diff[unpath(path)]
74
+ end
75
+
76
+ def serialization(path)
77
+ @serialization[unpath(path)]
78
+ end
79
+
80
+ def merge_handlers(merge_diff, serialization)
81
+ @merge_diff.merge!(merge_diff)
82
+ @serialization.merge!(serialization)
83
+ end
84
+
85
+ def handler_lists
86
+ [@merge_diff, @serialization]
87
+ end
88
+
89
+ def find(path)
90
+ item = get(path).present.first
91
+ return item unless item.nil?
92
+ raise Errors::NotFound, "#{path} not found in #{@search_roots.inspect}"
93
+ end
94
+
95
+ def each(&block)
96
+ @search_roots.each(&block)
97
+ end
98
+
99
+ def files
100
+ unless block_given?
101
+ return self.enum_for(:files)
102
+ end
103
+
104
+ visited = {}
105
+ @search_roots.each do |root|
106
+ root.each do |segments|
107
+ unless visited.has_key?(segments)
108
+ item = find(segments)
109
+ visited[segments] = item
110
+ yield(item)
111
+ end
112
+ end
113
+ end
114
+ return visited
115
+ end
116
+
117
+ def not_above(root)
118
+ index = @search_roots.index(root)
119
+ raise Errors::RootNotInSet if index.nil?
120
+ set = self.class.new
121
+ set.search_roots = @search_roots[index..-1]
122
+ set
123
+ end
124
+
125
+ def below(root)
126
+ index = @search_roots.index(root)
127
+ raise Errors::RootNotInSet if index.nil?
128
+ set = self.class.new
129
+ set.search_roots = @search_roots[(index+1)..-1]
130
+ set
131
+ end
132
+
133
+ def [](index)
134
+ return @search_roots[index]
135
+ end
136
+
137
+ def populate(to = self)
138
+ files do |item|
139
+ contents = item.contents
140
+ to_stack = to.get(item.segments)
141
+ to_stack = yield(to_stack) if block_given?
142
+ target = to_stack.writable.first
143
+ next if target.present?
144
+ target.contents = contents
145
+ end
146
+ end
147
+ end
148
+ end
@@ -10,7 +10,7 @@ module Valise
10
10
  if top_part == @stem
11
11
  return segments[@stem.length..-1]
12
12
  else
13
- raise PathOutsideOfRoot
13
+ raise Errors::PathOutsideOfRoot
14
14
  end
15
15
  end
16
16
 
@@ -35,13 +35,13 @@ module Valise
35
35
 
36
36
  def writable?(segments)
37
37
  @search_root.writable?(under_stem(segments))
38
- rescue PathOutsideOfRoot
38
+ rescue Errors::PathOutsideOfRoot
39
39
  return false
40
40
  end
41
41
 
42
42
  def present?(segments)
43
43
  @search_root.present?(under_stem(segments))
44
- rescue PathOutsideOfRoot
44
+ rescue Errors::PathOutsideOfRoot
45
45
  return false
46
46
  end
47
47
 
data/lib/valise/utils.rb CHANGED
@@ -5,13 +5,12 @@ module Valise
5
5
  first = lines.shift
6
6
  match = /^(\s*)<<</.match(first)
7
7
  unless(match.nil?)
8
- catch(:haircut) do
9
- return lines.map do |line|
10
- raise line if /^#{match[1]}|^\s*$/ !~ line
11
- throw :haircut if /^#{match[1]}|^\s*$/ !~ line
12
- line.sub(/^#{match[1]}/, "")
13
- end.join("\n")
14
- end
8
+ return lines.map do |line|
9
+ unless /^#{match[1]}|^\s*$/ =~ line
10
+ raise Errors::UnderIndented, line
11
+ end
12
+ line.sub(/^#{match[1]}/, "")
13
+ end.join("\n")
15
14
  end
16
15
  return string
17
16
  end
@@ -29,7 +28,7 @@ module Valise
29
28
  if (parts.find{|part| not (String === part or Symbol === part)}.nil?)
30
29
  parts = parts.map{|part| part.to_s}
31
30
  else
32
- raise "path must be composed of strings or symbols"
31
+ raise ArgumentError, "path must be composed of strings or symbols"
33
32
  end
34
33
  when String
35
34
  parts = parts.split(::File::Separator)
@@ -39,7 +38,7 @@ module Valise
39
38
  parts = parts.path
40
39
  parts = parts.split(::File::Separator)
41
40
  else
42
- raise "path must be String, Array of Strings or File"
41
+ raise ArgumentError, "path must be String, Array of Strings or File"
43
42
  end
44
43
 
45
44
  parts = parts.map do |part|
data/lib/valise.rb CHANGED
@@ -1,221 +1,8 @@
1
+ require 'valise/set'
1
2
  require 'valise/errors'
2
- require 'valise/search-root'
3
- require 'valise/utils'
4
- require 'valise/stack'
5
- require 'valise/path-matcher'
6
- require 'valise/stem-decorator'
7
3
 
8
4
  module Valise
9
- module Debugging
10
- def remark
11
- end
12
-
13
- class << self
14
- attr_reader :remark_to
15
-
16
- def enable(destination)
17
- @remark_to = destination
18
- class_eval do
19
- def remark
20
- self.class.remark_to.puts(yield)
21
- end
22
- end
23
- end
24
- end
25
- end
26
-
27
- class Set
28
- include Debugging
29
- include Enumerable
30
-
31
- class StemmedDefiner
32
- include Unpath
33
- def initialize(path, set)
34
- @segments = unpath(path)
35
- @target = set
36
- end
37
-
38
- def rw(name)
39
- @target.add_search_root(
40
- StemDecorator.new(@segments, SearchRoot.new(name)))
41
- end
42
-
43
- def ro(name)
44
- @target.add_search_root(
45
- StemDecorator.new(@segments, ReadOnlySearchRoot.new(name)))
46
- end
47
- end
48
-
49
- class Definer
50
- include Unpath
51
- def initialize(set)
52
- @target = set
53
- end
54
-
55
- def rw(name, path = nil)
56
- @target.add_search_root(SearchRoot.new(name))
57
- end
58
-
59
- def ro(name, path = nil)
60
- @target.add_search_root(ReadOnlySearchRoot.new(name))
61
- end
62
-
63
- def stemmed(path, &block)
64
- definer = StemmedDefiner.new(path, @target)
65
- definer.instance_eval(&block) unless block.nil?
66
- return definer
67
- end
68
-
69
- def from_here(rel_path)
70
- m = /(.*):\d+/.match(caller[0])
71
- dir = ::File::dirname(::File::expand_path(m[1]))
72
-
73
- unpath(dir) + unpath(rel_path)
74
- end
75
-
76
- def handle(path, serialization, merge_diff = nil)
77
- @target.add_handler(unpath(path),
78
- Valise::Serialization[serialization],
79
- Valise::MergeDiff[merge_diff])
80
- end
81
-
82
- def defaults(name=nil, &block)
83
- loc = DefinedDefaults.define(&block)
84
- @target.add_search_root(loc)
85
- end
86
- end
87
-
88
- def initialize
89
- @search_roots = []
90
- @merge_diff = PathMatcher.new
91
- @serialization = PathMatcher.new
92
- end
93
-
94
- def inspect
95
- @search_roots.inspect
96
- end
97
-
98
- def reverse
99
- set = Set.new
100
- set.search_roots = @search_roots.reverse
101
- set
102
- end
103
-
104
- def define(&block)
105
- definer = Definer.new(self)
106
- definer.instance_eval(&block)
107
- return self
108
- end
109
-
110
- def self.define(&block)
111
- return self.new.define(&block)
112
- end
113
-
114
- def prepend_search_root(search_root)
115
- @search_roots.unshift(search_root)
116
- end
117
-
118
- def add_search_root(search_root)
119
- @search_roots << search_root
120
- end
121
-
122
- def add_handler(segments, serialization_class, merge_diff_class)
123
- @merge_diff[segments] = merge_diff_class unless merge_diff_class.nil?
124
- @serialization[segments] = serialization_class unless serialization_class.nil?
125
- end
126
-
127
- def +(other)
128
- result = self.class.new
129
- result.search_roots = @search_roots + other.search_roots
130
- result.merge_handlers(*other.handler_lists)
131
- result.merge_handlers(*handler_lists)
132
- return result
133
- end
134
-
135
- attr_accessor :search_roots
136
- protected :search_roots, :search_roots=
137
-
138
- include Unpath
139
- def get(path)
140
- return Stack.new(path, self,
141
- merge_diff(path),
142
- serialization(path))
143
- end
144
-
145
- def merge_diff(path)
146
- @merge_diff[unpath(path)]
147
- end
148
-
149
- def serialization(path)
150
- @serialization[unpath(path)]
151
- end
152
-
153
- def merge_handlers(merge_diff, serialization)
154
- @merge_diff.merge!(merge_diff)
155
- @serialization.merge!(serialization)
156
- end
157
-
158
- def handler_lists
159
- [@merge_diff, @serialization]
160
- end
161
-
162
- def find(path)
163
- item = get(path).present.first
164
- return item unless item.nil?
165
- raise "#{path} not found in #{@search_roots.inspect}"
166
- end
167
-
168
- def each(&block)
169
- @search_roots.each(&block)
170
- end
171
-
172
- def files
173
- unless block_given?
174
- return self.enum_for(:files)
175
- end
176
-
177
- visited = {}
178
- @search_roots.each do |root|
179
- root.each do |segments|
180
- unless visited.has_key?(segments)
181
- item = find(segments)
182
- visited[segments] = item
183
- yield(item)
184
- end
185
- end
186
- end
187
- return visited
188
- end
189
-
190
- def not_above(root)
191
- index = @search_roots.index(root)
192
- raise RootNotInSet if index.nil?
193
- set = self.class.new
194
- set.search_roots = @search_roots[index..-1]
195
- set
196
- end
197
-
198
- def below(root)
199
- index = @search_roots.index(root)
200
- raise RootNotInSet if index.nil?
201
- set = self.class.new
202
- set.search_roots = @search_roots[(index+1)..-1]
203
- set
204
- end
205
-
206
- def [](index)
207
- return @search_roots[index]
208
- end
209
-
210
- def populate(to = self)
211
- files do |item|
212
- contents = item.contents
213
- to_stack = to.get(item.segments)
214
- to_stack = yield(to_stack) if block_given?
215
- target = to_stack.writable.first
216
- next if target.present?
217
- target.contents = contents
218
- end
219
- end
5
+ def self.define(&block)
6
+ Valise::Set.define(&block)
220
7
  end
221
8
  end
metadata CHANGED
@@ -1,36 +1,36 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: valise
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.7'
4
5
  prerelease:
5
- version: "0.6"
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Judson Lester
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-03-08 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-09-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: corundum
17
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &88576980 !ruby/object:Gem::Requirement
18
17
  none: false
19
- requirements:
18
+ requirements:
20
19
  - - ~>
21
- - !ruby/object:Gem::Version
20
+ - !ruby/object:Gem::Version
22
21
  version: 0.0.1
23
22
  type: :development
24
23
  prerelease: false
25
- version_requirements: *id001
26
- description: " Valise provides an API for accessing configuration and data files for your\n application, including the population of default values, and managing search\n paths. Written to encourage a cross-platform approach to maintaining configs\n for an application.\n"
27
- email:
24
+ version_requirements: *88576980
25
+ description: ! " Valise provides an API for accessing configuration and data files
26
+ for your\n application, including the population of default values, and managing
27
+ search\n paths. Written to encourage a cross-platform approach to maintaining
28
+ configs\n for an application.\n"
29
+ email:
28
30
  - nyarly@gmail.com
29
31
  executables: []
30
-
31
32
  extensions: []
32
-
33
- extra_rdoc_files:
33
+ extra_rdoc_files:
34
34
  - doc/README
35
35
  - doc/Specification
36
36
  - doc/Specifications
@@ -95,11 +95,14 @@ extra_rdoc_files:
95
95
  - doc/coverage/lib-fileset-populator_rb.html
96
96
  - doc/coverage/screen.css
97
97
  - doc/coverage/print.css
98
- files:
98
+ files:
99
99
  - doc/README
100
100
  - doc/Specification
101
101
  - doc/Specifications
102
102
  - lib/valise.rb
103
+ - lib/valise/debugging.rb
104
+ - lib/valise/set/definer.rb
105
+ - lib/valise/set.rb
103
106
  - lib/valise/errors.rb
104
107
  - lib/valise/item.rb
105
108
  - lib/valise/path-matcher.rb
@@ -183,38 +186,37 @@ files:
183
186
  - doc/coverage/screen.css
184
187
  - doc/coverage/print.css
185
188
  homepage: http://valise.rubyforge.org/
186
- licenses:
189
+ licenses:
187
190
  - MIT
188
191
  post_install_message: Another tidy package brought to you by Judson
189
- rdoc_options:
192
+ rdoc_options:
190
193
  - --inline-source
191
194
  - --main
192
195
  - doc/README
193
196
  - --title
194
- - valise-0.6 RDoc
195
- require_paths:
197
+ - valise-0.7 RDoc
198
+ require_paths:
196
199
  - lib/
197
- required_ruby_version: !ruby/object:Gem::Requirement
200
+ required_ruby_version: !ruby/object:Gem::Requirement
198
201
  none: false
199
- requirements:
200
- - - ">="
201
- - !ruby/object:Gem::Version
202
- hash: 940501423
203
- segments:
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ segments:
204
207
  - 0
205
- version: "0"
206
- required_rubygems_version: !ruby/object:Gem::Requirement
208
+ hash: -446270865
209
+ required_rubygems_version: !ruby/object:Gem::Requirement
207
210
  none: false
208
- requirements:
209
- - - ">="
210
- - !ruby/object:Gem::Version
211
- version: "0"
211
+ requirements:
212
+ - - ! '>='
213
+ - !ruby/object:Gem::Version
214
+ version: '0'
212
215
  requirements: []
213
-
214
216
  rubyforge_project: valise
215
- rubygems_version: 1.8.11
217
+ rubygems_version: 1.8.15
216
218
  signing_key:
217
219
  specification_version: 3
218
220
  summary: Manage configuration and data files simply
219
- test_files:
221
+ test_files:
220
222
  - spec_help/gem_test_suite.rb