Class: Valise::Set
- Inherits:
-
Object
show all
- Includes:
- Enumerable, Debugging, Unpath
- Defined in:
- lib/valise/set.rb,
lib/valise/set/definer.rb,
lib/valise/adapters/tilt.rb,
lib/valise/set/extensions-decorator.rb
Defined Under Namespace
Classes: Definer, ExtensionsDecorator, StemmedDefiner
Constant Summary
collapse
- ALL_FILES =
PathMatcher.build("**")
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Unpath
#collapse, file_from_backtrace, #file_from_backtrace, #from_here, from_here, repath, string_to_segments, unpath, up_to, #up_to
Methods included from Debugging
enable, #remark
Constructor Details
#initialize ⇒ Set
Returns a new instance of Set.
17
18
19
20
21
|
# File 'lib/valise/set.rb', line 17
def initialize
@search_roots = []
@merge_diff = PathMatcher.new
@serialization = PathMatcher.new
end
|
Class Method Details
.define(&block) ⇒ Object
93
94
95
|
# File 'lib/valise/set.rb', line 93
def self.define(&block)
return self.new.define(&block)
end
|
Instance Method Details
#+(other) ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/valise/set.rb', line 122
def +(other)
result = self.class.new
result.search_roots = search_roots + other.search_roots
result.merge_handlers(*other.handler_lists)
result.merge_handlers(*handler_lists)
return result
end
|
#[](index) ⇒ Object
197
198
199
|
# File 'lib/valise/set.rb', line 197
def [](index)
return search_roots[index]
end
|
#add_handler(segments, serialization_class, merge_diff_class) ⇒ Object
105
106
107
108
|
# File 'lib/valise/set.rb', line 105
def add_handler(segments, serialization_class, merge_diff_class)
add_serialization_handler(segments, serialization_class)
add_merge_handler(segments, merge_diff_class)
end
|
#add_merge_handler(pattern, merger, options = nil) ⇒ Object
116
117
118
119
120
|
# File 'lib/valise/set.rb', line 116
def add_merge_handler(pattern, merger, options = nil)
return if merger.nil?
Strategies::MergeDiff.check!(merger)
merge_diff[pattern] = [merger, options]
end
|
#add_search_root(search_root) ⇒ Object
101
102
103
|
# File 'lib/valise/set.rb', line 101
def add_search_root(search_root)
search_roots << search_root
end
|
#add_serialization_handler(pattern, serializer, options = nil) ⇒ Object
110
111
112
113
114
|
# File 'lib/valise/set.rb', line 110
def add_serialization_handler(pattern, serializer, options = nil)
return if serializer.nil?
Strategies::Serialization.check!(serializer)
serialization[pattern] = [serializer, options]
end
|
#below(root) ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/valise/set.rb', line 79
def below(root)
index = search_roots.index(root)
raise Errors::RootNotInSet if index.nil?
transform do |roots|
roots[(index+1)..-1]
end
end
|
#define(&block) ⇒ Object
87
88
89
90
91
|
# File 'lib/valise/set.rb', line 87
def define(&block)
definer = Definer.new(self)
definer.instance_eval(&block)
return self
end
|
#depth_of(root) ⇒ Object
193
194
195
|
# File 'lib/valise/set.rb', line 193
def depth_of(root)
return search_roots.index(root)
end
|
#each(&block) ⇒ Object
163
164
165
|
# File 'lib/valise/set.rb', line 163
def each(&block)
search_roots.each(&block)
end
|
#exts(*extensions) ⇒ Object
31
32
33
34
35
|
# File 'lib/valise/set.rb', line 31
def exts(*extensions)
exts = ExtensionsDecorator.new(self)
exts.extensions = extensions
return exts
end
|
#files(&block) ⇒ Object
189
190
191
|
# File 'lib/valise/set.rb', line 189
def files(&block)
glob(ALL_FILES, &block)
end
|
#find(path) ⇒ Object
159
160
161
|
# File 'lib/valise/set.rb', line 159
def find(path)
get(path).find
end
|
#get(path) ⇒ Object
155
156
157
|
# File 'lib/valise/set.rb', line 155
def get(path)
Stack.new(path, self)
end
|
#glob(path_matcher) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/valise/set.rb', line 167
def glob(path_matcher)
unless block_given?
return self.enum_for(:glob, path_matcher)
end
visited = {}
path_matcher = PathMatcher.build(path_matcher)
search_roots.each do |root|
root.each do |segments|
next unless path_matcher === segments
unless visited.has_key?(segments)
item = get(segments).present.first
visited[segments] = item
yield(item)
end
end
end
return visited
end
|
#handler_lists ⇒ Object
151
152
153
|
# File 'lib/valise/set.rb', line 151
def handler_lists
[merge_diff, serialization]
end
|
#inspect ⇒ Object
23
24
25
|
# File 'lib/valise/set.rb', line 23
def inspect
search_roots.inspect
end
|
#merge_diff_for(stack) ⇒ Object
135
136
137
138
139
|
# File 'lib/valise/set.rb', line 135
def merge_diff_for(stack)
type, options = *(merge_diff[unpath(stack.segments)] || [])
options = (options || {}).merge(:stack => stack)
Strategies::MergeDiff.instance(type, options)
end
|
#merge_handlers(new_merge_diff, new_serialization) ⇒ Object
146
147
148
149
|
# File 'lib/valise/set.rb', line 146
def merge_handlers(new_merge_diff, new_serialization)
merge_diff.merge!(new_merge_diff)
serialization.merge!(new_serialization)
end
|
#not_above(root) ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/valise/set.rb', line 71
def not_above(root)
index = search_roots.index(root)
raise Errors::RootNotInSet if index.nil?
transform do |roots|
roots[index..-1]
end
end
|
#populate(to = self) ⇒ Object
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/valise/set.rb', line 201
def populate(to = self)
files do |item|
contents = item.contents
to_stack = to.get(item.segments)
to_stack = yield(to_stack) if block_given?
target = to_stack.writable.first
next if target.present?
target.contents = contents
end
end
|
#prepend_search_root(search_root) ⇒ Object
97
98
99
|
# File 'lib/valise/set.rb', line 97
def prepend_search_root(search_root)
search_roots.unshift(search_root)
end
|
#reverse ⇒ Object
45
46
47
48
49
|
# File 'lib/valise/set.rb', line 45
def reverse
transform do |roots|
roots.reverse
end
end
|
#serialization_for(stack) ⇒ Object
141
142
143
144
|
# File 'lib/valise/set.rb', line 141
def serialization_for(stack)
type, options = *serialization[unpath(stack.segments)]
Strategies::Serialization.instance(type, options)
end
|
#stemmed(path) ⇒ Object
62
63
64
65
66
67
68
69
|
# File 'lib/valise/set.rb', line 62
def stemmed(path)
segments = unpath(path)
transform do |roots|
roots.map do |root|
StemDecorator.new(segments, root)
end
end
end
|
#sub_set(path) ⇒ Object
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/valise/set.rb', line 51
def sub_set(path)
segments = unpath(path)
transform do |roots|
roots.map do |root|
new_root = root.dup
new_root.segments += segments
new_root
end
end
end
|
#templates(rel_path = nil) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/valise/adapters/tilt.rb', line 33
def templates(rel_path=nil)
rel_path ||= "templates"
new_set = self.sub_set(rel_path)
new_set = new_set.exts(*([""] + Tilt.mappings.map{|mapping, _| "." + mapping}))
::Tilt.mappings.each do |mapping, _|
options = nil
if block_given?
options = yield(mapping)
end
new_set.add_serialization_handler("**/*.#{mapping}", :tilt, options)
end
new_set
end
|
#to_s ⇒ Object
27
28
29
|
# File 'lib/valise/set.rb', line 27
def to_s
search_roots.map(&:to_s).join(":")
end
|
37
38
39
40
41
42
43
|
# File 'lib/valise/set.rb', line 37
def transform
set = self.class.new
set.search_roots = yield search_roots
set.merge_diff = merge_diff.dup
set.serialization = serialization.dup
return set
end
|