conformist 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +5 -0
- data/README.md +13 -1
- data/lib/conformist/builder.rb +2 -2
- data/lib/conformist/column.rb +6 -2
- data/lib/conformist/schema.rb +2 -1
- data/lib/conformist/version.rb +1 -1
- data/test/unit/conformist/builder_test.rb +2 -2
- data/test/unit/conformist/column_test.rb +12 -0
- data/test/unit/conformist/schema_test.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 522d7fcbc544f4bfc13ffbf3dc5a5b17a722de88
|
4
|
+
data.tar.gz: 6cfbcc1646ffae7f81add151b478a85ec0b35f57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c55cf94d0a0d3d759dcbc4520407182ed9900593b76e2343333c2b49e53d403ccd56214f4667a8905d3c469addd1d856c2d3b2a5a0407653986b45a4f1fa3c44
|
7
|
+
data.tar.gz: becc47e0d07e3547d751eb8501d4f5f56f6068337b88e2af5843b749059b7409e3599a2ac153dea58ef6ca56f7f98c28fe83ecbbbb690c9960f51c2fc3dc786f
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -262,6 +262,18 @@ column :first_name, 0 do |value|
|
|
262
262
|
end
|
263
263
|
```
|
264
264
|
|
265
|
+
It's also possible to provide a context object that is made available during preprocessing.
|
266
|
+
|
267
|
+
``` ruby
|
268
|
+
citizen = Conformist.new do
|
269
|
+
column :name, 0, 1 do |values, context|
|
270
|
+
(context[:upcase?] ? values.map(&:upcase) : values) * ' '
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
citizen.conform [['tate', 'johnson']], context: {upcase?: true}
|
275
|
+
```
|
276
|
+
|
265
277
|
### Virtual Columns
|
266
278
|
|
267
279
|
Virtual columns are not sourced from input. Omit the index to create a virtual column. Like real columns, virtual columns are included in the conformed output.
|
@@ -346,7 +358,7 @@ See CHANGELOG.md for a full list of changes.
|
|
346
358
|
|
347
359
|
## Compatibility
|
348
360
|
|
349
|
-
* MRI 2.3.1, 2.2.0, 2.1.0, 2.0.0, 1.9.3
|
361
|
+
* MRI 2.4.0, 2.3.1, 2.2.0, 2.1.0, 2.0.0, 1.9.3
|
350
362
|
* JRuby
|
351
363
|
|
352
364
|
## Dependencies
|
data/lib/conformist/builder.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
module Conformist
|
2
2
|
class Builder
|
3
|
-
def self.call schema, enumerable
|
3
|
+
def self.call schema, enumerable, context = nil
|
4
4
|
columns = schema.columns
|
5
5
|
hash = columns.each_with_object({}) do |column, hash|
|
6
|
-
hash[column.name] = column.values_in(enumerable)
|
6
|
+
hash[column.name] = column.values_in(enumerable, context)
|
7
7
|
end
|
8
8
|
HashStruct.new hash
|
9
9
|
end
|
data/lib/conformist/column.rb
CHANGED
@@ -21,7 +21,7 @@ module Conformist
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
def values_in enumerable
|
24
|
+
def values_in enumerable, context = nil
|
25
25
|
enumerable = Array(enumerable)
|
26
26
|
|
27
27
|
values = Array(indexes).map do |index|
|
@@ -35,7 +35,11 @@ module Conformist
|
|
35
35
|
end
|
36
36
|
values = values.first if values.size == 1
|
37
37
|
if preprocessor
|
38
|
-
preprocessor.
|
38
|
+
if preprocessor.arity == 1
|
39
|
+
preprocessor.call values
|
40
|
+
else
|
41
|
+
preprocessor.call values, context
|
42
|
+
end
|
39
43
|
else
|
40
44
|
values
|
41
45
|
end
|
data/lib/conformist/schema.rb
CHANGED
@@ -53,6 +53,7 @@ module Conformist
|
|
53
53
|
|
54
54
|
def conform enumerables, options = {}
|
55
55
|
options = options.dup
|
56
|
+
context = options.delete(:context)
|
56
57
|
|
57
58
|
Enumerator.new do |yielder|
|
58
59
|
enumerables.each do |enumerable|
|
@@ -61,7 +62,7 @@ module Conformist
|
|
61
62
|
next
|
62
63
|
end
|
63
64
|
|
64
|
-
yielder.yield builder.call(self, enumerable)
|
65
|
+
yielder.yield builder.call(self, enumerable, context)
|
65
66
|
end
|
66
67
|
end
|
67
68
|
end
|
data/lib/conformist/version.rb
CHANGED
@@ -4,9 +4,9 @@ class Conformist::BuilderTest < Minitest::Test
|
|
4
4
|
def test_call
|
5
5
|
column = Minitest::Mock.new
|
6
6
|
column.expect :name, :a
|
7
|
-
column.expect :values_in, [1], [Array]
|
7
|
+
column.expect :values_in, [1], [Array, Hash]
|
8
8
|
definition = Minitest::Mock.new
|
9
9
|
definition.expect :columns, [column]
|
10
|
-
assert_equal HashStruct.new({:a => [1]}), Builder.call(definition, [])
|
10
|
+
assert_equal HashStruct.new({:a => [1]}), Builder.call(definition, [], {})
|
11
11
|
end
|
12
12
|
end
|
@@ -50,4 +50,16 @@ class Conformist::ColumnTest < Minitest::Test
|
|
50
50
|
column = Column.new :foo, 0
|
51
51
|
assert_nil column.values_in([])
|
52
52
|
end
|
53
|
+
|
54
|
+
def test_passes_context
|
55
|
+
context = {'a' => 'b'}
|
56
|
+
column = Column.new(:foo, 0) { |_, ctx| ctx }
|
57
|
+
assert_equal context, column.values_in([], context)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_respects_preprocessor_arity
|
61
|
+
context = {'a' => 'b'}
|
62
|
+
column = Column.new(:foo, 0, &lambda { |values| })
|
63
|
+
column.values_in([], context)
|
64
|
+
end
|
53
65
|
end
|
@@ -26,7 +26,7 @@ class Conformist::SchemaTest < Minitest::Test
|
|
26
26
|
definition = Class.new { extend Schema }
|
27
27
|
definition.builder = Object
|
28
28
|
assert_equal Object, definition.builder
|
29
|
-
end
|
29
|
+
end
|
30
30
|
|
31
31
|
def test_columns_reader
|
32
32
|
assert_empty Class.new { extend Schema }.columns
|
@@ -66,7 +66,7 @@ class Conformist::SchemaTest < Minitest::Test
|
|
66
66
|
|
67
67
|
def test_conform_calls_builders_call_method
|
68
68
|
definition = Class.new { extend Schema }
|
69
|
-
definition.builder = lambda { |definition, value| value }
|
69
|
+
definition.builder = lambda { |definition, value, context| value }
|
70
70
|
assert_equal [2, 4], definition.conform([1, 2]).map { |value| value * 2 }
|
71
71
|
end
|
72
72
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: conformist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tate Johnson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|