primalize 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 +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +43 -0
- data/LICENSE.txt +21 -0
- data/README.md +131 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/lib/primalize.rb +3 -0
- data/lib/primalize/many.rb +90 -0
- data/lib/primalize/single.rb +253 -0
- data/lib/primalize/version.rb +3 -0
- data/primalize.gemspec +27 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f9454407dd6619d27027fc4a5ef40252868dc8ae
|
4
|
+
data.tar.gz: e841a6d57e23800b2fc273141e58ededbe969205
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2574db91856d1145c4738ab69831a4862227908f857e78668970c5dd59efe6397a06f79a4d033b2f5a885961fdccbf7afeac4f8334c381524a6dbebe6d320e96
|
7
|
+
data.tar.gz: 7eb666e43e360456289f70537262c1134d966fe47bb9eb89e4cd4d1399a777277957df148182f84cb2724bdfcdb884d1b65794c384a7c11f3cb07ed564632152
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at jgaskins@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
primalize (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.2)
|
10
|
+
diff-lcs (1.3)
|
11
|
+
method_source (0.8.2)
|
12
|
+
pry (0.10.4)
|
13
|
+
coderay (~> 1.1.0)
|
14
|
+
method_source (~> 0.8.1)
|
15
|
+
slop (~> 3.4)
|
16
|
+
rake (10.5.0)
|
17
|
+
rspec (3.6.0)
|
18
|
+
rspec-core (~> 3.6.0)
|
19
|
+
rspec-expectations (~> 3.6.0)
|
20
|
+
rspec-mocks (~> 3.6.0)
|
21
|
+
rspec-core (3.6.0)
|
22
|
+
rspec-support (~> 3.6.0)
|
23
|
+
rspec-expectations (3.6.0)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.6.0)
|
26
|
+
rspec-mocks (3.6.0)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.6.0)
|
29
|
+
rspec-support (3.6.0)
|
30
|
+
slop (3.6.0)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
bundler (~> 1.16.a)
|
37
|
+
primalize!
|
38
|
+
pry
|
39
|
+
rake (~> 10.0)
|
40
|
+
rspec (~> 3.0)
|
41
|
+
|
42
|
+
BUNDLED WITH
|
43
|
+
1.16.0.pre.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Jamie Gaskins
|
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,131 @@
|
|
1
|
+
# Primalize
|
2
|
+
|
3
|
+
Primalize lets you de-evolve your objects into primitive values for serialization. The primary use case is to serialize them into JSON, but once it's in its primitive state, it can be converted into other formats such as XML or CSV.
|
4
|
+
|
5
|
+
Primalizers support type checking by letting you specify the types of the resulting properties:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class OrderSerializer < Primalize::Single
|
9
|
+
attributes(
|
10
|
+
id: integer,
|
11
|
+
customer_id: integer,
|
12
|
+
product_ids: array(integer),
|
13
|
+
status: enum(
|
14
|
+
'requested',
|
15
|
+
'payment_processed',
|
16
|
+
'awaiting_shipment',
|
17
|
+
'shipped',
|
18
|
+
'delivered',
|
19
|
+
),
|
20
|
+
signature_required: boolean,
|
21
|
+
shipping_address: object(
|
22
|
+
address1: string,
|
23
|
+
address2: optional(string),
|
24
|
+
city: string,
|
25
|
+
state: string,
|
26
|
+
zip: string,
|
27
|
+
),
|
28
|
+
created_at: timestamp,
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
OrderSerializer.new(order).call
|
33
|
+
# { id: ... }
|
34
|
+
```
|
35
|
+
|
36
|
+
You can also primalize a nested structure of response objects with `Primalize::Many`, replacing the type annotations with the classes of their respective serializers:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class PostResponseSerializer < Primalize::Many
|
40
|
+
attributes(
|
41
|
+
post: PostSerializer,
|
42
|
+
author: UserSerializer,
|
43
|
+
comments: enumerable(CommentSerializer), # Not just one comment, but *many*
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Instantiate it by passing in the pertinent values
|
48
|
+
serializer = PostResponseSerializer.new(
|
49
|
+
post: @post,
|
50
|
+
author: @post.author,
|
51
|
+
comments: @post.comments,
|
52
|
+
)
|
53
|
+
|
54
|
+
serializer.call
|
55
|
+
# {
|
56
|
+
# post: { ... },
|
57
|
+
# author: { ... },
|
58
|
+
# comments: [
|
59
|
+
# { ... },
|
60
|
+
# ],
|
61
|
+
# }
|
62
|
+
```
|
63
|
+
|
64
|
+
## Installation
|
65
|
+
|
66
|
+
Add this line to your application's Gemfile:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
gem 'primalize'
|
70
|
+
```
|
71
|
+
|
72
|
+
And then execute:
|
73
|
+
|
74
|
+
$ bundle
|
75
|
+
|
76
|
+
Or install it yourself as:
|
77
|
+
|
78
|
+
$ gem install primalize
|
79
|
+
|
80
|
+
## Usage
|
81
|
+
|
82
|
+
If you need to primalize a single object, you subclass `Primalize::Single` and specify the attributes and types of the result as in the example above. But reducing the object's attributes to a hash isn't all you do in most apps. You may also need to do some coercion. For example, if you have an object whose `city` isn't stored as a string but you need to translate it to one:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
class ShipmentSerializer < Primalize::Single
|
86
|
+
attributes(
|
87
|
+
city: string { |city| city.name },
|
88
|
+
# ...
|
89
|
+
)
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
You can also generate attributes that don't exist on the object being primalized by defining methods on the primalizer:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
class ShipmentSerializer < Primalize::Single
|
97
|
+
attributes(
|
98
|
+
payment_method: string,
|
99
|
+
)
|
100
|
+
|
101
|
+
def payment_method
|
102
|
+
if object.paid_with_card?
|
103
|
+
'credit_card'
|
104
|
+
elsif object.purchase_order?
|
105
|
+
'purchase_order'
|
106
|
+
elsif object.bill_later?
|
107
|
+
'invoice'
|
108
|
+
else
|
109
|
+
'unknown'
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
## Development
|
116
|
+
|
117
|
+
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.
|
118
|
+
|
119
|
+
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).
|
120
|
+
|
121
|
+
## Contributing
|
122
|
+
|
123
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jgaskins/primalize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
124
|
+
|
125
|
+
## License
|
126
|
+
|
127
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
128
|
+
|
129
|
+
## Code of Conduct
|
130
|
+
|
131
|
+
Everyone interacting in the Primalize project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jgaskins/primalize/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/primalize.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
module Primalize
|
2
|
+
class Many
|
3
|
+
def self.attributes attrs={}
|
4
|
+
@attributes ||= {}
|
5
|
+
add_attributes attrs
|
6
|
+
|
7
|
+
@attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.add_attributes attrs
|
11
|
+
return if attrs.none?
|
12
|
+
|
13
|
+
attrs.each do |attr, serializer_class|
|
14
|
+
if serializer_class.nil?
|
15
|
+
raise TypeError, "Serializer for #{self}##{attr} cannot be nil"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
@attributes.merge! attrs
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.enumerable serializer_class
|
23
|
+
Class.new(Enumerable) do
|
24
|
+
define_method :call do
|
25
|
+
@enumerable.map { |item| serializer_class.new(item).call }
|
26
|
+
end
|
27
|
+
|
28
|
+
define_singleton_method :inspect do
|
29
|
+
"enumerable(#{serializer_class.inspect})"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Enumerable
|
35
|
+
def initialize enumerable
|
36
|
+
validate! enumerable
|
37
|
+
|
38
|
+
@enumerable = enumerable
|
39
|
+
end
|
40
|
+
|
41
|
+
def validate! enumerable
|
42
|
+
unless %w(each map).all? { |msg| enumerable.respond_to? msg }
|
43
|
+
raise ArgumentError, "#{self.class.inspect} must receive an Enumerable object, received #{enumerable.inspect}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def call
|
48
|
+
raise RuntimeError,
|
49
|
+
"Called #{inspect}#call. Please use Primalize::Many.enumerable to create primalizers for this."
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.inspect
|
54
|
+
attrs = attributes
|
55
|
+
.map { |attr, serializer| "#{attr}: #{serializer.inspect}" }
|
56
|
+
.join(', ')
|
57
|
+
|
58
|
+
"#{name}(#{attrs})"
|
59
|
+
end
|
60
|
+
|
61
|
+
def initialize attributes
|
62
|
+
validate_attributes! attributes
|
63
|
+
|
64
|
+
@attributes = attributes
|
65
|
+
end
|
66
|
+
|
67
|
+
def validate_attributes! attributes
|
68
|
+
unless self.class.attributes.each_key.all? { |key| attributes[key] }
|
69
|
+
non_nil_keys = attributes
|
70
|
+
.select { |_attr, value| value }
|
71
|
+
.map { |attr, _value| attr }
|
72
|
+
|
73
|
+
missing_keys = self.class.attributes.keys - non_nil_keys
|
74
|
+
|
75
|
+
raise ArgumentError,
|
76
|
+
"#{self.class} is missing the following keys: #{missing_keys.map(&:inspect).join(', ')}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def call
|
81
|
+
self.class.attributes.each_with_object({}) do |(attr, serializer_class), hash|
|
82
|
+
hash[attr] = serializer_class.new(@attributes.fetch(attr)).call
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def to_json
|
87
|
+
call.to_json
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,253 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
module Primalize
|
5
|
+
class Single
|
6
|
+
@type_mismatch_handler = proc do |attr, type, value|
|
7
|
+
raise TypeError, "#{self}##{attr} is specified as #{type.inspect}, but is #{value.inspect}"
|
8
|
+
end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
|
12
|
+
def attributes attrs={}
|
13
|
+
@attributes ||= {}
|
14
|
+
|
15
|
+
add_attributes attrs
|
16
|
+
|
17
|
+
@attributes
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_attributes attrs
|
21
|
+
return if attrs.none?
|
22
|
+
|
23
|
+
@attributes.merge! attrs
|
24
|
+
|
25
|
+
attrs.each do |attr, type|
|
26
|
+
define_method attr do
|
27
|
+
value = type.coerce(object.public_send(attr))
|
28
|
+
if type === value
|
29
|
+
value
|
30
|
+
else
|
31
|
+
self.class.type_mismatch_handler.call attr, type, value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def integer &coerce
|
38
|
+
Integer.new(&coerce)
|
39
|
+
end
|
40
|
+
|
41
|
+
def string &coerce
|
42
|
+
String.new(&coerce)
|
43
|
+
end
|
44
|
+
|
45
|
+
def boolean &coerce
|
46
|
+
enum(true, false, &coerce)
|
47
|
+
end
|
48
|
+
|
49
|
+
def array *types, &coerce
|
50
|
+
Array.new(types, &coerce)
|
51
|
+
end
|
52
|
+
|
53
|
+
def object **types, &coerce
|
54
|
+
Object.new(types, &coerce)
|
55
|
+
end
|
56
|
+
|
57
|
+
def float &coerce
|
58
|
+
Float.new(&coerce)
|
59
|
+
end
|
60
|
+
|
61
|
+
def optional *types, &coerce
|
62
|
+
Optional.new(types, &coerce)
|
63
|
+
end
|
64
|
+
|
65
|
+
def enum *values, &coerce
|
66
|
+
Enum.new(values, &coerce)
|
67
|
+
end
|
68
|
+
|
69
|
+
def timestamp &coerce
|
70
|
+
Timestamp.new(&coerce)
|
71
|
+
end
|
72
|
+
|
73
|
+
def type_mismatch_handler= handler
|
74
|
+
@type_mismatch_handler = handler
|
75
|
+
end
|
76
|
+
|
77
|
+
def type_mismatch_handler
|
78
|
+
if @type_mismatch_handler
|
79
|
+
@type_mismatch_handler
|
80
|
+
else
|
81
|
+
superclass.type_mismatch_handler
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def inspect
|
86
|
+
"#{name}(#{attributes.map { |attr, type| "#{attr}: #{type.inspect}" }.join(', ') })"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
attr_reader :object
|
91
|
+
|
92
|
+
def initialize object
|
93
|
+
@object = object
|
94
|
+
end
|
95
|
+
|
96
|
+
def call
|
97
|
+
self.class.attributes.each_with_object({}) do |(attr, _), hash|
|
98
|
+
hash[attr] = public_send(attr)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# TYPES
|
103
|
+
|
104
|
+
DEFAULT_COERCION = proc { |arg| arg } # Don't coerce by default
|
105
|
+
|
106
|
+
module Type
|
107
|
+
def coerce *args
|
108
|
+
(@coercion || DEFAULT_COERCION).call(*args)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
class Integer
|
113
|
+
include Type
|
114
|
+
|
115
|
+
def initialize &coercion
|
116
|
+
@coercion = coercion
|
117
|
+
end
|
118
|
+
|
119
|
+
def === value
|
120
|
+
::Integer === value
|
121
|
+
end
|
122
|
+
|
123
|
+
def inspect
|
124
|
+
'integer'
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
class Float
|
129
|
+
include Type
|
130
|
+
|
131
|
+
def initialize &coercion
|
132
|
+
@coercion = coercion
|
133
|
+
end
|
134
|
+
|
135
|
+
def === value
|
136
|
+
::Float === value
|
137
|
+
end
|
138
|
+
|
139
|
+
def inspect
|
140
|
+
'float'
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
class String
|
145
|
+
include Type
|
146
|
+
|
147
|
+
def initialize &coercion
|
148
|
+
@coercion = coercion
|
149
|
+
end
|
150
|
+
|
151
|
+
def === value
|
152
|
+
::String === value
|
153
|
+
end
|
154
|
+
|
155
|
+
def inspect
|
156
|
+
'string'
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
class Array
|
161
|
+
include Type
|
162
|
+
|
163
|
+
def initialize types, &coercion
|
164
|
+
@types = types
|
165
|
+
@coercion = coercion
|
166
|
+
end
|
167
|
+
|
168
|
+
def === value
|
169
|
+
return false unless ::Array === value
|
170
|
+
value.all? do |item|
|
171
|
+
@types.any? { |type| type === item }
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def inspect
|
176
|
+
"array(#{@types.map(&:inspect).join(', ')})"
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
class Enum
|
181
|
+
include Type
|
182
|
+
|
183
|
+
def initialize values, &coercion
|
184
|
+
@values = values
|
185
|
+
@coercion = coercion
|
186
|
+
end
|
187
|
+
|
188
|
+
def === value
|
189
|
+
@values.include? value
|
190
|
+
end
|
191
|
+
|
192
|
+
def coerce value
|
193
|
+
super value, @values
|
194
|
+
end
|
195
|
+
|
196
|
+
def inspect
|
197
|
+
"enum(#{@values.map(&:inspect).join(', ')})"
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
class Object
|
202
|
+
include Type
|
203
|
+
|
204
|
+
def initialize types, &coercion
|
205
|
+
@types = types
|
206
|
+
@coercion = coercion || DEFAULT_COERCION
|
207
|
+
end
|
208
|
+
|
209
|
+
def === value
|
210
|
+
return false unless ::Hash === value
|
211
|
+
|
212
|
+
@types.all? do |attr, type|
|
213
|
+
type === value[attr]
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
def inspect
|
218
|
+
"object(#{@types.map { |attr, type| "#{attr}: #{type.inspect}" }.join(', ')})"
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
class Timestamp
|
223
|
+
include Type
|
224
|
+
|
225
|
+
TYPES = [Time, Date, DateTime].freeze
|
226
|
+
|
227
|
+
def === value
|
228
|
+
TYPES.any? { |type| type === value }
|
229
|
+
end
|
230
|
+
|
231
|
+
def inspect
|
232
|
+
'timestamp'
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
class Optional
|
237
|
+
include Type
|
238
|
+
|
239
|
+
def initialize types, &coercion
|
240
|
+
@types = types
|
241
|
+
@coercion = coercion
|
242
|
+
end
|
243
|
+
|
244
|
+
def === value
|
245
|
+
value.nil? || @types.any? { |type| type === value }
|
246
|
+
end
|
247
|
+
|
248
|
+
def inspect
|
249
|
+
"optional(#{@types.map(&:inspect).join(', ')})"
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
data/primalize.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "primalize/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "primalize"
|
8
|
+
spec.version = Primalize::VERSION
|
9
|
+
spec.authors = ["Jamie Gaskins"]
|
10
|
+
spec.email = ["jgaskins@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Type-checked serializers for your Ruby objects}
|
13
|
+
spec.homepage = "https://github.com/jgaskins/primalize"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.16.a"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
spec.add_development_dependency "pry"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: primalize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jamie Gaskins
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-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.16.a
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.16.a
|
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: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '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'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- jgaskins@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/primalize.rb
|
88
|
+
- lib/primalize/many.rb
|
89
|
+
- lib/primalize/single.rb
|
90
|
+
- lib/primalize/version.rb
|
91
|
+
- primalize.gemspec
|
92
|
+
homepage: https://github.com/jgaskins/primalize
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.6.13
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Type-checked serializers for your Ruby objects
|
116
|
+
test_files: []
|