serial 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/.travis.yml +8 -1
- data/.yardopts +1 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +12 -2
- data/README.md +110 -26
- data/Rakefile +6 -1
- data/bin/console +2 -6
- data/elabs-logo.png +0 -0
- data/lib/serial.rb +2 -0
- data/lib/serial/array_builder.rb +31 -6
- data/lib/serial/builder.rb +22 -8
- data/lib/serial/hash_builder.rb +60 -7
- data/lib/serial/rails_helpers.rb +31 -0
- data/lib/serial/serializer.rb +97 -9
- data/lib/serial/version.rb +2 -1
- data/serial.gemspec +5 -3
- metadata +37 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29eb0ebd62982d93650098a3f2f4f54f11879bf3
|
4
|
+
data.tar.gz: 6d516f85165f6a46f767925a48c4d40d5a0cb1c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60d63e4986fc120337231c0ad4e0f1145947d6975e856fe61924c6760f3bb29c60218aafef65899015c19fb1a23241bf83558d81ca6b398a0b10fe7f5123d657
|
7
|
+
data.tar.gz: 6922159d7d31c6e77abc2c9e1cc3f4b7b3fc3f1990afc8c6d5c11303e9364ef940ee21f3428a4be577764a476fd5c931b8507be74e673abd9f7b94e3b504f588
|
data/.rspec
CHANGED
data/.travis.yml
CHANGED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--asset elabs-logo.png
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
@@ -1,4 +1,14 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
2
|
|
3
|
-
# Specify your gem's dependencies in serial.gemspec
|
4
3
|
gemspec
|
4
|
+
|
5
|
+
group :development do
|
6
|
+
platform :ruby do
|
7
|
+
gem "sqlite3"
|
8
|
+
end
|
9
|
+
|
10
|
+
platform :jruby do
|
11
|
+
gem "jdbc-sqlite3"
|
12
|
+
gem "activerecord-jdbcsqlite3-adapter"
|
13
|
+
end
|
14
|
+
end
|
data/README.md
CHANGED
@@ -1,64 +1,148 @@
|
|
1
1
|
# Serial
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/elabs/serial.svg?branch=master)](http://travis-ci.org/elabs/serial)
|
4
|
+
[![Dependency Status](https://gemnasium.com/elabs/serial.svg)](https://gemnasium.com/elabs/serial)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/elabs/serial/badges/gpa.svg)](https://codeclimate.com/github/elabs/serial)
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/serial.svg)](http://badge.fury.io/rb/serial)
|
7
|
+
[![Inline docs](http://inch-ci.org/github/elabs/serial.svg?branch=master&style=shields)](http://inch-ci.org/github/elabs/serial)
|
5
8
|
|
9
|
+
*Psst, full documentation can be found at [rubydoc.info/gems/serial](http://www.rubydoc.info/gems/serial)*
|
10
|
+
|
11
|
+
Serial is a light-weight and simple serialization library. Its primary purpose is to generate primitive
|
12
|
+
datastructures from object graphs, in other words to help you serialize your data.
|
13
|
+
|
14
|
+
Serial is sponsored by [Elabs][].
|
15
|
+
|
16
|
+
[![elabs logo][]][Elabs]
|
17
|
+
|
18
|
+
[Elabs]: http://www.elabs.se/
|
19
|
+
[elabs logo]: ./elabs-logo.png?raw=true
|
6
20
|
|
7
21
|
## Installation
|
8
22
|
|
9
23
|
Add this line to your application's Gemfile:
|
10
24
|
|
11
25
|
```ruby
|
12
|
-
gem
|
26
|
+
gem "serial"
|
13
27
|
```
|
14
28
|
|
15
29
|
And then execute:
|
16
30
|
|
17
31
|
$ bundle
|
18
32
|
|
19
|
-
|
33
|
+
## The DSL
|
20
34
|
|
21
|
-
|
35
|
+
*Full reference: [Serial::HashBuilder](http://www.rubydoc.info/gems/serial/Serial/HashBuilder), [Serial::ArrayBuilder](http://www.rubydoc.info/gems/serial/Serial/ArrayBuilder).*
|
22
36
|
|
23
|
-
|
37
|
+
- All keys are turned into strings.
|
38
|
+
- There is no automatic camel-casing. You name your keys the way you want them.
|
24
39
|
|
25
|
-
|
40
|
+
### Simple attributes
|
26
41
|
|
27
42
|
``` ruby
|
28
|
-
|
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|
|
43
|
+
ProjectSerializer = Serial::Serializer.new do |h, project|
|
36
44
|
h.attribute(:id, project.id)
|
37
|
-
h.attribute(:
|
38
|
-
|
45
|
+
h.attribute(:displayName, project.display_name)
|
46
|
+
end # => { "id" => …, "displayName" => … }
|
47
|
+
```
|
48
|
+
|
49
|
+
### Nested attributes
|
39
50
|
|
40
|
-
|
41
|
-
|
42
|
-
|
51
|
+
``` ruby
|
52
|
+
ProjectSerializer = Serial::Serializer.new do |h, project|
|
53
|
+
h.attribute(:owner, project.owner) do |h, owner|
|
54
|
+
h.attribute(:name, owner.name)
|
43
55
|
end
|
56
|
+
end # => { "owner" => { "name" => … } }
|
57
|
+
```
|
44
58
|
|
59
|
+
### Collections
|
60
|
+
|
61
|
+
`#map` is a convenient method for serializing lists of items.
|
62
|
+
|
63
|
+
``` ruby
|
64
|
+
ProjectSerializer = Serial::Serializer.new do |h, project|
|
45
65
|
h.map(:assignments, project.assignments) do |h, assignment|
|
46
66
|
h.attribute(:id, assignment.id)
|
47
67
|
h.attribute(:duration, assignment.duration)
|
68
|
+
end
|
69
|
+
end # => { "assignments" => [{ "id" => …, "duration" => … }, …] }
|
70
|
+
```
|
48
71
|
|
49
|
-
|
50
|
-
|
72
|
+
The low-level interface powering `#map` is `#collection`.
|
73
|
+
|
74
|
+
``` ruby
|
75
|
+
ProjectSerializer = Serial::Serializer.new do |h, project|
|
76
|
+
h.collection(:indices) do |l|
|
77
|
+
l.element { |h| h.attribute(…) }
|
78
|
+
l.element { |h| h.attribute(…) }
|
79
|
+
|
80
|
+
l.collection do |l|
|
81
|
+
l.element { … }
|
82
|
+
l.element { … }
|
83
|
+
end
|
51
84
|
end
|
85
|
+
end # => { "indices" => [{…}, {…}, [{…}, {…}]] }
|
86
|
+
```
|
87
|
+
|
88
|
+
### Composition
|
89
|
+
|
90
|
+
You can compose serializers by passing them as blocks to the DSL methods.
|
52
91
|
|
92
|
+
``` ruby
|
93
|
+
PersonSerializer = Serial::Serializer.new do |h, person|
|
94
|
+
h.attribute(:name, person.name)
|
95
|
+
end # => { "name" => … }
|
96
|
+
|
97
|
+
ProjectSerializer = Serial::Serializer.new do |h, project|
|
98
|
+
h.attribute(:owner, project.owner, &PersonSerializer)
|
53
99
|
h.map(:people, project.people, &PersonSerializer)
|
54
|
-
end
|
100
|
+
end # { "owner" => { "name" => … }, "people" => [{ "name" => … }, …] }
|
101
|
+
```
|
102
|
+
|
103
|
+
## Using your serializers
|
104
|
+
|
105
|
+
*Full reference: [Serial::Serializer](http://www.rubydoc.info/gems/serial/Serial/Serializer).*
|
106
|
+
|
107
|
+
- The context parameter in the below examples (when using `#call` and `#map`) is optional, if not provided regular block scoping rules apply.
|
108
|
+
- Tip: include [Serial::RailsHelpers](http://www.rubydoc.info/gems/serial/Serial/RailsHelpers) in ApplicationController for a convenient `#serialize` method.
|
109
|
+
|
110
|
+
### Serializing a single object
|
111
|
+
|
112
|
+
``` ruby
|
113
|
+
project = Project.find(…)
|
114
|
+
context = self
|
115
|
+
ProjectSerializer.call(context, project) # => { … }
|
55
116
|
```
|
56
117
|
|
57
|
-
|
118
|
+
### Serializing a list of objects
|
58
119
|
|
59
120
|
``` ruby
|
60
|
-
|
61
|
-
|
121
|
+
projects = Project.all
|
122
|
+
context = self
|
123
|
+
ProjectSerializer.map(context, projects) # => [{ … }, …]
|
124
|
+
```
|
125
|
+
|
126
|
+
### Using with Rails
|
127
|
+
|
128
|
+
``` ruby
|
129
|
+
# app/controllers/project_controller.rb
|
130
|
+
class ProjectController < ApplicationController
|
131
|
+
include Serial::RailsHelpers
|
132
|
+
|
133
|
+
def show
|
134
|
+
project = Project.find(…)
|
135
|
+
|
136
|
+
# 1. Using helper from RailsHelpers.
|
137
|
+
render json: serialize(project)
|
138
|
+
|
139
|
+
# 2. Explicitly mentioning serializer using helper method.
|
140
|
+
render json: serialize(project, &ProjectSerializer)
|
141
|
+
|
142
|
+
# 3. Explicitly mentioning serializer.
|
143
|
+
render json: ProjectSerializer.call(self, project)
|
144
|
+
end
|
145
|
+
end
|
62
146
|
```
|
63
147
|
|
64
148
|
## Development
|
data/Rakefile
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
+
task :release => :spec # BEFORE bundler gem tasks.
|
2
|
+
|
1
3
|
require "bundler/gem_tasks"
|
2
|
-
require "rspec/core/rake_task"
|
3
4
|
|
5
|
+
require "rspec/core/rake_task"
|
4
6
|
RSpec::Core::RakeTask.new(:spec)
|
5
7
|
|
8
|
+
require "yard"
|
9
|
+
YARD::Rake::YardocTask.new(:doc)
|
10
|
+
|
6
11
|
task :default => :spec
|
data/bin/console
CHANGED
@@ -6,9 +6,5 @@ require "serial"
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start
|
9
|
+
require "pry"
|
10
|
+
Pry.start
|
data/elabs-logo.png
ADDED
Binary file
|
data/lib/serial.rb
CHANGED
data/lib/serial/array_builder.rb
CHANGED
@@ -1,18 +1,43 @@
|
|
1
1
|
module Serial
|
2
|
-
#
|
2
|
+
# A builder for building arrays. You most likely just want to look at the
|
3
|
+
# public API methods in this class.
|
3
4
|
class ArrayBuilder < Builder
|
4
|
-
|
5
|
+
# @api private
|
6
|
+
def initialize(context)
|
5
7
|
@context = context
|
6
8
|
@data = []
|
7
|
-
yield self
|
8
9
|
end
|
9
10
|
|
11
|
+
# @api public
|
12
|
+
# Serializes a hash item in a collection.
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# h.collection(…) do |l|
|
16
|
+
# l.element do |h|
|
17
|
+
# h.attribute(…)
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# @yield [builder]
|
22
|
+
# @yieldparam builder [HashBuilder]
|
10
23
|
def element(&block)
|
11
|
-
@data << build(
|
24
|
+
@data << HashBuilder.build(@context, &block)
|
12
25
|
end
|
13
26
|
|
14
|
-
|
15
|
-
|
27
|
+
# @api public
|
28
|
+
# Serializes a collection in a collection.
|
29
|
+
#
|
30
|
+
# @example
|
31
|
+
# h.collection(…) do |l|
|
32
|
+
# l.collection do |l|
|
33
|
+
# l.element { … }
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# @yield [builder]
|
38
|
+
# @yieldparam builder [ArrayBuilder]
|
39
|
+
def collection(&block)
|
40
|
+
@data << ArrayBuilder.build(@context, &block)
|
16
41
|
end
|
17
42
|
end
|
18
43
|
end
|
data/lib/serial/builder.rb
CHANGED
@@ -1,12 +1,32 @@
|
|
1
1
|
module Serial
|
2
2
|
# @api private
|
3
|
+
#
|
4
|
+
# Builder contains common methods to the serializer DSL.
|
3
5
|
class Builder
|
4
|
-
|
5
|
-
|
6
|
+
# Create the builder, execute the block inside it, and return its' data.
|
7
|
+
# Any superflous arguments are given to {#exec}.
|
8
|
+
#
|
9
|
+
# @param context [#instance_exec, nil] the context to execute block inside
|
10
|
+
# @yield (see #exec)
|
11
|
+
# @yieldparam (see #exec)
|
12
|
+
# @return [#data]
|
13
|
+
def self.build(context, *args, &block)
|
14
|
+
builder = new(context)
|
15
|
+
builder.exec(*args, &block)
|
16
|
+
builder.data
|
6
17
|
end
|
7
18
|
|
19
|
+
# Builder data, depends on what kind of builder it is.
|
20
|
+
#
|
21
|
+
# @return [Array, Hash]
|
8
22
|
attr_reader :data
|
9
23
|
|
24
|
+
# Executes a block in the configured context, if there is one, otherwise using regular closure scoping.
|
25
|
+
#
|
26
|
+
#
|
27
|
+
# @yield [self, *args]
|
28
|
+
# @yieldparam self [Builder] passes in self as the first parameter.
|
29
|
+
# @yieldparam *args superflous arguments are passed to the block.
|
10
30
|
def exec(*args, &block)
|
11
31
|
if @context
|
12
32
|
@context.instance_exec(self, *args, &block)
|
@@ -14,11 +34,5 @@ module Serial
|
|
14
34
|
block.call(self, *args)
|
15
35
|
end
|
16
36
|
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
37
|
end
|
24
38
|
end
|
data/lib/serial/hash_builder.rb
CHANGED
@@ -1,26 +1,79 @@
|
|
1
1
|
module Serial
|
2
|
-
#
|
2
|
+
# A builder for building hashes. You most likely just want to look at the
|
3
|
+
# public API methods in this class.
|
3
4
|
class HashBuilder < Builder
|
4
|
-
|
5
|
+
# @api private
|
6
|
+
def initialize(context)
|
5
7
|
@context = context
|
6
8
|
@data = {}
|
7
|
-
yield self
|
8
9
|
end
|
9
10
|
|
11
|
+
# @api public
|
12
|
+
# Declare an attribute.
|
13
|
+
#
|
14
|
+
# @example without block
|
15
|
+
# h.attribute(:id, 5) # => { "id" => 5 }
|
16
|
+
#
|
17
|
+
# @example nested attribute, with block
|
18
|
+
# h.attribute(:project, project) do |h, project|
|
19
|
+
# h.attribute(:name, project.name)
|
20
|
+
# end # => { "project" => { "name" => … } }
|
21
|
+
#
|
22
|
+
# @param key [#to_s]
|
23
|
+
# @param value
|
24
|
+
# @yield [builder, value] declare nested attribute if block is given
|
25
|
+
# @yieldparam builder [HashBuilder] (keep in mind the examples shadow the outer `h` variable)
|
26
|
+
# @yieldparam value
|
10
27
|
def attribute(key, value = nil, &block)
|
11
|
-
value = build(
|
28
|
+
value = HashBuilder.build(@context, value, &block) if block
|
12
29
|
@data[key.to_s] = value
|
13
30
|
end
|
14
31
|
|
32
|
+
# @api public
|
33
|
+
# Declare a collection attribute. This is a low-level method, see {#map} instead.
|
34
|
+
#
|
35
|
+
# @example
|
36
|
+
# h.collection(:people) do |l|
|
37
|
+
# l.element do |h|
|
38
|
+
# h.attribute(…)
|
39
|
+
# end
|
40
|
+
# l.element do |h|
|
41
|
+
# h.attribute(…)
|
42
|
+
# end
|
43
|
+
# l.collection do |l|
|
44
|
+
# l.element do |h|
|
45
|
+
# h.attribute(…)
|
46
|
+
# end
|
47
|
+
# end
|
48
|
+
# end # => { "people" => [{…}, {…}, [{…}]] }
|
49
|
+
#
|
50
|
+
# @see ArrayBuilder
|
51
|
+
# @param key [#to_s]
|
52
|
+
# @yieldparam builder [ArrayBuilder]
|
15
53
|
def collection(key, &block)
|
16
|
-
|
17
|
-
attribute(key, list)
|
54
|
+
attribute(key, ArrayBuilder.build(@context, &block))
|
18
55
|
end
|
19
56
|
|
57
|
+
# @api public
|
58
|
+
# Declare a collection attribute from a list of values.
|
59
|
+
#
|
60
|
+
# @example
|
61
|
+
# h.map(:people, project.people) do |h, person|
|
62
|
+
# h.attribute(:name, person.name)
|
63
|
+
# end # => { "people" => [{ "name" => … }] }
|
64
|
+
#
|
65
|
+
# @see #collection
|
66
|
+
# @param key [#to_s]
|
67
|
+
# @param list [#each]
|
68
|
+
# @yield [builder, value] yields each value from list to build an array of hashes
|
69
|
+
# @yieldparam builder [HashBuilder]
|
70
|
+
# @yieldparam value
|
20
71
|
def map(key, list, &block)
|
21
72
|
collection(key) do |builder|
|
22
73
|
list.each do |item|
|
23
|
-
builder.element
|
74
|
+
builder.element do |element|
|
75
|
+
element.exec(item, &block)
|
76
|
+
end
|
24
77
|
end
|
25
78
|
end
|
26
79
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Serial
|
2
|
+
# Helpers for using Serial with Rails.
|
3
|
+
module RailsHelpers
|
4
|
+
# Find the serializer for `model` and serialize it in the context of self.
|
5
|
+
#
|
6
|
+
# @example serializing a single object
|
7
|
+
# render json: { person: serialize(Person.first) }
|
8
|
+
#
|
9
|
+
# @example serializing multiple objects
|
10
|
+
# render json: { people: serialize(Person.all) }
|
11
|
+
#
|
12
|
+
# @example serializing with explicit context
|
13
|
+
# render json: { people: serialize(presenter, Person.all) }
|
14
|
+
#
|
15
|
+
# @example serializing with explicit serializer
|
16
|
+
# render json: { people: serialize(Person.all, &my_serializer) }
|
17
|
+
#
|
18
|
+
# @param context [#instance_exec]
|
19
|
+
# @param model [#model_name, #each?]
|
20
|
+
def serialize(context = self, model, &serializer)
|
21
|
+
serializer &&= Serializer.new(&serializer)
|
22
|
+
serializer ||= "#{model.model_name}Serializer".constantize
|
23
|
+
|
24
|
+
if model.respond_to?(:map)
|
25
|
+
serializer.map(context, model)
|
26
|
+
else
|
27
|
+
serializer.call(context, model)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/serial/serializer.rb
CHANGED
@@ -1,23 +1,111 @@
|
|
1
1
|
module Serial
|
2
|
+
# Using this class you build serializers.
|
2
3
|
class Serializer
|
4
|
+
# Create a new Serializer, using the block as instructions.
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# # app/serializers/person_serializer.rb
|
8
|
+
# PersonSerializer = Serial::Serializer.new do |h, person|
|
9
|
+
# h.attribute(:name, person.name)
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# @yield [builder, value]
|
13
|
+
# @yieldparam builder [HashBuilder]
|
14
|
+
# @yieldparam value from {#call} or {#map}
|
3
15
|
def initialize(&block)
|
16
|
+
unless block_given?
|
17
|
+
raise ArgumentError, "instructions (block) is required"
|
18
|
+
end
|
19
|
+
|
4
20
|
@block = block
|
21
|
+
@to_proc = method(:to_proc_implementation).to_proc
|
22
|
+
end
|
23
|
+
|
24
|
+
# Serialize an object with this serializer, optionally within a context.
|
25
|
+
#
|
26
|
+
# @example with context, the serializer block is evaluated inside the context
|
27
|
+
# # app/serializers/person_serializer.rb
|
28
|
+
# PersonSerializer = Serial::Serializer.new do |h, person|
|
29
|
+
# h.attribute(:id, person.id)
|
30
|
+
# h.attribute(:url, people_url(person))
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# # app/controllers/person_controller.rb
|
34
|
+
# def show
|
35
|
+
# person = Person.find(…)
|
36
|
+
# render json: PersonSerializer.call(self, person)
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# @example without context, the serializer block is evaluated using normal closure rules
|
40
|
+
# # app/models/person.rb
|
41
|
+
# class Person
|
42
|
+
# Serializer = Serial::Serializer.new do |h, person|
|
43
|
+
# h.attribute(:id, person.id)
|
44
|
+
# h.attribute(:available_roles, available_roles)
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# def self.available_roles
|
48
|
+
# …
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# # app/controllers/person_controller.rb
|
53
|
+
# def show
|
54
|
+
# person = Person.find(…)
|
55
|
+
# render json: Person::Serializer.call(person)
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# @param context [#instance_exec, nil] context to execute serializer in, or nil to use regular block closure rules.
|
59
|
+
# @param value
|
60
|
+
# @return [Hash]
|
61
|
+
def call(context = nil, value)
|
62
|
+
HashBuilder.build(context, value, &@block)
|
5
63
|
end
|
6
64
|
|
65
|
+
# Serialize a list of objects with this serializer, optionally within a context.
|
66
|
+
#
|
67
|
+
# @example
|
68
|
+
# # app/serializers/person_serializer.rb
|
69
|
+
# PersonSerializer = Serial::Serializer.new do |h, person|
|
70
|
+
# h.attribute(:id, person.id)
|
71
|
+
# h.attribute(:url, people_url(person))
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
# # app/controllers/person_controller.rb
|
75
|
+
# def index
|
76
|
+
# people = Person.all
|
77
|
+
# render json: PersonSerializer.map(self, people)
|
78
|
+
# end
|
79
|
+
#
|
80
|
+
# @see #call see #call for an explanation of the context parameter
|
81
|
+
# @param context (see #call)
|
82
|
+
# @param list [#map]
|
83
|
+
# @return [Array<Hash>]
|
7
84
|
def map(context = nil, list)
|
8
85
|
list.map { |item| call(context, item) }
|
9
86
|
end
|
10
87
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
88
|
+
# Serializer composition!
|
89
|
+
#
|
90
|
+
# @example
|
91
|
+
# # app/serializers/person_serializer.rb
|
92
|
+
# PersonSerializer = Serial::Serializer.new do |h, person|
|
93
|
+
# h.attribute(:name, person.name)
|
94
|
+
# end
|
95
|
+
#
|
96
|
+
# # app/serializers/project_serializer.rb
|
97
|
+
# ProjectSerializer = Serial::Serializer.new do |h, project|
|
98
|
+
# h.attribute(:owner, project.owner, &PersonSerializer)
|
99
|
+
# h.map(:people, project.people, &PersonSerializer)
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# @return [Proc]
|
103
|
+
attr_reader :to_proc
|
104
|
+
|
105
|
+
private
|
17
106
|
|
18
|
-
def
|
19
|
-
|
20
|
-
proc { |builder, value| builder.exec(value, &block) }
|
107
|
+
def to_proc_implementation(builder, *args)
|
108
|
+
builder.exec(*args, &@block)
|
21
109
|
end
|
22
110
|
end
|
23
111
|
end
|
data/lib/serial/version.rb
CHANGED
data/serial.gemspec
CHANGED
@@ -6,11 +6,11 @@ require 'serial/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "serial"
|
8
8
|
spec.version = Serial::VERSION
|
9
|
-
spec.authors = ["Jonas Nicklas", "Kim Burgestrand"]
|
10
|
-
spec.email = ["jonas@elabs.se", "kim@elabs.se"]
|
9
|
+
spec.authors = ["Elabs", "Jonas Nicklas", "Kim Burgestrand"]
|
10
|
+
spec.email = ["dev@elabs.se", "jonas@elabs.se", "kim@elabs.se"]
|
11
11
|
spec.license = "MIT"
|
12
12
|
|
13
|
-
spec.summary = %q{Plain old Ruby for generating
|
13
|
+
spec.summary = %q{Plain old Ruby for generating primitive data structures from object graphs.}
|
14
14
|
spec.homepage = "https://github.com/elabs/serial"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
@@ -19,7 +19,9 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "pry", "~> 0.10"
|
22
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
24
|
spec.add_development_dependency "rspec", "~> 3.2"
|
24
25
|
spec.add_development_dependency "yard", "~> 0.8"
|
26
|
+
spec.add_development_dependency "activerecord", "~> 4.0"
|
25
27
|
end
|
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serial
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
+
- Elabs
|
7
8
|
- Jonas Nicklas
|
8
9
|
- Kim Burgestrand
|
9
10
|
autorequire:
|
10
11
|
bindir: exe
|
11
12
|
cert_chain: []
|
12
|
-
date: 2015-09-
|
13
|
+
date: 2015-09-18 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: bundler
|
@@ -25,6 +26,20 @@ dependencies:
|
|
25
26
|
- - "~>"
|
26
27
|
- !ruby/object:Gem::Version
|
27
28
|
version: '1.10'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: pry
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - "~>"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0.10'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0.10'
|
28
43
|
- !ruby/object:Gem::Dependency
|
29
44
|
name: rake
|
30
45
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,8 +82,23 @@ dependencies:
|
|
67
82
|
- - "~>"
|
68
83
|
- !ruby/object:Gem::Version
|
69
84
|
version: '0.8'
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: activerecord
|
87
|
+
requirement: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - "~>"
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '4.0'
|
92
|
+
type: :development
|
93
|
+
prerelease: false
|
94
|
+
version_requirements: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - "~>"
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '4.0'
|
70
99
|
description:
|
71
100
|
email:
|
101
|
+
- dev@elabs.se
|
72
102
|
- jonas@elabs.se
|
73
103
|
- kim@elabs.se
|
74
104
|
executables: []
|
@@ -78,15 +108,19 @@ files:
|
|
78
108
|
- ".gitignore"
|
79
109
|
- ".rspec"
|
80
110
|
- ".travis.yml"
|
111
|
+
- ".yardopts"
|
112
|
+
- CHANGELOG.md
|
81
113
|
- Gemfile
|
82
114
|
- README.md
|
83
115
|
- Rakefile
|
84
116
|
- bin/console
|
85
117
|
- bin/setup
|
118
|
+
- elabs-logo.png
|
86
119
|
- lib/serial.rb
|
87
120
|
- lib/serial/array_builder.rb
|
88
121
|
- lib/serial/builder.rb
|
89
122
|
- lib/serial/hash_builder.rb
|
123
|
+
- lib/serial/rails_helpers.rb
|
90
124
|
- lib/serial/serializer.rb
|
91
125
|
- lib/serial/version.rb
|
92
126
|
- serial.gemspec
|
@@ -113,6 +147,6 @@ rubyforge_project:
|
|
113
147
|
rubygems_version: 2.4.6
|
114
148
|
signing_key:
|
115
149
|
specification_version: 4
|
116
|
-
summary: Plain old Ruby for generating
|
150
|
+
summary: Plain old Ruby for generating primitive data structures from object graphs.
|
117
151
|
test_files: []
|
118
152
|
has_rdoc:
|