waveguide 0.1.0
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/.DS_Store +0 -0
- data/.fasterer.yml +3 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +81 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/waveguide.rb +6 -0
- data/lib/waveguide/serializer.rb +109 -0
- data/lib/waveguide/version.rb +3 -0
- data/waveguide.gemspec +31 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 68aafe56c56693aa78e51c25183674813758da5a
|
4
|
+
data.tar.gz: d53ee7ee8b0fc06ac94381004b5020e2a1f2e69a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 32338cc59b822e86be83a16d6d4cb6bd24189756feb026248530565734989bcf21f4dce9bdcdaa81197947c4cf45bca98bb4cafdce6974fe5c24bbfa4f1d73df
|
7
|
+
data.tar.gz: 02c7ffd90f624a18ac4c517b756f520b1f5b7172b4fb70f41a925dcc7989b4abc9c70d28cd7f469469ab3f6aec60da8fae99300ca5cc5c0e059327cdcdceebe6
|
data/.DS_Store
ADDED
Binary file
|
data/.fasterer.yml
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Aaron Gough
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Waveguide
|
2
|
+
|
3
|
+
Waveguide is a fast serializer library for Ruby. It's designed to be partly compatible with ActiveModelSerializers, but much faster:
|
4
|
+
|
5
|
+
```shell
|
6
|
+
300x Events (8701 objects) (Waveguide) : 111.56 ms
|
7
|
+
300x Events (8701 objects) (AMS) : 774.62 ms
|
8
|
+
```
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
class PlayerSerializer < Waveguide::Serializer
|
14
|
+
# define a single attribute
|
15
|
+
attribute :first_name
|
16
|
+
|
17
|
+
# override the source of an attribute with a block
|
18
|
+
attribute :last_name { object.parent.last_name }
|
19
|
+
|
20
|
+
attribute :full_name do
|
21
|
+
"#{object.first_name} #{object.last_name}"
|
22
|
+
end
|
23
|
+
|
24
|
+
# define a list of attributes
|
25
|
+
attributes :first_name, :last_name
|
26
|
+
|
27
|
+
# define a singular relation
|
28
|
+
has_one :country, serializer: CountrySerializer
|
29
|
+
|
30
|
+
# define a singular relation with a different key name
|
31
|
+
has_one :country, as: :origin_country, serializer: CountrySerializer
|
32
|
+
|
33
|
+
# define a multiple relation
|
34
|
+
has_many :games, serializer: GameSerializer
|
35
|
+
|
36
|
+
# define a multiple relation with a different key name
|
37
|
+
has_many :games, as: :events, serializer: GameSerializer
|
38
|
+
|
39
|
+
# conditionally include an attribute
|
40
|
+
include? :country do
|
41
|
+
object.respond_to?(:country)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
Example:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class EventSerializer < Waveguide::Serializer
|
50
|
+
has_many :players, serializer: PlayerSerializer
|
51
|
+
attributes :start_date, :end_date
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
## Installation
|
56
|
+
|
57
|
+
Add this line to your application's Gemfile:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
gem 'waveguide'
|
61
|
+
```
|
62
|
+
|
63
|
+
And then execute:
|
64
|
+
|
65
|
+
$ bundle
|
66
|
+
|
67
|
+
Or install it yourself as:
|
68
|
+
|
69
|
+
$ gem install waveguide
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
## Development
|
74
|
+
|
75
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
76
|
+
|
77
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
78
|
+
|
79
|
+
## License
|
80
|
+
|
81
|
+
The gem is available as open source under the terms of the [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 "waveguide"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/waveguide.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
module Waveguide
|
2
|
+
class Serializer
|
3
|
+
|
4
|
+
attr_reader :object, :scope
|
5
|
+
|
6
|
+
def initialize(object, scope = nil)
|
7
|
+
@object = object
|
8
|
+
@scope = scope
|
9
|
+
end
|
10
|
+
|
11
|
+
def as_json
|
12
|
+
self.class.attributes_to_serialize ||= []
|
13
|
+
self.class.conditional_attributes ||= []
|
14
|
+
self.class.serializer_methods ||= {}
|
15
|
+
|
16
|
+
output = {}
|
17
|
+
|
18
|
+
self.class.attributes_to_serialize.each do |key|
|
19
|
+
output[key] = self.send(key)
|
20
|
+
end
|
21
|
+
|
22
|
+
self.class.conditional_attributes.each do |key|
|
23
|
+
output[key] = self.send(key) if self.send("include_#{key}?")
|
24
|
+
end
|
25
|
+
|
26
|
+
output
|
27
|
+
end
|
28
|
+
|
29
|
+
class << self
|
30
|
+
|
31
|
+
attr_accessor :attributes_to_serialize,
|
32
|
+
:conditional_attributes,
|
33
|
+
:serializer_methods
|
34
|
+
|
35
|
+
def inherited(base)
|
36
|
+
super
|
37
|
+
base.attributes_to_serialize = attributes_to_serialize.dup || []
|
38
|
+
base.conditional_attributes = conditional_attributes.dup || []
|
39
|
+
base.serializer_methods = serializer_methods.dup || {}
|
40
|
+
|
41
|
+
(self.serializer_methods || {}).each do |key, block|
|
42
|
+
base.send(:define_method, key, &block)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def add_attribute(attr_name)
|
47
|
+
return if self.conditional_attributes.include?(attr_name)
|
48
|
+
|
49
|
+
self.attributes_to_serialize ||= []
|
50
|
+
self.attributes_to_serialize << attr_name
|
51
|
+
self.attributes_to_serialize.uniq!
|
52
|
+
end
|
53
|
+
|
54
|
+
def add_conditional_attribute(attr_name)
|
55
|
+
self.conditional_attributes ||= []
|
56
|
+
self.conditional_attributes << attr_name
|
57
|
+
self.conditional_attributes.uniq!
|
58
|
+
|
59
|
+
self.attributes_to_serialize.delete(attr_name)
|
60
|
+
end
|
61
|
+
|
62
|
+
def define_serializer_method(name, &block)
|
63
|
+
self.serializer_methods ||= {}
|
64
|
+
self.serializer_methods[name] = block
|
65
|
+
|
66
|
+
define_method(name, &block)
|
67
|
+
end
|
68
|
+
|
69
|
+
def attributes(*attr_names)
|
70
|
+
attr_names.each do |attr_name|
|
71
|
+
attribute(attr_name)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def attribute(attr_name, &block)
|
76
|
+
add_attribute(attr_name)
|
77
|
+
|
78
|
+
if block.nil?
|
79
|
+
define_serializer_method(attr_name) { object.send(attr_name) }
|
80
|
+
else
|
81
|
+
define_serializer_method(attr_name, &block)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def has_one(relation_name, as: nil, serializer:)
|
86
|
+
key = as || relation_name
|
87
|
+
add_attribute(key)
|
88
|
+
define_serializer_method(key) do
|
89
|
+
serializer.new(object.send(relation_name)).as_json
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def has_many(relation_name, as: nil, serializer:)
|
94
|
+
key = as || relation_name
|
95
|
+
add_attribute(key)
|
96
|
+
define_serializer_method(key) do
|
97
|
+
object.send(relation_name).map do |item|
|
98
|
+
serializer.new(item).as_json
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def include?(attr_name, &block)
|
104
|
+
add_conditional_attribute(attr_name)
|
105
|
+
define_serializer_method("include_#{attr_name}?", &block)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
data/waveguide.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "waveguide/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "waveguide"
|
8
|
+
spec.version = Waveguide::VERSION
|
9
|
+
spec.authors = ["Aaron Gough"]
|
10
|
+
spec.email = ["aaron@aarongough.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{An ultra-fast serialization library.}
|
13
|
+
spec.description = %q{An ultra-fast serialization library.}
|
14
|
+
spec.homepage = "http://github.com/aarongough/waveguide."
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
spec.add_development_dependency "active_model_serializers", "~> 0.10.0"
|
29
|
+
spec.add_development_dependency "ruby-prof"
|
30
|
+
spec.add_development_dependency "fasterer"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: waveguide
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Gough
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-12 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.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: active_model_serializers
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.10.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.10.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: ruby-prof
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: fasterer
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: An ultra-fast serialization library.
|
98
|
+
email:
|
99
|
+
- aaron@aarongough.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".DS_Store"
|
105
|
+
- ".fasterer.yml"
|
106
|
+
- ".gitignore"
|
107
|
+
- ".rspec"
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- lib/waveguide.rb
|
115
|
+
- lib/waveguide/serializer.rb
|
116
|
+
- lib/waveguide/version.rb
|
117
|
+
- waveguide.gemspec
|
118
|
+
homepage: http://github.com/aarongough/waveguide.
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.6.10
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: An ultra-fast serialization library.
|
142
|
+
test_files: []
|