Class: Valise::Set

Inherits:
Object
  • 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

Direct Known Subclasses

ExtensionsDecorator

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

#clean_pathname, #containing_workspace, #current_directory, #file_from_backtrace, #from_here, #make_pathname, #starting_directory, #up_to, #up_until

Constructor Details

#initializeSet

Returns a new instance of Set.



15
16
17
18
19
# File 'lib/valise/set.rb', line 15

def initialize
  @search_roots = []
  @merge_diff = PathMatcher.new
  @serialization = PathMatcher.new
end

Class Method Details

.define(&block) ⇒ Object



91
92
93
# File 'lib/valise/set.rb', line 91

def self.define(&block)
  return self.new.define(&block)
end

Instance Method Details

#+(other) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/valise/set.rb', line 126

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



205
206
207
# File 'lib/valise/set.rb', line 205

def [](index)
  return search_roots[index]
end

#add_handler(segments, serialization_class, merge_diff_class) ⇒ Object



108
109
110
111
112
# File 'lib/valise/set.rb', line 108

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



120
121
122
123
124
# File 'lib/valise/set.rb', line 120

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



99
100
101
# File 'lib/valise/set.rb', line 99

def add_search_root(search_root)
  search_roots << search_root
end

#add_serialization_handler(pattern, serializer, options = nil) ⇒ Object



114
115
116
117
118
# File 'lib/valise/set.rb', line 114

def add_serialization_handler(pattern, serializer, options = nil)
  return if serializer.nil?
  Strategies::Serialization.check!(serializer)
  serialization[pattern] = [serializer, options]
end

#below(root) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/valise/set.rb', line 77

def below(root)
  index = search_roots.index(root)
  raise Errors::RootNotInSet if index.nil?
  transform do |roots|
    roots[(index+1)..-1]
  end
end

#clean_pattern(pattern) ⇒ Object



103
104
105
106
# File 'lib/valise/set.rb', line 103

def clean_pattern(pattern)
  #deprecation warning maybe
  pattern.sub(%r{^[*][*]/[*]}, '**')
end

#contents(path) ⇒ Object



167
168
169
# File 'lib/valise/set.rb', line 167

def contents(path)
  find(path).contents
end

#define(&block) ⇒ Object



85
86
87
88
89
# File 'lib/valise/set.rb', line 85

def define(&block)
  definer = Definer.new(self)
  definer.instance_eval(&block)
  return self
end

#depth_of(root) ⇒ Object



201
202
203
# File 'lib/valise/set.rb', line 201

def depth_of(root)
  return search_roots.index(root)
end

#each(&block) ⇒ Object



171
172
173
# File 'lib/valise/set.rb', line 171

def each(&block)
  search_roots.each(&block)
end

#exts(*extensions) ⇒ Object



29
30
31
32
33
# File 'lib/valise/set.rb', line 29

def exts(*extensions)
  exts = ExtensionsDecorator.new(self)
  exts.extensions = extensions
  return exts
end

#files(&block) ⇒ Object



197
198
199
# File 'lib/valise/set.rb', line 197

def files(&block)
  glob(ALL_FILES, &block)
end

#find(path) ⇒ Object



163
164
165
# File 'lib/valise/set.rb', line 163

def find(path)
  get(path).find
end

#get(path) ⇒ Object



159
160
161
# File 'lib/valise/set.rb', line 159

def get(path)
  Stack.new(path, self)
end

#glob(path_matcher) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/valise/set.rb', line 175

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_listsObject



155
156
157
# File 'lib/valise/set.rb', line 155

def handler_lists
  [merge_diff, serialization]
end

#inspectObject



21
22
23
# File 'lib/valise/set.rb', line 21

def inspect
  search_roots.inspect
end

#merge_diff_for(stack) ⇒ Object



139
140
141
142
143
# File 'lib/valise/set.rb', line 139

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



150
151
152
153
# File 'lib/valise/set.rb', line 150

def merge_handlers(new_merge_diff, new_serialization)
  merge_diff.merge!(new_merge_diff)
  serialization.merge!(new_serialization)
end

#not_above(root) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/valise/set.rb', line 69

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



209
210
211
212
213
214
215
216
217
218
# File 'lib/valise/set.rb', line 209

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



95
96
97
# File 'lib/valise/set.rb', line 95

def prepend_search_root(search_root)
  search_roots.unshift(search_root)
end

#reverseObject



43
44
45
46
47
# File 'lib/valise/set.rb', line 43

def reverse
  transform do |roots|
    roots.reverse
  end
end

#serialization_for(stack) ⇒ Object



145
146
147
148
# File 'lib/valise/set.rb', line 145

def serialization_for(stack)
  type, options = *serialization[make_pathname(stack.segments)]
  Strategies::Serialization.instance(type, options)
end

#stemmed(path) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/valise/set.rb', line 60

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



49
50
51
52
53
54
55
56
57
58
# File 'lib/valise/set.rb', line 49

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



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(joiner = nil) ⇒ Object



25
26
27
# File 'lib/valise/set.rb', line 25

def to_s(joiner=nil)
  search_roots.map(&:to_s).join(joiner||":")
end

#transformObject



35
36
37
38
39
40
41
# File 'lib/valise/set.rb', line 35

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