valise 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: adb87f4f5568fa4acb5ffda1cb35bc7d11c3786f
4
+ data.tar.gz: f070549c2c75bef9a0996634f6a2ed7ab1b9f1f3
5
+ SHA512:
6
+ metadata.gz: c84e48d6b133094c9a9592af7b83d9595aba2ca90f191884e3a454b15b179e049115ac071d2b52307d03c5c227f20412031270d9c2894d0ea607dbae04a15fa5
7
+ data.tar.gz: 97822fbf48bb4ae92e139f7e9a842d596e1337357a91f8e04f984eebe4a4b3f96dd4bafaec457e1eeb20ff76d6e3b122b34cad5ec1eb5ba7b0407ed2fb82a3a4
@@ -30,18 +30,65 @@ module Valise
30
30
  end
31
31
 
32
32
  class Set
33
+ class TiltTemplateConfiguration
34
+ def initialize
35
+ @template_types = {}
36
+ @plain_files = true
37
+ end
38
+
39
+ attr_reader :template_types
40
+ attr_accessor :plain_files
41
+
42
+ def add_type(type, options)
43
+ template_types[type] = options
44
+ end
45
+
46
+ def extensions
47
+ template_types.keys.map{|type| ".#{type}"} + (@plain_files ? [""] : [])
48
+ end
49
+
50
+ def add_extenstions(set)
51
+ set.exts(*extensions)
52
+ end
53
+
54
+ def add_serializations(set)
55
+ template_types.each do |mapping, options|
56
+ set.add_serialization_handler("**.#{mapping}", :tilt, options)
57
+ end
58
+ end
59
+
60
+ def apply(set)
61
+ set = add_extenstions(set)
62
+ add_serializations(set)
63
+ set
64
+ end
65
+ end
66
+
67
+ def handle_templates(&block)
68
+ config = TiltTemplateConfiguration.new
69
+
70
+ if block.arity == 1
71
+ yield config
72
+ else
73
+ config.instance_eval(&block)
74
+ end
75
+
76
+ config.apply(self)
77
+ end
78
+
33
79
  def templates(rel_path=nil)
34
80
  rel_path ||= "templates"
35
81
  new_set = self.sub_set(rel_path)
36
- new_set = new_set.exts(*([""] + Tilt.mappings.map{|mapping, _| "." + mapping}))
37
- ::Tilt.mappings.each do |mapping, _|
38
- options = nil
39
- if block_given?
40
- options = yield(mapping)
82
+ new_set = new_set.pfxs("", "_")
83
+ new_set.handle_templates do |config|
84
+ ::Tilt.mappings.each do |mapping, _|
85
+ options = nil
86
+ if block_given?
87
+ options = yield(mapping)
88
+ end
89
+ config.add_type(mapping, options)
41
90
  end
42
- new_set.add_serialization_handler("**.#{mapping}", :tilt, options)
43
91
  end
44
- new_set
45
92
  end
46
93
  end
47
94
  end
@@ -0,0 +1,49 @@
1
+ require 'weakref'
2
+
3
+ module Valise
4
+ class Cache
5
+ class Store
6
+ def initialize
7
+ @hash = {}
8
+ end
9
+
10
+ def []=(key, object)
11
+ @hash[key] = WeakRef.new(object)
12
+ end
13
+
14
+ def [](key)
15
+ ref = @hash.fetch(key) do
16
+ return nil
17
+ end
18
+ return live_ref(key, ref)
19
+ rescue WeakRef::RefError
20
+ nil
21
+ end
22
+
23
+ def live_ref(key, ref)
24
+ ref.__getobj__
25
+ rescue WeakRef::RefError
26
+ @hash.delete(key)
27
+ raise
28
+ end
29
+
30
+ def has_key?(key)
31
+ ref = @hash.fetch(key) do
32
+ false
33
+ end
34
+ live_ref(key, ref)
35
+ true
36
+ rescue WeakRef::RefError
37
+ false
38
+ end
39
+ end
40
+
41
+ def initialize
42
+ @stores = Hash.new{|h,k| h[k] = Store.new}
43
+ end
44
+
45
+ def domain(key)
46
+ @stores[key]
47
+ end
48
+ end
49
+ end
@@ -2,19 +2,14 @@ require 'valise/set'
2
2
 
3
3
  module Valise
4
4
  class Set
5
- class ExtensionsDecorator < Set
5
+ class Decorator < Set
6
6
  def initialize(set)
7
7
  @set = set
8
- @extensions = []
9
8
  end
10
- attr_accessor :extensions
9
+
11
10
  attr_reader :set
12
11
  protected :set
13
12
 
14
- def inspect
15
- super + "x#{extensions.inspect}"
16
- end
17
-
18
13
  def search_roots
19
14
  set.search_roots
20
15
  end
@@ -26,6 +21,34 @@ module Valise
26
21
  def serialization
27
22
  set.serialization
28
23
  end
24
+ end
25
+
26
+ class PrefixesDecorator < Decorator
27
+ def initialize(set)
28
+ super
29
+ @prefixes = []
30
+ end
31
+ attr_accessor :prefixes
32
+
33
+ def inspect
34
+ "#{prefixes.inspect}x#{set.inspect}"
35
+ end
36
+
37
+ def get(path)
38
+ set.get(path).exts(*prefixes)
39
+ end
40
+ end
41
+
42
+ class ExtensionsDecorator < Decorator
43
+ def initialize(set)
44
+ super
45
+ @extensions = []
46
+ end
47
+ attr_accessor :extensions
48
+
49
+ def inspect
50
+ "#{set.inspect}x#{extensions.inspect}"
51
+ end
29
52
 
30
53
  def get(path)
31
54
  set.get(path).exts(*extensions)
data/lib/valise/set.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'valise/cache'
1
2
  require 'valise/errors'
2
3
  require 'valise/search-root'
3
4
  require 'valise/utils'
@@ -16,6 +17,7 @@ module Valise
16
17
  @search_roots = []
17
18
  @merge_diff = PathMatcher.new
18
19
  @serialization = PathMatcher.new
20
+ @cache = Cache.new
19
21
  end
20
22
 
21
23
  def inspect
@@ -32,6 +34,12 @@ module Valise
32
34
  return exts
33
35
  end
34
36
 
37
+ def pfxs(*prefixes)
38
+ pfxs = PrefixesDecorator.new(self)
39
+ pfxs.prefixes = prefixes
40
+ return pfxs
41
+ end
42
+
35
43
  def transform
36
44
  set = self.class.new
37
45
  set.search_roots = yield search_roots
@@ -156,6 +164,10 @@ module Valise
156
164
  [merge_diff, serialization]
157
165
  end
158
166
 
167
+ def cached(domain, key)
168
+ @cache.domain(domain)[key] ||= yield
169
+ end
170
+
159
171
  def get(path)
160
172
  Stack.new(path, self)
161
173
  end
@@ -0,0 +1,37 @@
1
+ require 'valise/stack'
2
+
3
+ module Valise
4
+ class Stack
5
+ class Decorator < Stack
6
+ def initialize(stack)
7
+ @stack = stack
8
+ @stacks = Hash.new do |h,segments|
9
+ h[segments] = @stack.valise.get(segments)
10
+ end
11
+ end
12
+
13
+ def valise
14
+ @stack.valise
15
+ end
16
+
17
+ def merged(item)
18
+ item.stack.merged(item)
19
+ end
20
+
21
+ def diffed(item, value)
22
+ item.stack.diffed(item, value)
23
+ end
24
+
25
+ def rel_path
26
+ @stack.rel_path
27
+ end
28
+
29
+ def each
30
+ return enum_for(:each) unless block_given?
31
+ @stack.each do |item|
32
+ decorate_item(item){|decorated| yield(decorated)}
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,24 +1,16 @@
1
- require 'valise/stack'
2
-
1
+ require 'valise/stack/decorator'
3
2
  module Valise
4
3
  class Stack
5
- class ExtensionsDecorator < Stack
4
+ class ExtensionsDecorator < Decorator
6
5
  def initialize(stack)
7
- @stack = stack
6
+ super
8
7
  @extensions = []
9
- @stacks = Hash.new do |h,segments|
10
- h[segments] = @stack.valise.get(segments)
11
- end
12
8
  end
13
9
 
14
10
  attr_accessor :extensions
15
11
 
16
12
  def inspect
17
- @stack.inspect + "x#{extensions.inspect}"
18
- end
19
-
20
- def valise
21
- @stack.valise
13
+ @stack.inspect + "xS#{extensions.inspect}"
22
14
  end
23
15
 
24
16
  def reget(root)
@@ -27,26 +19,11 @@ module Valise
27
19
  decorated
28
20
  end
29
21
 
30
- def merged(item)
31
- item.stack.merged(item)
32
- end
33
-
34
- def diffed(item, value)
35
- item.stack.diffed(item, value)
36
- end
37
-
38
- def rel_path
39
- @stack.rel_path
40
- end
41
-
42
- def each
43
- return enum_for(:each) unless block_given?
44
- @stack.each do |item|
45
- @extensions.each do |ext|
46
- dir, file = *item.segments.split
47
- ext_stack = @stacks[dir + (file.to_s + ext)]
48
- yield(ext_stack.item_for(item.root))
49
- end
22
+ def decorate_item(item)
23
+ dir, file = *item.segments.split
24
+ @extensions.each do |ext|
25
+ ext_stack = @stacks[dir + (file.to_s + ext)]
26
+ yield(ext_stack.item_for(item.root))
50
27
  end
51
28
  end
52
29
  end
@@ -0,0 +1,31 @@
1
+ require 'valise/stack/decorator'
2
+ module Valise
3
+ class Stack
4
+ class PrefixesDecorator < Decorator
5
+ def initialize(stack)
6
+ super
7
+ @prefixes = []
8
+ end
9
+
10
+ attr_accessor :prefixes
11
+
12
+ def inspect
13
+ "P#{prefixes.inspect}x#{@stack.inspect}"
14
+ end
15
+
16
+ def reget(root)
17
+ decorated = self.new(super)
18
+ decorated.prefixes = self.prefixes
19
+ decorated
20
+ end
21
+
22
+ def decorate_item(item)
23
+ dir, file = *item.segments.split
24
+ @prefixes.each do |pfx|
25
+ dec_stack = @stacks[dir + (pfx + file.to_s)]
26
+ yield(dec_stack.item_for(item.root))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/valise/stack.rb CHANGED
@@ -2,6 +2,7 @@ require 'valise/utils'
2
2
  require 'valise/item-enum'
3
3
  require 'valise/strategies/merge-diff'
4
4
  require 'valise/stack/extensions-decorator'
5
+ require 'valise/stack/prefixes-decorator'
5
6
 
6
7
  module Valise
7
8
  class Stack
@@ -55,10 +56,16 @@ module Valise
55
56
  valise.depth_of(item.root)
56
57
  end
57
58
 
58
- def find
59
+ def raw_find
59
60
  item = present.first
60
61
  return item unless item.nil?
61
- raise Errors::NotFound, "'#{rel_path}' not found in #{valise.inspect}"
62
+ raise Errors::NotFound, "#{self.inspect} not found"
63
+ end
64
+
65
+ def find
66
+ valise.cached(:find, rel_path) do
67
+ raw_find
68
+ end
62
69
  end
63
70
 
64
71
  def exts(*extensions)
@@ -67,6 +74,12 @@ module Valise
67
74
  return exts
68
75
  end
69
76
 
77
+ def pfxs(*prefixes)
78
+ pfxs = PrefixesDecorator.new(self)
79
+ pfxs.prefixes = prefixes
80
+ return pfxs
81
+ end
82
+
70
83
  def reget(root)
71
84
  root.get(@segments)
72
85
  end
@@ -2,7 +2,7 @@ require 'rspec'
2
2
  #require 'cadre/rspec'
3
3
 
4
4
  RSpec.configure do |config|
5
- config.backtrace_clean_patterns.delete(/gems/)
5
+ config.backtrace_exclusion_patterns.delete(/gems/)
6
6
  config.run_all_when_everything_filtered = true
7
7
  # config.add_formatter(Cadre::RSpec::NotifyOnCompleteFormatter)
8
8
  # config.add_formatter(Cadre::RSpec::QuickfixFormatter)
metadata CHANGED
@@ -1,36 +1,34 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valise
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 1.0.0
4
+ version: 1.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Judson Lester
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-07-25 00:00:00.000000000 Z
11
+ date: 2013-12-30 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- prerelease: false
16
- type: :development
17
14
  name: corundum
18
- version_requirements: !ruby/object:Gem::Requirement
15
+ requirement: !ruby/object:Gem::Requirement
19
16
  requirements:
20
- - - ! '>='
17
+ - - '>='
21
18
  - !ruby/object:Gem::Version
22
19
  version: '0'
23
- none: false
24
- requirement: !ruby/object:Gem::Requirement
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
25
23
  requirements:
26
- - - ! '>='
24
+ - - '>='
27
25
  - !ruby/object:Gem::Version
28
26
  version: '0'
29
- none: false
30
- description: ! " Valise provides an API for accessing configuration and data files
31
- for your\n application, including the population of default values, and managing
32
- search\n paths. Written to encourage a cross-platform approach to maintaining
33
- configs\n for an application.\n"
27
+ description: |2
28
+ Valise provides an API for accessing configuration and data files for your
29
+ application, including the population of default values, and managing search
30
+ paths. Written to encourage a cross-platform approach to maintaining configs
31
+ for an application.
34
32
  email:
35
33
  - nyarly@gmail.com
36
34
  executables: []
@@ -110,6 +108,7 @@ files:
110
108
  - lib/valise/utils.rb
111
109
  - lib/valise/errors.rb
112
110
  - lib/valise/adapters.rb
111
+ - lib/valise/cache.rb
113
112
  - lib/valise/strategies/set.rb
114
113
  - lib/valise/strategies/merge-diff.rb
115
114
  - lib/valise/strategies/serialization.rb
@@ -120,6 +119,8 @@ files:
120
119
  - lib/valise/set/definer.rb
121
120
  - lib/valise/set/extensions-decorator.rb
122
121
  - lib/valise/stack.rb
122
+ - lib/valise/stack/decorator.rb
123
+ - lib/valise/stack/prefixes-decorator.rb
123
124
  - lib/valise/stack/extensions-decorator.rb
124
125
  - lib/valise/stem-decorator.rb
125
126
  - lib/valise/item.rb
@@ -202,33 +203,29 @@ files:
202
203
  homepage: http://valise.rubyforge.org/
203
204
  licenses:
204
205
  - MIT
206
+ metadata: {}
205
207
  post_install_message:
206
208
  rdoc_options:
207
209
  - --inline-source
208
210
  - --main
209
211
  - doc/README
210
212
  - --title
211
- - valise-1.0.0 RDoc
213
+ - valise-1.1.0 RDoc
212
214
  require_paths:
213
215
  - lib/
214
216
  required_ruby_version: !ruby/object:Gem::Requirement
215
217
  requirements:
216
- - - ! '>='
218
+ - - '>='
217
219
  - !ruby/object:Gem::Version
218
- segments:
219
- - 0
220
- hash: -884203725
221
220
  version: '0'
222
- none: false
223
221
  required_rubygems_version: !ruby/object:Gem::Requirement
224
222
  requirements:
225
- - - ! '>='
223
+ - - '>='
226
224
  - !ruby/object:Gem::Version
227
225
  version: '0'
228
- none: false
229
226
  requirements: []
230
227
  rubyforge_project: valise
231
- rubygems_version: 1.8.24
228
+ rubygems_version: 2.0.3
232
229
  signing_key:
233
230
  specification_version: 3
234
231
  summary: Manage configuration and data files simply