blockhead 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/blockhead/extractors/abstract.rb +3 -3
- data/lib/blockhead/extractors/block.rb +2 -2
- data/lib/blockhead/extractors/enumerable.rb +2 -2
- data/lib/blockhead/extractors/proc.rb +0 -6
- data/lib/blockhead/extractors/value.rb +10 -2
- data/lib/blockhead/marshaller.rb +1 -1
- data/lib/blockhead/option_key.rb +8 -9
- data/lib/blockhead/value_extractor.rb +3 -3
- data/lib/blockhead/version.rb +1 -1
- data/spec/blockhead/extractors/block_spec.rb +1 -1
- data/spec/blockhead/extractors/enumerable_spec.rb +1 -1
- data/spec/blockhead/extractors/value_spec.rb +12 -2
- data/spec/blockhead/schema_spec.rb +12 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 211e1d502a90c82116e5d5d16bcd63480313f796
|
4
|
+
data.tar.gz: 66e0903c9cd217b820e3d4d5094853c1b8bc84eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e1161b550868745a6e47844eb1b2140c998d2e9b304f0850fdd8eec1847eee909135c96bec51e0bf269f18cc905d00eb84eea0e1638b5367dcf271d918d7462
|
7
|
+
data.tar.gz: 68eb246e76b42dc4f65f08d4df295a3e2485a52d791a9178e2f6cbb956c80d6e7b90f49d6c87731911fec891ea73e8c06039dacea446eab647351c40fc1e94aa
|
@@ -4,9 +4,9 @@ module Blockhead
|
|
4
4
|
attr_writer :next
|
5
5
|
attr_reader :object
|
6
6
|
|
7
|
-
def initialize(object,
|
7
|
+
def initialize(object, arg, proc)
|
8
8
|
@object = object
|
9
|
-
@
|
9
|
+
@arg = arg
|
10
10
|
@proc = proc
|
11
11
|
end
|
12
12
|
|
@@ -28,7 +28,7 @@ module Blockhead
|
|
28
28
|
|
29
29
|
protected
|
30
30
|
|
31
|
-
attr_reader :
|
31
|
+
attr_reader :arg, :proc
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -2,11 +2,11 @@ module Blockhead
|
|
2
2
|
module Extractors
|
3
3
|
class Enumerable < Abstract
|
4
4
|
def valid?
|
5
|
-
|
5
|
+
proc && safe_enumerable?
|
6
6
|
end
|
7
7
|
|
8
8
|
def extract_value
|
9
|
-
object.map { |obj| Block.new(obj, [],
|
9
|
+
object.map { |obj| Block.new(obj, [], proc).extract_value }
|
10
10
|
end
|
11
11
|
|
12
12
|
private
|
@@ -12,12 +12,20 @@ module Blockhead
|
|
12
12
|
private
|
13
13
|
|
14
14
|
def value
|
15
|
-
if
|
16
|
-
|
15
|
+
if arg.is_a?(Hash) && arg[:with]
|
16
|
+
send(:"#{modifier}_modifier")
|
17
17
|
else
|
18
18
|
object
|
19
19
|
end
|
20
20
|
end
|
21
|
+
|
22
|
+
def modifier
|
23
|
+
arg[:with]
|
24
|
+
end
|
25
|
+
|
26
|
+
def pretty_print_modifier
|
27
|
+
object.split(/ |\_/).map(&:capitalize).join(' ').strip
|
28
|
+
end
|
21
29
|
end
|
22
30
|
end
|
23
31
|
end
|
data/lib/blockhead/marshaller.rb
CHANGED
@@ -14,7 +14,7 @@ module Blockhead
|
|
14
14
|
def method_missing(name, *args, &block)
|
15
15
|
@arg = args.first
|
16
16
|
key = OptionKey.new(name, arg).key
|
17
|
-
attributes[key] = ValueExtractor.new(_call(name),
|
17
|
+
attributes[key] = ValueExtractor.new(_call(name), arg, &block).extract
|
18
18
|
end
|
19
19
|
|
20
20
|
private
|
data/lib/blockhead/option_key.rb
CHANGED
@@ -8,23 +8,22 @@ module Blockhead
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def key
|
11
|
-
key = extract_key
|
12
|
-
raise TypeError, 'Aliases cannot be nil' unless key
|
13
|
-
key
|
14
|
-
end
|
15
|
-
|
16
|
-
private
|
17
|
-
|
18
|
-
def extract_key
|
19
11
|
if options?
|
12
|
+
raise TypeError, 'Aliase is not of expected type' if bad_type?
|
20
13
|
options[:as]
|
21
14
|
else
|
22
15
|
default
|
23
16
|
end
|
24
17
|
end
|
25
18
|
|
19
|
+
private
|
20
|
+
|
26
21
|
def options?
|
27
|
-
options.is_a?(Hash)
|
22
|
+
options.is_a?(Hash) && options.has_key?(:as)
|
23
|
+
end
|
24
|
+
|
25
|
+
def bad_type?
|
26
|
+
!(options[:as].is_a?(Symbol) || options[:as].is_a?(String))
|
28
27
|
end
|
29
28
|
end
|
30
29
|
end
|
@@ -2,12 +2,12 @@ module Blockhead
|
|
2
2
|
class ValueExtractor
|
3
3
|
attr_reader :extractor
|
4
4
|
|
5
|
-
def initialize(value,
|
5
|
+
def initialize(value, arg, &block)
|
6
6
|
proc = block.to_proc if block
|
7
|
-
@extractor = Extractors::Enumerable.new value,
|
7
|
+
@extractor = Extractors::Enumerable.new value, arg, proc
|
8
8
|
|
9
9
|
extractors.inject(extractor) do |fallback, link|
|
10
|
-
fallback.next = link.new value,
|
10
|
+
fallback.next = link.new value, arg, proc
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
data/lib/blockhead/version.rb
CHANGED
@@ -2,14 +2,24 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Blockhead::Extractors::Value, '#valid?' do
|
4
4
|
it 'returns true' do
|
5
|
-
extractor = Blockhead::Extractors::Value.new('test',
|
5
|
+
extractor = Blockhead::Extractors::Value.new('test', nil, nil)
|
6
6
|
expect(extractor).to be_valid
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
10
|
describe Blockhead::Extractors::Value, '#extract_value' do
|
11
11
|
it 'returns @value unmolested' do
|
12
|
-
extractor = Blockhead::Extractors::Value.new('test',
|
12
|
+
extractor = Blockhead::Extractors::Value.new('test', nil, nil)
|
13
13
|
expect(extractor.extract_value).to eq 'test'
|
14
14
|
end
|
15
|
+
|
16
|
+
it 'cleans up strings when passed with: :pretty_print' do
|
17
|
+
extractor = Blockhead::Extractors::Value.new(
|
18
|
+
"This is Crazy \n",
|
19
|
+
{with: :pretty_print},
|
20
|
+
nil
|
21
|
+
)
|
22
|
+
|
23
|
+
expect(extractor.extract_value).to eq 'This Is Crazy'
|
24
|
+
end
|
15
25
|
end
|
@@ -134,12 +134,12 @@ describe Blockhead::Schema, '::define' do
|
|
134
134
|
expect(schema.marshal).to eq 'header' => 'Title', 'body' => 'Description'
|
135
135
|
end
|
136
136
|
|
137
|
-
it 'raises TypeError on
|
137
|
+
it 'raises TypeError on non symbol aliases' do
|
138
138
|
expect {
|
139
139
|
schema_with do
|
140
140
|
title as: header
|
141
141
|
end
|
142
|
-
}.to raise_error TypeError, '
|
142
|
+
}.to raise_error TypeError, 'Aliase is not of expected type'
|
143
143
|
end
|
144
144
|
|
145
145
|
it 'accepts procs' do
|
@@ -164,9 +164,9 @@ describe Blockhead::Schema, '::define' do
|
|
164
164
|
expect(schema.marshal).to eq result
|
165
165
|
end
|
166
166
|
|
167
|
-
it 'cleans fields when passed :
|
167
|
+
it 'cleans fields when passed with: :pretty_print' do
|
168
168
|
schema = schema_with(BadFormatting) do
|
169
|
-
title :pretty_print
|
169
|
+
title with: :pretty_print
|
170
170
|
another_field
|
171
171
|
end.marshal
|
172
172
|
|
@@ -176,6 +176,14 @@ describe Blockhead::Schema, '::define' do
|
|
176
176
|
)
|
177
177
|
end
|
178
178
|
|
179
|
+
it 'allows multiple options on a field' do
|
180
|
+
schema = schema_with(BadFormatting) do
|
181
|
+
title with: :pretty_print, as: :alias
|
182
|
+
end.marshal
|
183
|
+
|
184
|
+
expect(schema).to eq alias: 'Title And Things'
|
185
|
+
end
|
186
|
+
|
179
187
|
it 'handles nested objects with aliases' do
|
180
188
|
schema = schema_with do
|
181
189
|
nested_obj as: :cart do
|