primalize 0.3.4 → 0.3.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 732b999e4ee12455d64ccf1b683bbb743091b7379358b9a1e5ac773a7291c0a1
4
- data.tar.gz: 014675a6a41e85d6d82a3c3e48b621016eb9582cc6d81655879dbaf5f96322b4
3
+ metadata.gz: 1e2195339a7d20216051be9c268d056de17fb98290fb17c23291f3762d17d08e
4
+ data.tar.gz: 06b4b92e05b04f1dc4094eb1d7ff448bdf464e49e8c696d96bb158ca63299034
5
5
  SHA512:
6
- metadata.gz: f778b6a13b836a7d8099dff6b6de0fb3745e20372d1b7d373b76aa9e7f0924f0848256a8378249d3b7dbcad6eeeee8a324f609578a147aeca1278fd3f9aa1c6a
7
- data.tar.gz: 52cbddd5ebb3d3246d159c9ae5234e23fa2608ecb1a88902a05bcef2088dcc1abbd21938bf84e904b975741c722d7636c41d4187eee32ea55771be50c5adcbc0
6
+ metadata.gz: 5eb859b12f0b503fc6d1a4b1ca17911cf5d96b6fd7bfce73a864e1fcfbc1ec58e4f4027b3c1a685ed528388cebd6a3961f97ef19fe5fd00a1fbaac26bec90100
7
+ data.tar.gz: a145520259b1d375acd3cda9a1472463e551c0b35ec8537d163886ee0b47d140a0aa1ef09b7c44389f3ad51bd6ace17527af431abbc2bd996154f22167c90edf
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Primalize
2
2
 
3
+ [![Join the chat at https://gitter.im/jgaskins/primalize](https://badges.gitter.im/jgaskins/primalize.svg)](https://gitter.im/jgaskins/primalize?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4
+
3
5
  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
6
 
5
7
  Primalizers support type checking by letting you specify the types of the resulting properties:
@@ -1,3 +1,5 @@
1
+ require 'json'
2
+
1
3
  module Primalize
2
4
  class Many
3
5
  def self.attributes attrs={}
@@ -19,6 +21,11 @@ module Primalize
19
21
  @attributes.merge! attrs
20
22
  end
21
23
 
24
+ # Pass on our attributes to child classes
25
+ def self.inherited klass
26
+ klass.attributes attributes
27
+ end
28
+
22
29
  def self.enumerable serializer_class
23
30
  Class.new(Enumerable) do
24
31
  define_method :call do
@@ -9,13 +9,16 @@ module Primalize
9
9
  end
10
10
 
11
11
  class << self
12
+ def attributes **attrs
13
+ _attributes **attrs
14
+ end
12
15
 
13
- def attributes attrs={}
14
- @attributes ||= if self.equal? Primalize::Single
15
- {}
16
- else
17
- superclass.attributes.dup
18
- end
16
+ def _attributes **attrs
17
+ @attributes ||= if self.equal? Primalize::Single
18
+ {}
19
+ else
20
+ superclass._attributes.dup
21
+ end
19
22
 
20
23
  add_attributes attrs
21
24
 
@@ -78,6 +81,14 @@ module Primalize
78
81
  Any.new(types, &coerce)
79
82
  end
80
83
 
84
+ def all *types, &coerce
85
+ All.new(types, &coerce)
86
+ end
87
+
88
+ def match matcher, &coercion
89
+ Match.new(matcher, &coercion)
90
+ end
91
+
81
92
  def primalize primalizer, &coerce
82
93
  Primalizer.new(primalizer, &coerce)
83
94
  end
@@ -107,7 +118,7 @@ module Primalize
107
118
  end
108
119
 
109
120
  def call
110
- self.class.attributes.each_with_object({}) do |(attr, type), hash|
121
+ self.class._attributes.each_with_object({}) do |(attr, type), hash|
111
122
  value = public_send(attr)
112
123
 
113
124
  hash[attr] = if type === value
@@ -125,12 +136,12 @@ module Primalize
125
136
 
126
137
  # CONVERSION
127
138
 
128
- def to_json
129
- call.to_json
139
+ def to_json(options=nil)
140
+ call.to_json(options)
130
141
  end
131
142
 
132
143
  def csv_headers
133
- self.class.attributes.keys.map(&:to_s)
144
+ self.class._attributes.keys.map(&:to_s)
134
145
  end
135
146
 
136
147
  def to_csv
@@ -289,9 +300,9 @@ module Primalize
289
300
  return @coercion.call(hash)
290
301
  end
291
302
 
292
- if hash.respond_to? :map
303
+ if hash.respond_to? :each_with_object
293
304
  hash.each_with_object({}) do |(key, value), h|
294
- _, type = @types.find { |_, type| type === value }
305
+ type = @types[key]
295
306
 
296
307
  h[key] = if type.respond_to? :coerce
297
308
  type.coerce(value)
@@ -397,5 +408,39 @@ module Primalize
397
408
  "any#{@types.empty? ? nil : params}"
398
409
  end
399
410
  end
411
+
412
+ class All
413
+ include Type
414
+
415
+ def initialize types, &coercion
416
+ @types = types
417
+ @coercion = coercion
418
+ end
419
+
420
+ def === value
421
+ @types.all? { |type| type === value }
422
+ end
423
+
424
+ def inspect
425
+ "all(#{@types.map(&:inspect).join(', ')})"
426
+ end
427
+ end
428
+
429
+ class Match
430
+ include Type
431
+
432
+ def initialize matcher, &coercion
433
+ @matcher = matcher
434
+ @coercion = coercion
435
+ end
436
+
437
+ def === value
438
+ @matcher === value
439
+ end
440
+
441
+ def inspect
442
+ "match(#{@matcher.inspect})"
443
+ end
444
+ end
400
445
  end
401
446
  end
@@ -1,3 +1,3 @@
1
1
  module Primalize
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.9"
3
3
  end
data/primalize.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.16.a"
23
+ spec.add_development_dependency "bundler"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
26
  spec.add_development_dependency "pry"
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: primalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Gaskins
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-24 00:00:00.000000000 Z
11
+ date: 2021-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.16.a
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.16.a
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description:
69
+ description:
70
70
  email:
71
71
  - jgaskins@gmail.com
72
72
  executables: []
@@ -92,7 +92,7 @@ homepage: https://github.com/jgaskins/primalize
92
92
  licenses:
93
93
  - MIT
94
94
  metadata: {}
95
- post_install_message:
95
+ post_install_message:
96
96
  rdoc_options: []
97
97
  require_paths:
98
98
  - lib
@@ -107,9 +107,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  - !ruby/object:Gem::Version
108
108
  version: '0'
109
109
  requirements: []
110
- rubyforge_project:
111
- rubygems_version: 2.7.3
112
- signing_key:
110
+ rubygems_version: 3.1.4
111
+ signing_key:
113
112
  specification_version: 4
114
113
  summary: Type-checked serializers for your Ruby objects
115
114
  test_files: []