Class: Valise::Set

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Debugging, Unpath
Defined in:
lib/valise/set.rb,
lib/valise/set/definer.rb

Defined Under Namespace

Classes: Definer, StemmedDefiner

Constant Summary collapse

ALL_FILES =
PathMatcher.build("**")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Unpath

#collapse, #repath, #string_to_segments, #unpath

Methods included from Debugging

enable, #remark

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

Instance Attribute Details

#merge_diff(path) ⇒ Object

Returns the value of attribute merge_diff.



78
79
80
# File 'lib/valise/set.rb', line 78

def merge_diff
  @merge_diff
end

#serialization(path) ⇒ Object

Returns the value of attribute serialization.



78
79
80
# File 'lib/valise/set.rb', line 78

def serialization
  @serialization
end

Class Method Details

.define(&block) ⇒ Object



53
54
55
# File 'lib/valise/set.rb', line 53

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

Instance Method Details

#+(other) ⇒ Object



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

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



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

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

#add_handler(segments, serialization_class, merge_diff_class) ⇒ Object



65
66
67
68
# File 'lib/valise/set.rb', line 65

def add_handler(segments, serialization_class, merge_diff_class)
  @merge_diff[segments] = merge_diff_class unless merge_diff_class.nil?
  @serialization[segments] = serialization_class unless serialization_class.nil?
end

#add_search_root(search_root) ⇒ Object



61
62
63
# File 'lib/valise/set.rb', line 61

def add_search_root(search_root)
  @search_roots << search_root
end

#below(root) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/valise/set.rb', line 151

def below(root)
  index = @search_roots.index(root)
  raise Errors::RootNotInSet if index.nil?
  set = self.class.new
  set.search_roots = @search_roots[(index+1)..-1]
  set
end

#define(&block) ⇒ Object



47
48
49
50
51
# File 'lib/valise/set.rb', line 47

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

#depth_of(root) ⇒ Object



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

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

#each(&block) ⇒ Object



113
114
115
# File 'lib/valise/set.rb', line 113

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

#files(&block) ⇒ Object



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

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

#find(path) ⇒ Object

Raises:



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

def find(path)
  item = get(path).present.first
  return item unless item.nil?
  raise Errors::NotFound, "#{path} not found in #{@search_roots.inspect}"
end

#get(path) ⇒ Object



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

def get(path)
  return Stack.new(path, self,
                   merge_diff(path),
                   serialization(path))
end

#glob(path_matcher) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/valise/set.rb', line 117

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



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

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_handlers(merge_diff, serialization) ⇒ Object



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

def merge_handlers(merge_diff, serialization)
  @merge_diff.merge!(merge_diff)
  @serialization.merge!(serialization)
end

#not_above(root) ⇒ Object



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

def not_above(root)
  index = @search_roots.index(root)
  raise Errors::RootNotInSet if index.nil?
  set = self.class.new
  set.search_roots = @search_roots[index..-1]
  set
end

#populate(to = self) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/valise/set.rb', line 167

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



57
58
59
# File 'lib/valise/set.rb', line 57

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

#reverseObject



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

def reverse
  set = Set.new
  set.search_roots = @search_roots.reverse
  set.merge_diff = @merge_diff.dup
  set.serialization = @serialization.dup
  set
end

#sub_set(path) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/valise/set.rb', line 37

def sub_set(path)
  set = Set.new
  set.search_roots = @search_roots.map do |root|
    SearchRoot.new(root.segments + unpath(path))
  end
  set.merge_diff = @merge_diff.dup
  set.serialization = @serialization.dup
  set
end

#to_sObject



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

def to_s
  @search_roots.map(&:to_s).join(":")
end