valise 0.7 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/valise/item.rb CHANGED
@@ -57,10 +57,18 @@ module Valise
57
57
  "#{@root.inspect}//#{@stack.inspect}"
58
58
  end
59
59
 
60
+ def depth
61
+ @stack.depth_of(self)
62
+ end
63
+
60
64
  def segments
61
65
  @stack.segments
62
66
  end
63
67
 
68
+ def rel_path
69
+ @stack.rel_path
70
+ end
71
+
64
72
  def full_path
65
73
  @root.full_path(segments)
66
74
  end
@@ -4,6 +4,19 @@ module Valise
4
4
  class PathMatcher
5
5
  include Unpath
6
6
 
7
+ def self.build(path, value = true)
8
+ case path
9
+ when PathMatcher
10
+ return path
11
+ when String, Array
12
+ matcher = PathMatcher.new
13
+ matcher[path] = value
14
+ return matcher
15
+ else
16
+ raise ArgumentError, "Path matchers can only be built from arrays or strings"
17
+ end
18
+ end
19
+
7
20
  def initialize(segment = nil)
8
21
  @children = []
9
22
  @segment = segment
@@ -58,6 +71,10 @@ module Valise
58
71
  store(unpath(pattern), result)
59
72
  end
60
73
 
74
+ def ===(path)
75
+ return !!self[path]
76
+ end
77
+
61
78
  def store(segments, result)
62
79
  if segments.empty?
63
80
  @value = result
@@ -66,7 +83,7 @@ module Valise
66
83
  target = @children.find {|child| child.segment == index } ||
67
84
  case index
68
85
  when "**"; DirGlob.new.tap{|m| @children << m}
69
- when /^[*].*/; FileGlob.new(index).tap{|m| @children << m}
86
+ when /.*[*].*/; FileGlob.new(index).tap{|m| @children << m}
70
87
  else; PathMatcher.new(index).tap{|m| @children << m}
71
88
  end
72
89
  target.store(segments, result)
@@ -95,7 +112,7 @@ module Valise
95
112
  class FileGlob < PathMatcher
96
113
  def initialize(segment)
97
114
  super
98
- @regex = %r{.*#{segment.sub(/^[*]/,"")}$}
115
+ @regex = %r{^#{segment.gsub(/[*]/,".*")}$}
99
116
  end
100
117
 
101
118
  def match?(segment)
@@ -37,6 +37,10 @@ module Valise
37
37
  "#{self.class.name.split(":").last}:#{[*@segments].join("/")}"
38
38
  end
39
39
 
40
+ def to_s
41
+ "#{[*@segments].join("/")}"
42
+ end
43
+
40
44
  def full_path(segments)
41
45
  repath(@segments + segments)
42
46
  end
@@ -46,7 +46,7 @@ module Valise
46
46
  m = /(.*):\d+/.match(caller[0])
47
47
  dir = ::File::dirname(::File::expand_path(m[1]))
48
48
 
49
- unpath(dir) + unpath(rel_path)
49
+ collapse(unpath(dir) + unpath(rel_path))
50
50
  end
51
51
 
52
52
  def handle(path, serialization, merge_diff = nil)
data/lib/valise/set.rb CHANGED
@@ -22,9 +22,25 @@ module Valise
22
22
  @search_roots.inspect
23
23
  end
24
24
 
25
+ def to_s
26
+ @search_roots.map(&:to_s).join(":")
27
+ end
28
+
25
29
  def reverse
26
30
  set = Set.new
27
31
  set.search_roots = @search_roots.reverse
32
+ set.merge_diff = @merge_diff.dup
33
+ set.serialization = @serialization.dup
34
+ set
35
+ end
36
+
37
+ def sub_set(path)
38
+ set = Set.new
39
+ set.search_roots = @search_roots.map do |root|
40
+ SearchRoot.new(root.segments + unpath(path))
41
+ end
42
+ set.merge_diff = @merge_diff.dup
43
+ set.serialization = @serialization.dup
28
44
  set
29
45
  end
30
46
 
@@ -59,10 +75,12 @@ module Valise
59
75
  return result
60
76
  end
61
77
 
62
- attr_accessor :search_roots
63
- protected :search_roots, :search_roots=
78
+ attr_accessor :search_roots, :merge_diff, :serialization
79
+ protected :search_roots=, :search_roots,
80
+ :merge_diff=, :merge_diff,
81
+ :serialization=, :serialization
64
82
 
65
- include Unpath
83
+ include Unpath
66
84
  def get(path)
67
85
  return Stack.new(path, self,
68
86
  merge_diff(path),
@@ -96,16 +114,19 @@ module Valise
96
114
  @search_roots.each(&block)
97
115
  end
98
116
 
99
- def files
117
+ def glob(path_matcher)
100
118
  unless block_given?
101
- return self.enum_for(:files)
119
+ return self.enum_for(:glob, path_matcher)
102
120
  end
103
121
 
104
122
  visited = {}
123
+ path_matcher = PathMatcher.build(path_matcher)
124
+
105
125
  @search_roots.each do |root|
106
126
  root.each do |segments|
127
+ next unless path_matcher === segments
107
128
  unless visited.has_key?(segments)
108
- item = find(segments)
129
+ item = get(segments).present.first
109
130
  visited[segments] = item
110
131
  yield(item)
111
132
  end
@@ -114,6 +135,11 @@ module Valise
114
135
  return visited
115
136
  end
116
137
 
138
+ ALL_FILES = PathMatcher.build("**")
139
+ def files(&block)
140
+ glob(ALL_FILES, &block)
141
+ end
142
+
117
143
  def not_above(root)
118
144
  index = @search_roots.index(root)
119
145
  raise Errors::RootNotInSet if index.nil?
@@ -130,6 +156,10 @@ module Valise
130
156
  set
131
157
  end
132
158
 
159
+ def depth_of(root)
160
+ return @search_roots.index(root)
161
+ end
162
+
133
163
  def [](index)
134
164
  return @search_roots[index]
135
165
  end
data/lib/valise/stack.rb CHANGED
@@ -132,7 +132,7 @@ module Valise
132
132
  end
133
133
 
134
134
  def initialize(path, set, merge_class, dump_load)
135
- @segments = unpath(path)
135
+ @segments = collapse(unpath(path))
136
136
  @valise = set
137
137
  @merge_diff = (merge_class || MergeDiff::TopMost).new(self)
138
138
  @dump_load = dump_load
@@ -144,6 +144,10 @@ module Valise
144
144
  @merge_diff.merge(item)
145
145
  end
146
146
 
147
+ def rel_path
148
+ repath(@segments)
149
+ end
150
+
147
151
  def diffed(item, value)
148
152
  @merge_diff.diff(item, value)
149
153
  end
@@ -156,6 +160,10 @@ module Valise
156
160
  Stack.new(@segments, @valise.below(item.root), @merge_diff.class, @dump_load)
157
161
  end
158
162
 
163
+ def depth_of(item)
164
+ @valise.depth_of(item.root)
165
+ end
166
+
159
167
  def each
160
168
  @valise.each do |root|
161
169
  yield(Item.new(self, root, @dump_load))
data/lib/valise/utils.rb CHANGED
@@ -18,6 +18,10 @@ module Valise
18
18
  end
19
19
 
20
20
  module Unpath
21
+ def string_to_segments(string)
22
+ string.split(::File::Separator)
23
+ end
24
+
21
25
  def unpath(parts)
22
26
  if Array === parts and parts.length == 1
23
27
  parts = parts[0]
@@ -26,14 +30,14 @@ module Valise
26
30
  case parts
27
31
  when Array
28
32
  if (parts.find{|part| not (String === part or Symbol === part)}.nil?)
29
- parts = parts.map{|part| part.to_s}
33
+ parts = parts.map{|part| string_to_segments(part.to_s)}.flatten
30
34
  else
31
35
  raise ArgumentError, "path must be composed of strings or symbols"
32
36
  end
33
37
  when String
34
- parts = parts.split(::File::Separator)
38
+ parts = string_to_segments(parts)
35
39
  when Symbol
36
- parts = [parts.to_s]
40
+ parts = string_to_segments(parts.to_s)
37
41
  when ::File
38
42
  parts = parts.path
39
43
  parts = parts.split(::File::Separator)
@@ -41,17 +45,27 @@ module Valise
41
45
  raise ArgumentError, "path must be String, Array of Strings or File"
42
46
  end
43
47
 
44
- parts = parts.map do |part|
45
- if /^~/ =~ part
46
- ::File::expand_path(part).split(::File::Separator)
47
- else
48
- part
49
- end
50
- end.flatten
48
+ if /^~/ =~ parts[0]
49
+ parts = ::File::expand_path(parts[0]).split(::File::Separator) + parts[1..-1]
50
+ end
51
51
 
52
52
  return parts
53
53
  end
54
54
 
55
+ def collapse(segments)
56
+ collapsed = []
57
+ segments.each do |segment|
58
+ case segment
59
+ when '.'
60
+ when '..'
61
+ collapsed.pop
62
+ else
63
+ collapsed.push segment
64
+ end
65
+ end
66
+ collapsed
67
+ end
68
+
55
69
  def repath(segments)
56
70
  case segments
57
71
  when Array
data/spec/fileset.rb CHANGED
@@ -160,6 +160,10 @@ describe Valise, " - the unpath method: " do
160
160
  @search_root.unpath("state").should == %w{state}
161
161
  end
162
162
 
163
+ it '["step", "step/step"] => ["step", "step", "step"]' do
164
+ @search_root.unpath(["step", "step/step"]).should == %w{step step step}
165
+ end
166
+
163
167
  it 'a File => #path' do
164
168
  @file = File.new(__FILE__)
165
169
  @path = __FILE__.split("/")
@@ -1,3 +1,5 @@
1
1
  require 'rspec'
2
2
 
3
- #Ungemmer::ungem_gemspec
3
+ RSpec.configure do |config|
4
+ config.backtrace_clean_patterns.delete(/gems/)
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valise
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.7'
4
+ version: '0.8'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-15 00:00:00.000000000 Z
12
+ date: 2012-12-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: corundum
16
- requirement: &88576980 !ruby/object:Gem::Requirement
16
+ requirement: &73826910 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.0.1
21
+ version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *88576980
24
+ version_requirements: *73826910
25
25
  description: ! " Valise provides an API for accessing configuration and data files
26
26
  for your\n application, including the population of default values, and managing
27
27
  search\n paths. Written to encourage a cross-platform approach to maintaining
@@ -194,7 +194,7 @@ rdoc_options:
194
194
  - --main
195
195
  - doc/README
196
196
  - --title
197
- - valise-0.7 RDoc
197
+ - valise-0.8 RDoc
198
198
  require_paths:
199
199
  - lib/
200
200
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -205,7 +205,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
205
205
  version: '0'
206
206
  segments:
207
207
  - 0
208
- hash: -446270865
208
+ hash: 796712559
209
209
  required_rubygems_version: !ruby/object:Gem::Requirement
210
210
  none: false
211
211
  requirements: