middleman-data_source 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/changelog.md +4 -0
- data/lib/middleman/data_source/extension.rb +37 -23
- data/lib/middleman/data_source/version.rb +1 -1
- data/readme.md +31 -1
- data/spec/fixtures/collection/config.rb +30 -2
- data/spec/fixtures/middleware/config.rb +18 -0
- data/spec/lib/middleman/data_source/extension/collection_spec.rb +37 -0
- data/spec/lib/middleman/data_source/extension/middleware_spec.rb +16 -0
- data/spec/lib/middleman/data_source/extension_spec.rb +0 -28
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44c1b70621ff4a70696742d721daea2dffee76ac
|
4
|
+
data.tar.gz: 22cf1ed7c44ba3d5aa5c2c0626b236f05e19a8dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6c6aa484e291c1bba697dedd88410f3289df2a0c7d4194caf34e0cd5f28965ace6b37dbc15ab4fa00f3127963a5b260f3a28c4b2f9d29b49166fbc99a442040
|
7
|
+
data.tar.gz: a61eccd9fe52b00cc0eeecaa63a7656b2340b623c37f571965304c95cbdec7fc33153157a76d0b35fbdfb318bc69f495611c7b725f6247b71a80ced39ffb3dad
|
data/changelog.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
# 0.8.0
|
2
|
+
- add feature for passing `middleware:` option with sources to alter incoming data
|
3
|
+
- allow passing `index:` option to collections to define custom index (or `false` for no index)
|
4
|
+
|
1
5
|
# 0.7.1
|
2
6
|
- allow passing data type to collection
|
3
7
|
- fix access to data object in middleman 3.x code branches
|
@@ -11,8 +11,7 @@ module Middleman
|
|
11
11
|
option :decoders, {}, 'callable functions to decode data sources'
|
12
12
|
option :collection, {}, 'group of recursive resources'
|
13
13
|
|
14
|
-
attr_reader :decoders, :sources, :
|
15
|
-
:app_inst
|
14
|
+
attr_reader :decoders, :sources, :app_inst
|
16
15
|
|
17
16
|
def rack_app
|
18
17
|
@_rack_app ||= ::Rack::Test::Session.new( ::Rack::MockSession.new( options.rack_app ) )
|
@@ -20,24 +19,26 @@ module Middleman
|
|
20
19
|
|
21
20
|
def initialize app, options_hash={}, &block
|
22
21
|
super app, options_hash, &block
|
23
|
-
@app_inst = app.respond_to?(:inst) ? app.inst : app
|
24
|
-
|
25
|
-
@sources = options.sources.dup + convert_files_to_sources(options.files)
|
26
|
-
@decoders = default_decoders.merge(options.decoders)
|
27
22
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
@collection = options.collection
|
32
|
-
sources.push options.collection.merge alias: File.join( options.collection[:alias], 'all' )
|
33
|
-
end
|
23
|
+
@app_inst = app.respond_to?(:inst) ? app.inst : app
|
24
|
+
@sources = options.sources.dup + convert_files_to_sources(options.files)
|
25
|
+
@decoders = default_decoders.merge(options.decoders)
|
34
26
|
|
35
27
|
sources.each do |source|
|
36
|
-
add_data_callback_for_source
|
28
|
+
add_data_callback_for_source source
|
37
29
|
end
|
38
30
|
|
39
|
-
if collection
|
40
|
-
collection
|
31
|
+
if !options.collection.empty?
|
32
|
+
collection = { index: 'all' }.merge options.collection
|
33
|
+
|
34
|
+
if collection[:index]
|
35
|
+
add_data_callback_for_source collection.merge alias: File.join( collection[:alias], collection[:index] )
|
36
|
+
collection_data_callback = lambda { app_inst.data[collection[:alias]][collection[:index]] }
|
37
|
+
else
|
38
|
+
collection_data_callback = lambda { get_data(collection) }
|
39
|
+
end
|
40
|
+
|
41
|
+
collection[:items].call( collection_data_callback.call() ).map do |source|
|
41
42
|
source[:alias] = File.join(collection[:alias], source[:alias])
|
42
43
|
source
|
43
44
|
end.each do |source|
|
@@ -49,21 +50,18 @@ module Middleman
|
|
49
50
|
private
|
50
51
|
|
51
52
|
def add_data_callback_for_source source
|
52
|
-
|
53
|
-
extension = raw_extension.split('?').first
|
54
|
-
parts = source[:alias].split(File::SEPARATOR)
|
55
|
-
basename = File.basename(parts.pop, raw_extension)
|
53
|
+
basename, parts, extension = extract_basename_parts_and_extension source
|
56
54
|
|
57
55
|
if parts.empty?
|
58
56
|
original_callback = app_inst.data.callbacks[basename]
|
59
57
|
app_inst.data.callbacks[basename] = Proc.new do
|
60
|
-
attempt_merge_then_enhance
|
58
|
+
attempt_merge_then_enhance get_data(source, extension), original_callback
|
61
59
|
end
|
62
60
|
else
|
63
61
|
original_callback = app_inst.data.callbacks[parts.first]
|
64
62
|
app_inst.data.callbacks[parts.first] = Proc.new do
|
65
63
|
begin
|
66
|
-
built_data = { basename =>
|
64
|
+
built_data = { basename => get_data(source, extension) }
|
67
65
|
parts[1..-1].reverse.each do |part|
|
68
66
|
built_data = { part => built_data }
|
69
67
|
end
|
@@ -111,10 +109,20 @@ module Middleman
|
|
111
109
|
return ::Middleman::Util.recursively_enhance new_data
|
112
110
|
end
|
113
111
|
|
114
|
-
def
|
112
|
+
def extract_basename_parts_and_extension source
|
113
|
+
raw_extension = File.extname(source[:path])
|
114
|
+
extension = raw_extension.split('?').first
|
115
|
+
parts = source[:alias].split(File::SEPARATOR)
|
116
|
+
basename = File.basename(parts.pop, raw_extension)
|
117
|
+
|
118
|
+
[basename, parts, extension]
|
119
|
+
end
|
120
|
+
|
121
|
+
def get_data source, extension=nil
|
115
122
|
if source.has_key? :type
|
116
123
|
decoder = decoders[source[:type]]
|
117
124
|
else
|
125
|
+
_, _, extension = extract_basename_parts_and_extension(source) unless extension
|
118
126
|
decoder = decoders.find do |candidate|
|
119
127
|
candidate[1][:extensions].include? extension
|
120
128
|
end
|
@@ -123,7 +131,13 @@ module Middleman
|
|
123
131
|
|
124
132
|
raise UnsupportedDataExtension unless decoder
|
125
133
|
|
126
|
-
decoder[:decoder].call get_file_contents source[:path]
|
134
|
+
data = decoder[:decoder].call get_file_contents source[:path]
|
135
|
+
|
136
|
+
if source[:middleware]
|
137
|
+
data = source[:middleware].call data
|
138
|
+
end
|
139
|
+
|
140
|
+
return data
|
127
141
|
end
|
128
142
|
|
129
143
|
def get_file_contents file_path
|
data/readme.md
CHANGED
@@ -155,7 +155,7 @@ Collections allow you to collection sources that have belong together in an arra
|
|
155
155
|
}
|
156
156
|
```
|
157
157
|
|
158
|
-
Then set up a collection to access them through Middleman. A collection requires 3 keys, an `alias`, `path`, and `items`. The alias & path act just like a source, except that data will be available at `#{
|
158
|
+
Then set up a collection to access them through Middleman. A collection requires 3 keys, an `alias`, `path`, and `items`. The alias & path act just like a source, except that data will be available at `#{alias}.#{index}`. Items should be an object that responds to `#call` and returns an array of sources when given the data from the collection index. A collection for our example API:
|
159
159
|
|
160
160
|
```ruby
|
161
161
|
activate :data_source do |c|
|
@@ -163,6 +163,7 @@ activate :data_source do |c|
|
|
163
163
|
c.collection = {
|
164
164
|
alias: 'got_chars',
|
165
165
|
path: '/got/index.json',
|
166
|
+
index: 'all',
|
166
167
|
items: Proc.new { |data|
|
167
168
|
data.map do |char|
|
168
169
|
{
|
@@ -185,6 +186,35 @@ data.got_chars['eddard-stark'].quote
|
|
185
186
|
# => Winter is coming
|
186
187
|
```
|
187
188
|
|
189
|
+
For index_name, you can also pass `false` to not generate the index data.
|
190
|
+
|
191
|
+
|
192
|
+
### Adding Middleware
|
193
|
+
|
194
|
+
You may wish to alter the incoming data before it gets added to Middleman's data object, to do so you'll add middleware. Middleware should respond to `#call` and return your altered data. Here's an example where we add "1" to each item in an array:
|
195
|
+
|
196
|
+
```yaml
|
197
|
+
# count.yaml
|
198
|
+
- 1
|
199
|
+
- 2
|
200
|
+
- 3
|
201
|
+
```
|
202
|
+
|
203
|
+
```ruby
|
204
|
+
# config.rb
|
205
|
+
activate :data_source do |c|
|
206
|
+
c.root = "http://example.com"
|
207
|
+
c.sources = [{
|
208
|
+
alias: 'altered',
|
209
|
+
path: 'count.yaml',
|
210
|
+
middleware: Proc.new { |data| data.map { |i| i + 1 } }
|
211
|
+
}]
|
212
|
+
end
|
213
|
+
|
214
|
+
data.altered
|
215
|
+
# => [2, 3, 4]
|
216
|
+
```
|
217
|
+
|
188
218
|
|
189
219
|
# Testing
|
190
220
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
set :environment, :test
|
2
2
|
set :show_exceptions, false
|
3
3
|
|
4
|
-
|
4
|
+
# base collection spec
|
5
5
|
activate :data_source do |c|
|
6
6
|
|
7
7
|
c.root = File.join( Dir.pwd, 'remote_data' )
|
@@ -21,7 +21,7 @@ activate :data_source do |c|
|
|
21
21
|
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
# extensionless
|
25
25
|
activate :data_source do |c|
|
26
26
|
|
27
27
|
c.root = File.join( Dir.pwd, 'remote_data' )
|
@@ -35,3 +35,31 @@ activate :data_source do |c|
|
|
35
35
|
|
36
36
|
end
|
37
37
|
|
38
|
+
# custom index key
|
39
|
+
activate :data_source do |c|
|
40
|
+
|
41
|
+
c.root = File.join( Dir.pwd, 'remote_data' )
|
42
|
+
|
43
|
+
c.collection = {
|
44
|
+
alias: 'custom_index',
|
45
|
+
index: 'dem_things',
|
46
|
+
path: 'custom_index/index.yaml',
|
47
|
+
items: Proc.new { |d| [] }
|
48
|
+
}
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
# no index key
|
53
|
+
activate :data_source do |c|
|
54
|
+
|
55
|
+
c.root = File.join( Dir.pwd, 'remote_data' )
|
56
|
+
|
57
|
+
c.collection = {
|
58
|
+
alias: 'no_index',
|
59
|
+
index: false,
|
60
|
+
path: 'no_index/index.yaml',
|
61
|
+
items: Proc.new { |d| [{ alias: 'data',
|
62
|
+
path: 'no_index/index.yaml' }] }
|
63
|
+
}
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
set :environment, :test
|
2
|
+
set :show_exceptions, false
|
3
|
+
|
4
|
+
activate :data_source do |c|
|
5
|
+
|
6
|
+
c.root = File.join( Dir.pwd, 'remote_data' )
|
7
|
+
|
8
|
+
c.sources = [
|
9
|
+
{
|
10
|
+
alias: 'altered',
|
11
|
+
path: 'zero_index_array.yaml',
|
12
|
+
middleware: Proc.new { |data|
|
13
|
+
data.map { |i| i + 1 }
|
14
|
+
}
|
15
|
+
}
|
16
|
+
]
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
RSpec.describe "Middleman::DataSource::Extension (feat) collection" do
|
2
|
+
|
3
|
+
before :each do
|
4
|
+
Given.fixture 'collection'
|
5
|
+
@mm = Middleman::Fixture.app
|
6
|
+
end
|
7
|
+
|
8
|
+
after :each do
|
9
|
+
Given.cleanup!
|
10
|
+
end
|
11
|
+
|
12
|
+
it "makes collection items available at aliases" do
|
13
|
+
expect( @mm.data.root.john.title ).to eq "John"
|
14
|
+
expect( @mm.data.root.hodor.title ).to eq "Hodor"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "makes collection index available at #all" do
|
18
|
+
expect( @mm.data.root.all.map(&:to_h) ).to match_array [{ "extra" => "info",
|
19
|
+
"slug" => "hodor" },
|
20
|
+
{ "extra" => "info",
|
21
|
+
"slug" => "john" }]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "allows passing an extension type" do
|
25
|
+
expect( @mm.data.extensionless.all.foo ).to eq "bar"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "sets custom index key if given one" do
|
29
|
+
expect( @mm.data.custom_index.dem_things.map(&:to_h) ).to match_array [{"dem" => "things"}]
|
30
|
+
end
|
31
|
+
|
32
|
+
it "removes index if given index false" do
|
33
|
+
expect( @mm.data.no_index.all ).to be_nil
|
34
|
+
expect( @mm.data.no_index.data.missing ).to eq "index"
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
RSpec.describe "Middleman::DataSource::Extension (feat) middleware" do
|
2
|
+
|
3
|
+
before :each do
|
4
|
+
Given.fixture 'middleware'
|
5
|
+
@mm = Middleman::Fixture.app
|
6
|
+
end
|
7
|
+
|
8
|
+
after :each do
|
9
|
+
Given.cleanup!
|
10
|
+
end
|
11
|
+
|
12
|
+
it "sets data after being passed through middleware" do
|
13
|
+
expect( @mm.data.altered ).to match_array [1, 2, 3, 4]
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -148,32 +148,4 @@ RSpec.describe Middleman::DataSource::Extension do
|
|
148
148
|
|
149
149
|
end
|
150
150
|
|
151
|
-
|
152
|
-
context "with collection app" do
|
153
|
-
before :each do
|
154
|
-
Given.fixture 'collection'
|
155
|
-
@mm = Middleman::Fixture.app
|
156
|
-
end
|
157
|
-
|
158
|
-
after :each do
|
159
|
-
Given.cleanup!
|
160
|
-
end
|
161
|
-
|
162
|
-
it "makes collection items available at aliases" do
|
163
|
-
expect( @mm.data.root.john.title ).to eq "John"
|
164
|
-
expect( @mm.data.root.hodor.title ).to eq "Hodor"
|
165
|
-
end
|
166
|
-
|
167
|
-
it "makes collection index available at #all" do
|
168
|
-
expect( @mm.data.root.all.map(&:to_h) ).to match_array [{ "extra" => "info",
|
169
|
-
"slug" => "hodor" },
|
170
|
-
{ "extra" => "info",
|
171
|
-
"slug" => "john" }]
|
172
|
-
end
|
173
|
-
|
174
|
-
it "allows passing an extension type" do
|
175
|
-
expect( @mm.data.extensionless.all.foo ).to eq "bar"
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
151
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: middleman-data_source
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steven Sloan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: middleman
|
@@ -70,10 +70,13 @@ files:
|
|
70
70
|
- spec/fixtures/collection/config.rb
|
71
71
|
- spec/fixtures/files_as_hash/config.rb
|
72
72
|
- spec/fixtures/imediate_use/config.rb
|
73
|
+
- spec/fixtures/middleware/config.rb
|
73
74
|
- spec/fixtures/multiple_instances/config.rb
|
74
75
|
- spec/fixtures/nested_alias/config.rb
|
75
76
|
- spec/fixtures/unsupported_extension/config.rb
|
76
77
|
- spec/lib/middleman-data_source_spec.rb
|
78
|
+
- spec/lib/middleman/data_source/extension/collection_spec.rb
|
79
|
+
- spec/lib/middleman/data_source/extension/middleware_spec.rb
|
77
80
|
- spec/lib/middleman/data_source/extension_spec.rb
|
78
81
|
- spec/lib/middleman/data_source/version_spec.rb
|
79
82
|
- spec/spec_helper.rb
|
@@ -110,9 +113,12 @@ test_files:
|
|
110
113
|
- spec/fixtures/collection/config.rb
|
111
114
|
- spec/fixtures/files_as_hash/config.rb
|
112
115
|
- spec/fixtures/imediate_use/config.rb
|
116
|
+
- spec/fixtures/middleware/config.rb
|
113
117
|
- spec/fixtures/multiple_instances/config.rb
|
114
118
|
- spec/fixtures/nested_alias/config.rb
|
115
119
|
- spec/fixtures/unsupported_extension/config.rb
|
120
|
+
- spec/lib/middleman/data_source/extension/collection_spec.rb
|
121
|
+
- spec/lib/middleman/data_source/extension/middleware_spec.rb
|
116
122
|
- spec/lib/middleman/data_source/extension_spec.rb
|
117
123
|
- spec/lib/middleman/data_source/version_spec.rb
|
118
124
|
- spec/lib/middleman-data_source_spec.rb
|