Class: Valise::Set
- Inherits:
-
Object
show all
- Includes:
- Enumerable, 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: Decorator, Definer, ExtensionsDecorator, PrefixesDecorator, StemmedDefiner, TiltTemplateConfiguration
Constant Summary
collapse
- ALL_FILES =
PathMatcher.build("**")
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Unpath
#clean_pathname, #containing_workspace, #current_directory, #file_from_backtrace, #from_here, #make_pathname, #starting_directory, #up_to, #up_until
Constructor Details
#initialize ⇒ Set
Returns a new instance of Set.
Class Method Details
.define(&block) ⇒ Object
99
100
101
|
# File 'lib/valise/set.rb', line 99
def self.define(&block)
return self.new.define(&block)
end
|
Instance Method Details
#+(other) ⇒ Object
134
135
136
137
138
139
140
|
# File 'lib/valise/set.rb', line 134
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
217
218
219
|
# File 'lib/valise/set.rb', line 217
def [](index)
return search_roots[index]
end
|
#add_handler(segments, serialization_class, merge_diff_class) ⇒ Object
116
117
118
119
120
|
# File 'lib/valise/set.rb', line 116
def add_handler(segments, serialization_class, merge_diff_class)
segments = clean_pattern(segments)
add_serialization_handler(segments, serialization_class)
add_merge_handler(segments, merge_diff_class)
end
|
#add_merge_handler(pattern, merger, options = nil) ⇒ Object
128
129
130
131
132
|
# File 'lib/valise/set.rb', line 128
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
107
108
109
|
# File 'lib/valise/set.rb', line 107
def add_search_root(search_root)
search_roots << search_root
end
|
#add_serialization_handler(pattern, serializer, options = nil) ⇒ Object
122
123
124
125
126
|
# File 'lib/valise/set.rb', line 122
def add_serialization_handler(pattern, serializer, options = nil)
return if serializer.nil?
Strategies::Serialization.check!(serializer)
serialization[pattern] = [serializer, options]
end
|
#below(root) ⇒ Object
85
86
87
88
89
90
91
|
# File 'lib/valise/set.rb', line 85
def below(root)
index = search_roots.index(root)
raise Errors::RootNotInSet if index.nil?
transform do |roots|
roots[(index+1)..-1]
end
end
|
#cached(domain, key) ⇒ Object
167
168
169
|
# File 'lib/valise/set.rb', line 167
def cached(domain, key)
@cache.domain(domain)[key] ||= yield
end
|
#clean_pattern(pattern) ⇒ Object
111
112
113
114
|
# File 'lib/valise/set.rb', line 111
def clean_pattern(pattern)
pattern.sub(%r{^[*][*]/[*]}, '**')
end
|
#contents(path) ⇒ Object
179
180
181
|
# File 'lib/valise/set.rb', line 179
def contents(path)
find(path).contents
end
|
#default_mappings ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/valise/adapters/tilt.rb', line 79
def default_mappings
if ::Tilt.respond_to? :default_mapping
mapping = ::Tilt.default_mapping
mapping.template_map.merge(mapping.lazy_map).keys
else
::Tilt.mappings.keys
end
rescue => ex
warn "Couldn't access Tilt's default template mappings"
warn " The specific error was #{ex}"
warn " Falling back to an *empty* template list"
warn " To add by hand, change #templates to #handle_template{|cfg| cfg.add_type('ext') }"
[]
end
|
#define(&block) ⇒ Object
93
94
95
96
97
|
# File 'lib/valise/set.rb', line 93
def define(&block)
definer = Definer.new(self)
definer.instance_eval(&block)
return self
end
|
#depth_of(root) ⇒ Object
213
214
215
|
# File 'lib/valise/set.rb', line 213
def depth_of(root)
return search_roots.index(root)
end
|
#each(&block) ⇒ Object
183
184
185
|
# File 'lib/valise/set.rb', line 183
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
209
210
211
|
# File 'lib/valise/set.rb', line 209
def files(&block)
glob(ALL_FILES, &block)
end
|
#find(path) ⇒ Object
175
176
177
|
# File 'lib/valise/set.rb', line 175
def find(path)
get(path).find
end
|
#get(path) ⇒ Object
171
172
173
|
# File 'lib/valise/set.rb', line 171
def get(path)
Stack.new(path, self)
end
|
#glob(path_matcher) ⇒ Object
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/valise/set.rb', line 187
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
|
#handle_templates(&block) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/valise/adapters/tilt.rb', line 67
def handle_templates(&block)
config = TiltTemplateConfiguration.new
if block.arity == 1
yield config
else
config.instance_eval(&block)
end
config.apply(self)
end
|
#handler_lists ⇒ Object
163
164
165
|
# File 'lib/valise/set.rb', line 163
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
147
148
149
150
151
|
# File 'lib/valise/set.rb', line 147
def merge_diff_for(stack)
type, options = *(merge_diff[make_pathname(stack.segments)] || [])
options = (options || {}).merge(:stack => stack)
Strategies::MergeDiff.instance(type, options)
end
|
#merge_handlers(new_merge_diff, new_serialization) ⇒ Object
158
159
160
161
|
# File 'lib/valise/set.rb', line 158
def merge_handlers(new_merge_diff, new_serialization)
merge_diff.merge!(new_merge_diff)
serialization.merge!(new_serialization)
end
|
#not_above(root) ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/valise/set.rb', line 77
def not_above(root)
index = search_roots.index(root)
raise Errors::RootNotInSet if index.nil?
transform do |roots|
roots[index..-1]
end
end
|
#pfxs(*prefixes) ⇒ Object
37
38
39
40
41
|
# File 'lib/valise/set.rb', line 37
def pfxs(*prefixes)
pfxs = PrefixesDecorator.new(self)
pfxs.prefixes = prefixes
return pfxs
end
|
#populate(to = self) ⇒ Object
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/valise/set.rb', line 221
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
103
104
105
|
# File 'lib/valise/set.rb', line 103
def prepend_search_root(search_root)
search_roots.unshift(search_root)
end
|
#reverse ⇒ Object
51
52
53
54
55
|
# File 'lib/valise/set.rb', line 51
def reverse
transform do |roots|
roots.reverse
end
end
|
#serialization_for(stack) ⇒ Object
153
154
155
156
|
# File 'lib/valise/set.rb', line 153
def serialization_for(stack)
type, options = *serialization[make_pathname(stack.segments)]
Strategies::Serialization.instance(type, options)
end
|
#stemmed(path) ⇒ Object
68
69
70
71
72
73
74
75
|
# File 'lib/valise/set.rb', line 68
def stemmed(path)
segments = make_pathname(path)
transform do |roots|
roots.map do |root|
StemDecorator.new(segments, root)
end
end
end
|
#sub_set(path) ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/valise/set.rb', line 57
def sub_set(path)
segments = make_pathname(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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/valise/adapters/tilt.rb', line 94
def templates(rel_path=nil)
rel_path ||= "templates"
new_set = self.sub_set(rel_path)
new_set = new_set.pfxs("", "_")
new_set.handle_templates do |config|
default_mappings.each do |mapping|
options = nil
if block_given?
options = yield(mapping)
end
config.add_type(mapping, options)
end
end
end
|
#to_s(joiner = nil) ⇒ Object
27
28
29
|
# File 'lib/valise/set.rb', line 27
def to_s(joiner=nil)
search_roots.map(&:to_s).join(joiner||":")
end
|
43
44
45
46
47
48
49
|
# File 'lib/valise/set.rb', line 43
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
|