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

Decorator

Defined Under Namespace

Classes: Decorator, Definer, ExtensionsDecorator, PrefixesDecorator, StemmedDefiner, TiltTemplateConfiguration

Constant Summary collapse

ALL_FILES =
"**"

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.



17
18
19
20
21
22
# File 'lib/valise/set.rb', line 17

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

Class Method Details

.define(&block) ⇒ Object



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

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

Instance Method Details

#+(other) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/valise/set.rb', line 135

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



215
216
217
# File 'lib/valise/set.rb', line 215

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

#add_handler(segments, serialization_class, merge_diff_class) ⇒ Object



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

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



129
130
131
132
133
# File 'lib/valise/set.rb', line 129

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



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

def add_search_root(search_root)
  search_roots << search_root
end

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



123
124
125
126
127
# File 'lib/valise/set.rb', line 123

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

#below(root) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/valise/set.rb', line 86

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



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

def cached(domain, key)
  @cache.domain(domain)[key] ||= yield
end

#clean_pattern(pattern) ⇒ Object



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

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

#contents(path) ⇒ Object



180
181
182
# File 'lib/valise/set.rb', line 180

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

#default_mappingsObject



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



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

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

#depth_of(root) ⇒ Object



211
212
213
# File 'lib/valise/set.rb', line 211

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

#each(&block) ⇒ Object



184
185
186
# File 'lib/valise/set.rb', line 184

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

#exts(*extensions) ⇒ Object



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

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

#files(&block) ⇒ Object



207
208
209
# File 'lib/valise/set.rb', line 207

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

#filter(glob = nil, flags = nil) {|filter| ... } ⇒ Object

Yields:



188
189
190
191
192
193
194
195
196
# File 'lib/valise/set.rb', line 188

def filter(glob = nil, flags = nil)
  filter = SetFilter.new
  filter.set = self
  unless glob.nil?
    filter.include(glob, flags)
  end
  yield filter if block_given?
  return filter
end

#find(path) ⇒ Object



176
177
178
# File 'lib/valise/set.rb', line 176

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

#get(path) ⇒ Object



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

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

#glob(path_matcher, &block) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/valise/set.rb', line 198

def glob(path_matcher, &block)
  unless block_given?
    return self.enum_for(:glob, path_matcher)
  end

  filter(path_matcher).files(&block)
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_listsObject



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

def handler_lists
  [merge_diff, serialization]
end

#inspectObject



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

def inspect
  search_roots.inspect
end

#merge_diff_for(stack) ⇒ Object



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

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



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

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

#not_above(root) ⇒ Object



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

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



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

def pfxs(*prefixes)
  pfxs = PrefixesDecorator.new(self)
  pfxs.prefixes = prefixes
  return pfxs
end

#populate(to = self) ⇒ Object



219
220
221
222
223
224
225
226
227
228
# File 'lib/valise/set.rb', line 219

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



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

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

#reverseObject



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

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

#serialization_for(stack) ⇒ Object



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

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

#stemmed(path) ⇒ Object



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

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



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

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



28
29
30
# File 'lib/valise/set.rb', line 28

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

#transformObject



44
45
46
47
48
49
50
# File 'lib/valise/set.rb', line 44

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