serial 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +3 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +85 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/serial.rb +8 -0
- data/lib/serial/array_builder.rb +18 -0
- data/lib/serial/builder.rb +24 -0
- data/lib/serial/hash_builder.rb +28 -0
- data/lib/serial/serializer.rb +23 -0
- data/lib/serial/version.rb +3 -0
- data/serial.gemspec +25 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 726a364ac913fa10ae6b515961c6d868ae2c3fb3
|
4
|
+
data.tar.gz: e4fab7b274b159f7fcc6127213d5a14262f8da47
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5e1e2d538b088082e41b9bd873d455074cd5ce85812179978cbe4aedbc63df29f1898966a9040f0f185f11e980ca41ae46d8360d9d68e891981610dd4f69f75a
|
7
|
+
data.tar.gz: f0308bd4d267cea9bfb3eb47df0a6722fd96eea4430616dfae7e1f34f9f94193c6d4db14617111cf93082fd9b1260018ec48bc381581f17f124d1028e28d1d23
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# Serial
|
2
|
+
|
3
|
+
Serial is a serialization library. Its primary purpose is to generate simple
|
4
|
+
datastructures from object graphs.
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'serial'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install serial
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
You can set up your serializers like this:
|
26
|
+
|
27
|
+
``` ruby
|
28
|
+
# app/serializers/person_serializer.rb
|
29
|
+
PersonSerializer = Serializer.new do |h, person|
|
30
|
+
h.attribute(:id, person.id)
|
31
|
+
h.attribute(:name, person.name)
|
32
|
+
end
|
33
|
+
|
34
|
+
# app/serializers/project_serializer.rb
|
35
|
+
ProjectSerializer = Serializer.new do |h, project|
|
36
|
+
h.attribute(:id, project.id)
|
37
|
+
h.attribute(:projectName, project.name)
|
38
|
+
h.attribute(:description, project.description)
|
39
|
+
|
40
|
+
h.attribute(:client, project.client) do |h, client|
|
41
|
+
h.attribute(:id, client.id)
|
42
|
+
h.attribute(:name, client.name)
|
43
|
+
end
|
44
|
+
|
45
|
+
h.map(:assignments, project.assignments) do |h, assignment|
|
46
|
+
h.attribute(:id, assignment.id)
|
47
|
+
h.attribute(:duration, assignment.duration)
|
48
|
+
|
49
|
+
# This is how you compose serializers.
|
50
|
+
h.attribute(:person, assignment.person, &PersonSerializer)
|
51
|
+
end
|
52
|
+
|
53
|
+
h.map(:people, project.people, &PersonSerializer)
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
Whenever you need to use them you invoke them like this:
|
58
|
+
|
59
|
+
``` ruby
|
60
|
+
person = Person.find(1)
|
61
|
+
render json: PersonSerializer.call(self, person)
|
62
|
+
```
|
63
|
+
|
64
|
+
## Development
|
65
|
+
|
66
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
67
|
+
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
68
|
+
prompt that will allow you to experiment.
|
69
|
+
|
70
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
71
|
+
release a new version, update the version number in `version.rb`, and then run
|
72
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
73
|
+
git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
Bug reports and pull requests are welcome on GitHub at
|
78
|
+
https://github.com/elabs/serial. This project is intended to be a safe,
|
79
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
80
|
+
the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
81
|
+
|
82
|
+
## License
|
83
|
+
|
84
|
+
The gem is available as open source under the terms of the
|
85
|
+
[MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "serial"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/serial.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Serial
|
2
|
+
# @api private
|
3
|
+
class ArrayBuilder < Builder
|
4
|
+
def initialize(context, &block)
|
5
|
+
@context = context
|
6
|
+
@data = []
|
7
|
+
yield self
|
8
|
+
end
|
9
|
+
|
10
|
+
def element(&block)
|
11
|
+
@data << build(HashBuilder, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def collection(key, &block)
|
15
|
+
@data << build(ArrayBuilder, &block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Serial
|
2
|
+
# @api private
|
3
|
+
class Builder
|
4
|
+
def self.build(context, &block)
|
5
|
+
new(context, &block).data
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :data
|
9
|
+
|
10
|
+
def exec(*args, &block)
|
11
|
+
if @context
|
12
|
+
@context.instance_exec(self, *args, &block)
|
13
|
+
else
|
14
|
+
block.call(self, *args)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def build(builder_klass, *args, &block)
|
19
|
+
builder_klass.build(@context) do |builder|
|
20
|
+
builder.exec(*args, &block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Serial
|
2
|
+
# @api private
|
3
|
+
class HashBuilder < Builder
|
4
|
+
def initialize(context, &block)
|
5
|
+
@context = context
|
6
|
+
@data = {}
|
7
|
+
yield self
|
8
|
+
end
|
9
|
+
|
10
|
+
def attribute(key, value = nil, &block)
|
11
|
+
value = build(HashBuilder, value, &block) if block
|
12
|
+
@data[key.to_s] = value
|
13
|
+
end
|
14
|
+
|
15
|
+
def collection(key, &block)
|
16
|
+
list = build(ArrayBuilder, &block)
|
17
|
+
attribute(key, list)
|
18
|
+
end
|
19
|
+
|
20
|
+
def map(key, list, &block)
|
21
|
+
collection(key) do |builder|
|
22
|
+
list.each do |item|
|
23
|
+
builder.element { |element| element.exec(item, &block) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Serial
|
2
|
+
class Serializer
|
3
|
+
def initialize(&block)
|
4
|
+
@block = block
|
5
|
+
end
|
6
|
+
|
7
|
+
def map(list)
|
8
|
+
list.map { |item| call(item) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(context = nil, value)
|
12
|
+
block = @block
|
13
|
+
HashBuilder.build(context) do |builder|
|
14
|
+
builder.exec(value, &block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_proc
|
19
|
+
block = @block
|
20
|
+
proc { |builder, value| builder.exec(value, &block) }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/serial.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 'serial/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "serial"
|
8
|
+
spec.version = Serial::VERSION
|
9
|
+
spec.authors = ["Jonas Nicklas", "Kim Burgestrand"]
|
10
|
+
spec.email = ["jonas@elabs.se", "kim@elabs.se"]
|
11
|
+
spec.license = "MIT"
|
12
|
+
|
13
|
+
spec.summary = %q{Plain old Ruby for generating simple data structures from object graphs.}
|
14
|
+
spec.homepage = "https://github.com/elabs/serial"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
24
|
+
spec.add_development_dependency "yard", "~> 0.8"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serial
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonas Nicklas
|
8
|
+
- Kim Burgestrand
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-09-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.10'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.10'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3.2'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '3.2'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: yard
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0.8'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.8'
|
70
|
+
description:
|
71
|
+
email:
|
72
|
+
- jonas@elabs.se
|
73
|
+
- kim@elabs.se
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- ".rspec"
|
80
|
+
- ".travis.yml"
|
81
|
+
- Gemfile
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/serial.rb
|
87
|
+
- lib/serial/array_builder.rb
|
88
|
+
- lib/serial/builder.rb
|
89
|
+
- lib/serial/hash_builder.rb
|
90
|
+
- lib/serial/serializer.rb
|
91
|
+
- lib/serial/version.rb
|
92
|
+
- serial.gemspec
|
93
|
+
homepage: https://github.com/elabs/serial
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.4.6
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Plain old Ruby for generating simple data structures from object graphs.
|
117
|
+
test_files: []
|
118
|
+
has_rdoc:
|