blockhead 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a25635acbe737a9bb1a835991c44832f3ac02388
4
- data.tar.gz: 56b936c08c511abc20980fed4063f47618440c09
3
+ metadata.gz: 211e1d502a90c82116e5d5d16bcd63480313f796
4
+ data.tar.gz: 66e0903c9cd217b820e3d4d5094853c1b8bc84eb
5
5
  SHA512:
6
- metadata.gz: 158300c7966ca2ef3c6f0e9a2e03d47dd679e1bc0daf4c6693d358d4361b3e11a67b603b75b18c2cb7b0953df8d9089664353b019a8f0a283ff7674a430e0600
7
- data.tar.gz: 572f8400f3652eb2a73fd592156e0d868e9742b8e9cd4b91c56ce2ca7a6eed8e5e4bd782ac04869f49f5ba9a945dde9b915e5e6084328a739ba4a9bb11b63a48
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, *args, proc)
7
+ def initialize(object, arg, proc)
8
8
  @object = object
9
- @args = args
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 :args
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 Block < Abstract
4
4
  def valid?
5
- !!@proc
5
+ !!proc
6
6
  end
7
7
 
8
8
  def extract_value
9
- Schema.define(object, &@proc).marshal
9
+ Schema.define(object, &proc).marshal
10
10
  end
11
11
  end
12
12
  end
@@ -2,11 +2,11 @@ module Blockhead
2
2
  module Extractors
3
3
  class Enumerable < Abstract
4
4
  def valid?
5
- @proc && safe_enumerable?
5
+ proc && safe_enumerable?
6
6
  end
7
7
 
8
8
  def extract_value
9
- object.map { |obj| Block.new(obj, [], @proc).extract_value }
9
+ object.map { |obj| Block.new(obj, [], proc).extract_value }
10
10
  end
11
11
 
12
12
  private
@@ -8,12 +8,6 @@ module Blockhead
8
8
  def extract_value
9
9
  arg.call
10
10
  end
11
-
12
- private
13
-
14
- def arg
15
- @args.first
16
- end
17
11
  end
18
12
  end
19
13
  end
@@ -12,12 +12,20 @@ module Blockhead
12
12
  private
13
13
 
14
14
  def value
15
- if object.is_a?(String) && args.first == :pretty_print
16
- object.split(/ |\_/).map(&:capitalize).join(' ').strip
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
@@ -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), *args, &block).extract
17
+ attributes[key] = ValueExtractor.new(_call(name), arg, &block).extract
18
18
  end
19
19
 
20
20
  private
@@ -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, *args, &block)
5
+ def initialize(value, arg, &block)
6
6
  proc = block.to_proc if block
7
- @extractor = Extractors::Enumerable.new value, *args, proc
7
+ @extractor = Extractors::Enumerable.new value, arg, proc
8
8
 
9
9
  extractors.inject(extractor) do |fallback, link|
10
- fallback.next = link.new value, *args, proc
10
+ fallback.next = link.new value, arg, proc
11
11
  end
12
12
  end
13
13
 
@@ -1,3 +1,3 @@
1
1
  module Blockhead
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -14,5 +14,5 @@ end
14
14
 
15
15
  def block_factory
16
16
  object = double('poro', title: 'This')
17
- Blockhead::Extractors::Block.new object, [], Proc.new { title }
17
+ Blockhead::Extractors::Block.new object, nil, Proc.new { title }
18
18
  end
@@ -26,5 +26,5 @@ describe Blockhead::Extractors::Enumerable, '#extract_value' do
26
26
  end
27
27
 
28
28
  def enum_factory(object, proc = nil)
29
- Blockhead::Extractors::Enumerable.new(object, [], proc)
29
+ Blockhead::Extractors::Enumerable.new(object, nil, proc)
30
30
  end
@@ -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', [], nil)
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', [], nil)
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 nil aliases' do
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, 'Aliases cannot be nil'
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 :cleanup' do
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blockhead
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Franco