blockhead 0.0.5
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 +7 -0
- data/.gitignore +22 -0
- data/.rspec +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +81 -0
- data/Rakefile +2 -0
- data/blockhead.gemspec +25 -0
- data/lib/blockhead.rb +13 -0
- data/lib/blockhead/extractors/abstract.rb +30 -0
- data/lib/blockhead/extractors/block.rb +13 -0
- data/lib/blockhead/extractors/enumerable.rb +19 -0
- data/lib/blockhead/extractors/proc.rb +19 -0
- data/lib/blockhead/extractors/value.rb +13 -0
- data/lib/blockhead/marshaller.rb +29 -0
- data/lib/blockhead/option_key.rb +24 -0
- data/lib/blockhead/schema.rb +21 -0
- data/lib/blockhead/value_extractor.rb +24 -0
- data/lib/blockhead/version.rb +3 -0
- data/spec/blockhead/extractors/abstract_spec.rb +39 -0
- data/spec/blockhead/extractors/block_spec.rb +18 -0
- data/spec/blockhead/extractors/enumerable_spec.rb +30 -0
- data/spec/blockhead/extractors/proc_spec.rb +23 -0
- data/spec/blockhead/extractors/value_spec.rb +15 -0
- data/spec/blockhead/schema_spec.rb +120 -0
- data/spec/spec_helper.rb +7 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4e2f6018fc079c37204364a6c67616e98ddfbed2
|
4
|
+
data.tar.gz: 4661877ffcd4484944ce3c4f5c5caeb5a01482bb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 93c2aa2bc903bd8b3e7d078f19e480354497bf1e7a216d1ca80ce9fd263f9c39417e2a821d6ec1015f551bd89ab6e9497b29b4f28dc1316b5655ae1cd7221aad
|
7
|
+
data.tar.gz: 3a9b5e507c747bd739fd1bea248b07c4fcd0f6ae960ff98b0744552ae3da047c71ba1bd5ef6606edb10b04031d9544bb16f86b72870a3d1e4cb12d3a8fccf600
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Vincent Franco
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Blockhead
|
2
|
+
|
3
|
+
[](https://travis-ci.org/vinniefranco/blockhead.svg)
|
4
|
+
[](https://codeclimate.com/github/vinniefranco/blockhead.png)
|
5
|
+
Easy marshalling of object attributes.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'blockhead'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install blockhead
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Say you have the following object with a 1:1 and a 1:M set of relational attributes:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
|
27
|
+
object = OpenStruct.new(
|
28
|
+
title: 'Fancy pants',
|
29
|
+
order_number: 'STORE-1234'
|
30
|
+
items: [
|
31
|
+
OpenStruct.new(name: 'ACD', sku: '1234', ...),
|
32
|
+
]
|
33
|
+
customer: OpenStruct.new(name: 'Bill Murray', score: 10, ...)
|
34
|
+
)
|
35
|
+
|
36
|
+
```
|
37
|
+
|
38
|
+
And you want to send a different structure to your data warehouse in the format of:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
|
42
|
+
{
|
43
|
+
name: 'Bill Murray',
|
44
|
+
order: 'STORE-1234',
|
45
|
+
items: [
|
46
|
+
{ name: 'ACD', sku: '1234' }
|
47
|
+
],
|
48
|
+
customer: {
|
49
|
+
score: 10
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
To marshal you would just:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
|
59
|
+
schema = Blockhead::Schema.define object do
|
60
|
+
name -> { object.customer.name } # Accepts procs with object in scope
|
61
|
+
order_number as: :order # Aliases
|
62
|
+
items do
|
63
|
+
name # Simple definitions on collection attributes
|
64
|
+
sku
|
65
|
+
end
|
66
|
+
customer do
|
67
|
+
score
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
schema.marshal #=> { name: 'Bill Murray', order: 'STORE-1234', items: [{ name: 'ACD', sku: '1234' }], customer: { score: 10 } }
|
72
|
+
|
73
|
+
```
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
1. Fork it ( https://github.com/vinniefranco/blockhead/fork )
|
78
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
79
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
80
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
81
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/blockhead.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'blockhead/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'blockhead'
|
8
|
+
spec.version = Blockhead::VERSION
|
9
|
+
spec.authors = ['Vincent Franco']
|
10
|
+
spec.email = ['vince@freshivore.net']
|
11
|
+
spec.summary = 'A simple DSL for marshaling objects into a schema'
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = 'https://github.com/vinniefranco/blockhead'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(spec)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'debugger', '~> 1.6'
|
25
|
+
end
|
data/lib/blockhead.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'blockhead/version'
|
2
|
+
require 'blockhead/schema'
|
3
|
+
require 'blockhead/marshaller'
|
4
|
+
require 'blockhead/option_key'
|
5
|
+
require 'blockhead/value_extractor'
|
6
|
+
require 'blockhead/extractors/abstract'
|
7
|
+
require 'blockhead/extractors/block'
|
8
|
+
require 'blockhead/extractors/enumerable'
|
9
|
+
require 'blockhead/extractors/proc'
|
10
|
+
require 'blockhead/extractors/value'
|
11
|
+
|
12
|
+
module Blockhead
|
13
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Blockhead
|
2
|
+
module Extractors
|
3
|
+
class Abstract
|
4
|
+
attr_writer :next
|
5
|
+
attr_reader :object
|
6
|
+
|
7
|
+
def initialize(object, *args, proc)
|
8
|
+
@object = object
|
9
|
+
@args = args
|
10
|
+
@proc = proc
|
11
|
+
end
|
12
|
+
|
13
|
+
def valid?
|
14
|
+
fail '#valid? not implemented'
|
15
|
+
end
|
16
|
+
|
17
|
+
def extract_value
|
18
|
+
fail '#extract_value not implemented'
|
19
|
+
end
|
20
|
+
|
21
|
+
def extract
|
22
|
+
if valid?
|
23
|
+
extract_value
|
24
|
+
else
|
25
|
+
@next.extract
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Blockhead
|
2
|
+
module Extractors
|
3
|
+
class Enumerable < Abstract
|
4
|
+
def valid?
|
5
|
+
@proc && safe_enumerable?
|
6
|
+
end
|
7
|
+
|
8
|
+
def extract_value
|
9
|
+
object.map { |obj| Block.new(obj, [], @proc).extract_value }
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def safe_enumerable?
|
15
|
+
object.is_a?(::Enumerable) && !object.is_a?(::Hash)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Blockhead
|
2
|
+
class Marshaller < BasicObject
|
3
|
+
attr_reader :attributes, :object
|
4
|
+
|
5
|
+
def initialize(object)
|
6
|
+
@attributes = {}
|
7
|
+
@object = object
|
8
|
+
end
|
9
|
+
|
10
|
+
def marshal
|
11
|
+
attributes
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(name, *args, &block)
|
15
|
+
key = OptionKey.new(name, args.first).key
|
16
|
+
attributes[key] = ValueExtractor.new(_call(name), *args, &block).extract
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def _call(name)
|
22
|
+
if object.respond_to?(name)
|
23
|
+
object.send name
|
24
|
+
else
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Blockhead
|
2
|
+
class OptionKey
|
3
|
+
attr_reader :default, :options
|
4
|
+
|
5
|
+
def initialize(name, options)
|
6
|
+
@default = name
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
def key
|
11
|
+
if options?
|
12
|
+
options[:as]
|
13
|
+
else
|
14
|
+
default
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def options?
|
21
|
+
options.is_a?(Hash)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Blockhead
|
2
|
+
class Schema
|
3
|
+
attr_reader :object
|
4
|
+
|
5
|
+
def self.define(object, &block)
|
6
|
+
new(object).define(&block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(object)
|
10
|
+
@object = object
|
11
|
+
end
|
12
|
+
|
13
|
+
def define(&block)
|
14
|
+
fail 'No schema definition' unless block
|
15
|
+
schema = Marshaller.new(object)
|
16
|
+
schema.instance_eval(&block)
|
17
|
+
|
18
|
+
schema
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Blockhead
|
2
|
+
class ValueExtractor
|
3
|
+
attr_reader :extractor
|
4
|
+
|
5
|
+
def initialize(value, *args, &block)
|
6
|
+
proc = block.to_proc if block
|
7
|
+
@extractor = Extractors::Enumerable.new value, *args, proc
|
8
|
+
|
9
|
+
extractors.inject(extractor) do |fallback, link|
|
10
|
+
fallback.next = link.new value, *args, proc
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def extract
|
15
|
+
extractor.extract
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def extractors
|
21
|
+
[Extractors::Block, Extractors::Proc, Extractors::Value]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Blockhead::Extractors::Abstract, '#valid?' do
|
4
|
+
let(:blank) { Blockhead::Extractors::Abstract.new(nil, nil, nil) }
|
5
|
+
|
6
|
+
it 'fails with "#valid? not implemented"' do
|
7
|
+
expect { blank.valid? }.to raise_error RuntimeError
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
describe Blockhead::Extractors::Abstract, '#extract_value' do
|
13
|
+
let(:blank) { Blockhead::Extractors::Abstract.new(nil, nil, nil) }
|
14
|
+
|
15
|
+
it 'fails with #extract_value not implemented' do
|
16
|
+
expect { blank.extract_value }.to raise_error RuntimeError
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe Blockhead::Extractors::Abstract, '#extract' do
|
21
|
+
let(:blank) { Blockhead::Extractors::Abstract.new(nil, nil, nil) }
|
22
|
+
|
23
|
+
it 'receives #extract_value when valid? is true' do
|
24
|
+
allow(blank).to receive(:valid?) { true }
|
25
|
+
expect(blank).to receive(:extract_value)
|
26
|
+
|
27
|
+
blank.extract
|
28
|
+
end
|
29
|
+
|
30
|
+
it '@next receives #extract when valid? is false' do
|
31
|
+
link = double
|
32
|
+
blank.next = link
|
33
|
+
|
34
|
+
allow(blank).to receive(:valid?) { false }
|
35
|
+
expect(link).to receive(:extract)
|
36
|
+
|
37
|
+
blank.extract
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Blockhead::Extractors::Block, '#valid?' do
|
4
|
+
it 'is valid when @proc is assigned' do
|
5
|
+
expect(block_factory).to be_valid
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Blockhead::Extractors::Block, '#extract_value' do
|
10
|
+
it 'marshals objects through Schema.define' do
|
11
|
+
expect(block_factory.extract_value).to eq title: 'This'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def block_factory
|
16
|
+
object = double('poro', title: 'This')
|
17
|
+
Blockhead::Extractors::Block.new object, [], Proc.new { title }
|
18
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Blockhead::Extractors::Enumerable, '#valid?' do
|
4
|
+
it 'is valid with a @proc and a array @value' do
|
5
|
+
extractor = enum_factory([], Proc.new { 'Test.'})
|
6
|
+
expect(extractor).to be_valid
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'is invalid without a @proc' do
|
10
|
+
extractor = enum_factory([], nil)
|
11
|
+
expect(extractor).to_not be_valid
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'is invalid with the incorrect @value type' do
|
15
|
+
extractor = enum_factory('', Proc.new { title })
|
16
|
+
expect(extractor).to_not be_valid
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe Blockhead::Extractors::Enumerable, '#extract_value' do
|
21
|
+
it 'passes elements in value to Schema with @proc' do
|
22
|
+
item = double(title: 'Foo')
|
23
|
+
extractor = enum_factory([item], Proc.new { title })
|
24
|
+
expect(extractor.extract_value).to eq [{ title: 'Foo' }]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def enum_factory(object, proc = nil)
|
29
|
+
Blockhead::Extractors::Enumerable.new(object, [], proc)
|
30
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Blockhead::Extractors::Proc, '#valid?' do
|
4
|
+
it 'is valid when a proc is passed in as the first argument' do
|
5
|
+
expect(proc_factory).to be_valid
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Blockhead::Extractors::Proc, '#extract_value' do
|
10
|
+
it "uses the body of the proc as it's extracted value" do
|
11
|
+
expect(proc_factory.extract_value).to eq 'test'
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'has @value within scope on extract' do
|
15
|
+
value = double(title: 'This')
|
16
|
+
proc = proc_factory(-> { "#{value.title} is a title" }, value)
|
17
|
+
expect(proc.extract_value).to eq 'This is a title'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def proc_factory(proc = -> { 'test' }, value = nil)
|
22
|
+
Blockhead::Extractors::Proc.new value, proc, nil
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Blockhead::Extractors::Value, '#valid?' do
|
4
|
+
it 'returns true' do
|
5
|
+
extractor = Blockhead::Extractors::Value.new('test', [], nil)
|
6
|
+
expect(extractor).to be_valid
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe Blockhead::Extractors::Value, '#extract_value' do
|
11
|
+
it 'returns @value unmolested' do
|
12
|
+
extractor = Blockhead::Extractors::Value.new('test', [], nil)
|
13
|
+
expect(extractor.extract_value).to eq 'test'
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'debugger'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
describe Blockhead::Schema, '::define' do
|
6
|
+
class Tester
|
7
|
+
def title
|
8
|
+
'Title'
|
9
|
+
end
|
10
|
+
|
11
|
+
def description
|
12
|
+
'Description'
|
13
|
+
end
|
14
|
+
|
15
|
+
def nested_array
|
16
|
+
[OpenStruct.new(sku: '1234')]
|
17
|
+
end
|
18
|
+
|
19
|
+
def nested_obj
|
20
|
+
OpenStruct.new(sku: '4321', price: OpenStruct.new(base: 1200))
|
21
|
+
end
|
22
|
+
|
23
|
+
def empty_array
|
24
|
+
[]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
it 'returns a hash' do
|
30
|
+
schema = schema_with { title }
|
31
|
+
|
32
|
+
expect(schema.marshal).to be_a Hash
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'marshals objects methods' do
|
36
|
+
schema = schema_with do
|
37
|
+
title
|
38
|
+
description
|
39
|
+
end
|
40
|
+
|
41
|
+
expect(schema.marshal).to eq title: 'Title', description: 'Description'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'handles nil attributes' do
|
45
|
+
schema = schema_with do
|
46
|
+
title
|
47
|
+
description
|
48
|
+
not_found
|
49
|
+
end
|
50
|
+
response = { title: 'Title', description: 'Description', not_found: nil }
|
51
|
+
expect(schema.marshal).to eq response
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'accepts hash aliases' do
|
55
|
+
schema = schema_with do
|
56
|
+
title as: 'header'
|
57
|
+
description as: 'body'
|
58
|
+
end
|
59
|
+
|
60
|
+
expect(schema.marshal).to eq 'header' => 'Title', 'body' => 'Description'
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'accepts procs' do
|
64
|
+
schema = schema_with do
|
65
|
+
summary -> { "#{object.title} #{object.description}" }
|
66
|
+
end
|
67
|
+
|
68
|
+
expect(schema.marshal).to eq summary: 'Title Description'
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'handles nested objects' do
|
72
|
+
schema = schema_with do
|
73
|
+
nested_obj do
|
74
|
+
sku
|
75
|
+
price do
|
76
|
+
base
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
result = { nested_obj: { sku: '4321', price: { base: 1200 } } }
|
81
|
+
|
82
|
+
expect(schema.marshal).to eq result
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'handles nested objects with aliases' do
|
86
|
+
schema = schema_with do
|
87
|
+
nested_obj as: :cart do
|
88
|
+
sku
|
89
|
+
price do
|
90
|
+
base as: :cost
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
expect(schema.marshal).to eq cart: { sku: '4321', price: { cost: 1200 } }
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'handles collections of objects' do
|
99
|
+
schema = schema_with do
|
100
|
+
nested_array as: :cart do
|
101
|
+
sku
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
expect(schema.marshal).to eq cart: [{ sku: '1234' }]
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'handles empty collections' do
|
109
|
+
schema = schema_with do
|
110
|
+
title
|
111
|
+
empty_array
|
112
|
+
end
|
113
|
+
|
114
|
+
expect(schema.marshal).to eq title: 'Title', empty_array: []
|
115
|
+
end
|
116
|
+
|
117
|
+
def schema_with(&block)
|
118
|
+
Blockhead::Schema.define(Tester.new, &block)
|
119
|
+
end
|
120
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blockhead
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vincent Franco
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: debugger
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.6'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.6'
|
69
|
+
description: A simple DSL for marshaling objects into a schema
|
70
|
+
email:
|
71
|
+
- vince@freshivore.net
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .travis.yml
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- blockhead.gemspec
|
84
|
+
- lib/blockhead.rb
|
85
|
+
- lib/blockhead/extractors/abstract.rb
|
86
|
+
- lib/blockhead/extractors/block.rb
|
87
|
+
- lib/blockhead/extractors/enumerable.rb
|
88
|
+
- lib/blockhead/extractors/proc.rb
|
89
|
+
- lib/blockhead/extractors/value.rb
|
90
|
+
- lib/blockhead/marshaller.rb
|
91
|
+
- lib/blockhead/option_key.rb
|
92
|
+
- lib/blockhead/schema.rb
|
93
|
+
- lib/blockhead/value_extractor.rb
|
94
|
+
- lib/blockhead/version.rb
|
95
|
+
- spec/blockhead/extractors/abstract_spec.rb
|
96
|
+
- spec/blockhead/extractors/block_spec.rb
|
97
|
+
- spec/blockhead/extractors/enumerable_spec.rb
|
98
|
+
- spec/blockhead/extractors/proc_spec.rb
|
99
|
+
- spec/blockhead/extractors/value_spec.rb
|
100
|
+
- spec/blockhead/schema_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
homepage: https://github.com/vinniefranco/blockhead
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.2.2
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: A simple DSL for marshaling objects into a schema
|
126
|
+
test_files:
|
127
|
+
- spec/blockhead/extractors/abstract_spec.rb
|
128
|
+
- spec/blockhead/extractors/block_spec.rb
|
129
|
+
- spec/blockhead/extractors/enumerable_spec.rb
|
130
|
+
- spec/blockhead/extractors/proc_spec.rb
|
131
|
+
- spec/blockhead/extractors/value_spec.rb
|
132
|
+
- spec/blockhead/schema_spec.rb
|
133
|
+
- spec/spec_helper.rb
|