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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Unpath

#repath, #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

Class Method Details

.define(&block) ⇒ Object



37
38
39
# File 'lib/valise/set.rb', line 37

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

Instance Method Details

#+(other) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/valise/set.rb', line 54

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



133
134
135
# File 'lib/valise/set.rb', line 133

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

#add_handler(segments, serialization_class, merge_diff_class) ⇒ Object



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

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



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

def add_search_root(search_root)
  @search_roots << search_root
end

#below(root) ⇒ Object



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

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



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

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

#each(&block) ⇒ Object



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

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

#filesObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/valise/set.rb', line 99

def files
  unless block_given?
    return self.enum_for(:files)
  end

  visited = {}
  @search_roots.each do |root|
    root.each do |segments|
      unless visited.has_key?(segments)
        item = find(segments)
        visited[segments] = item
        yield(item)
      end
    end
  end
  return visited
end

#find(path) ⇒ Object

Raises:



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

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



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

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

#handler_listsObject



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

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(path) ⇒ Object



72
73
74
# File 'lib/valise/set.rb', line 72

def merge_diff(path)
  @merge_diff[unpath(path)]
end

#merge_handlers(merge_diff, serialization) ⇒ Object



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

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

#not_above(root) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/valise/set.rb', line 117

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



137
138
139
140
141
142
143
144
145
146
# File 'lib/valise/set.rb', line 137

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



41
42
43
# File 'lib/valise/set.rb', line 41

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

#reverseObject



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

def reverse
  set = Set.new
  set.search_roots = @search_roots.reverse
  set
end

#serialization(path) ⇒ Object



76
77
78
# File 'lib/valise/set.rb', line 76

def serialization(path)
  @serialization[unpath(path)]
end