seri 0.1.5 → 1.0.0

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: 3a080e5eb873ba467789a339f0ceefa26e8f86b9c88f92d11f55b97a5bba86d0
4
- data.tar.gz: 30b4ef48887b9a4083c64623f46294079078add3176ef45ee92a9dfbe98e4f66
3
+ metadata.gz: 6cfa2981681193432bc52036121c8481f8558594fb4162e5ca7938e911ade897
4
+ data.tar.gz: 005d6c9f9fa17d6aac6d1ec719cb4acd87e713782f46621899ff53c199ff9702
5
5
  SHA512:
6
- metadata.gz: 5e8fe5badf7bfc337d91e02540a1df44f66de49786e9b8c1da6cfd59b631f613bdf9e3aba61ad972d366716dfe32c21b527bc4b67373d3cd59a61e672952e226
7
- data.tar.gz: 65d6c70dfea3489728816beffd1c4febbabf2076607f06daa55991a4f1ac9cfbd5526ac6fab32bd2b78d5a89f534b9e844f8e3a53f2f69a6692fa3ebf95b106e
6
+ metadata.gz: b004ae3a8febfd4ba2869390f7ba91a982835f44c83813fc8423b19ff214f28804feeb960659ca9e09c7a0fb25c92eb3d9d16cff7541acc6a584eb71d4f954cf
7
+ data.tar.gz: 13dc8907b0083bbd8e251b10b23f58781d561e8f26deaefe013f56a825072bc640f08a58c3abddb9594a9074e61482a1fc324b7bb67c7c9a6e68d3f18c908edc
data/.circleci/config.yml CHANGED
@@ -40,6 +40,12 @@ jobs:
40
40
  --format progress \
41
41
  $TEST_FILES
42
42
 
43
+ # run rubocop
44
+ - run:
45
+ name: run rubocop
46
+ command: |
47
+ bundle exec rubocop .
48
+
43
49
  # collect reports
44
50
  - store_test_results:
45
51
  path: /tmp/test-results
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ Metrics/BlockLength:
2
+ Exclude:
3
+ - 'spec/**/*.rb'
4
+ Style/Documentation:
5
+ Enabled: false
6
+ # This is causing breaking changes:
7
+ Style/ExpandPathArguments:
8
+ Enabled: false
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in seri.gemspec
6
6
  gemspec
data/Gemfile.lock CHANGED
@@ -1,18 +1,25 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- seri (0.1.2)
4
+ seri (0.1.5)
5
5
  appsignal (~> 2.7)
6
6
  oj (~> 3.7)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- appsignal (2.7.2)
11
+ appsignal (2.9.5)
12
12
  rack
13
+ ast (2.4.0)
13
14
  diff-lcs (1.3)
14
- oj (3.7.4)
15
- rack (2.0.6)
15
+ jaro_winkler (1.5.2)
16
+ oj (3.7.11)
17
+ parallel (1.17.0)
18
+ parser (2.6.2.1)
19
+ ast (~> 2.4.0)
20
+ psych (3.1.0)
21
+ rack (2.0.7)
22
+ rainbow (3.0.0)
16
23
  rake (10.5.0)
17
24
  rspec (3.8.0)
18
25
  rspec-core (~> 3.8.0)
@@ -27,6 +34,16 @@ GEM
27
34
  diff-lcs (>= 1.2.0, < 2.0)
28
35
  rspec-support (~> 3.8.0)
29
36
  rspec-support (3.8.0)
37
+ rubocop (0.67.2)
38
+ jaro_winkler (~> 1.5.1)
39
+ parallel (~> 1.10)
40
+ parser (>= 2.5, != 2.5.1.1)
41
+ psych (>= 3.1.0)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ ruby-progressbar (~> 1.7)
44
+ unicode-display_width (>= 1.4.0, < 1.6)
45
+ ruby-progressbar (1.10.0)
46
+ unicode-display_width (1.5.0)
30
47
 
31
48
  PLATFORMS
32
49
  ruby
@@ -35,6 +52,7 @@ DEPENDENCIES
35
52
  bundler (~> 1.16)
36
53
  rake (~> 10.0)
37
54
  rspec (~> 3.0)
55
+ rubocop (~> 0.67.2)
38
56
  seri!
39
57
 
40
58
  BUNDLED WITH
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "serializer"
3
+ require 'bundler/setup'
4
+ require 'serializer'
5
5
 
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
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
10
+ # require 'pry'
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start(__FILE__)
@@ -1,9 +1,10 @@
1
1
  class GroupSerializer
2
- def initialize(objects, serializer: nil)
2
+ def initialize(objects, serializer: nil, scope: {})
3
3
  raise ArgumentError, 'serializer needs to be specified' if serializer.nil?
4
4
 
5
5
  @objects = objects
6
6
  @serializer = serializer
7
+ @scope = scope
7
8
  end
8
9
 
9
10
  def to_json(*)
@@ -12,9 +13,13 @@ class GroupSerializer
12
13
  'Group serializer',
13
14
  @serializer.to_s
14
15
  ) do
15
- result = @objects.map { |object| @serializer.new(object).to_h }
16
-
17
- Oj.dump(result, mode: :json)
16
+ Oj.dump(to_h, mode: :json)
18
17
  end
19
18
  end
19
+
20
+ def to_h
21
+ @objects
22
+ .map { |object| @serializer.new(object, scope: @scope) }
23
+ .map(&:to_h)
24
+ end
20
25
  end
@@ -1,3 +1,3 @@
1
1
  class Serializer
2
- VERSION = "0.1.5"
2
+ VERSION = '1.0.0'.freeze
3
3
  end
data/lib/serializer.rb CHANGED
@@ -4,11 +4,11 @@ require 'serializer/group_serializer'
4
4
  class Serializer
5
5
  class SerializerError < StandardError; end
6
6
 
7
- ARRAYS = %w(
7
+ ARRAYS = %w[
8
8
  Array
9
9
  ActiveRecord_AssociationRelation
10
10
  ActiveRecord_Associations_CollectionProxy
11
- )
11
+ ].freeze
12
12
 
13
13
  Attribute = Struct.new(:key, :condition, :from, :serializer, :options)
14
14
 
@@ -32,24 +32,7 @@ class Serializer
32
32
  self.class.attributes.each_with_object({}) do |attribute, obj|
33
33
  next if attribute.condition && !public_send(attribute.condition)
34
34
 
35
- extraction_key = attribute.from || attribute.key
36
-
37
- # Fetches a value from an attribute by checking if there's a ..
38
- # .. static value set, or a ..
39
- # .. method defined in the serializer, or a ..
40
- # .. method/attribute defined in the object or ..
41
- # .. it raises an error
42
- value = if attribute.options.has_key?(:static_value)
43
- attribute.options.fetch(:static_value)
44
- elsif respond_to?(extraction_key)
45
- public_send(extraction_key)
46
- elsif object.respond_to?(extraction_key)
47
- object.public_send(extraction_key)
48
- else
49
- raise SerializerError, "unknown attribute '#{extraction_key}'"
50
- end
51
-
52
- obj[attribute.key] = serialize_value(value, attribute.serializer)
35
+ obj[attribute.key] = serialize_value(attribute)
53
36
  end
54
37
  end
55
38
 
@@ -61,13 +44,35 @@ class Serializer
61
44
 
62
45
  private
63
46
 
64
- def serialize_value(value, serializer)
65
- return value unless serializer
47
+ def serialize_value(attribute)
48
+ value = extract_value(attribute)
49
+ serializer = attribute.serializer
66
50
 
67
51
  if ARRAYS.any? { |match| value.class.to_s.end_with?(match) }
68
52
  value.map { |v| serializer.new(v).to_h }
69
- else
53
+ elsif serializer
70
54
  serializer.new(value).to_h
55
+ else
56
+ value
57
+ end
58
+ end
59
+
60
+ # Fetches a value from an attribute by checking if there's a ..
61
+ # .. static value set, or a ..
62
+ # .. method defined in the serializer, or a ..
63
+ # .. method/attribute defined in the object or ..
64
+ # .. it raises an error
65
+ def extract_value(attribute)
66
+ extraction_key = attribute.from || attribute.key
67
+
68
+ if attribute.options.key?(:static_value)
69
+ attribute.options.fetch(:static_value)
70
+ elsif respond_to?(extraction_key)
71
+ public_send(extraction_key)
72
+ elsif object.respond_to?(extraction_key)
73
+ object.public_send(extraction_key)
74
+ else
75
+ raise SerializerError, "unknown attribute '#{extraction_key}'"
71
76
  end
72
77
  end
73
78
  end
data/seri.gemspec CHANGED
@@ -1,32 +1,35 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
1
+ lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "serializer/version"
3
+ require 'serializer/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = "seri"
6
+ spec.name = 'seri'
8
7
  spec.version = Serializer::VERSION
9
- spec.authors = ["grdw"]
10
- spec.email = ["gerard@wetransfer.com"]
8
+ spec.authors = ['grdw']
9
+ spec.email = ['gerard@wetransfer.com']
11
10
 
12
- spec.summary = %q{A basic serializer}
13
- spec.description = %q{A basic serializer which can suite as a replacement to other gems}
14
- spec.homepage = "https://github.com/grdw/seri"
15
- spec.license = "MIT"
11
+ spec.summary = 'A basic serializer'
12
+ spec.description = 'A basic serializer'
13
+ spec.homepage = 'https://github.com/grdw/seri'
14
+ spec.license = 'MIT'
16
15
 
17
16
  # Specify which files should be added to the gem when it is released.
18
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
18
+ # into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
22
+ end
21
23
  end
22
- spec.bindir = "exe"
24
+ spec.bindir = 'exe'
23
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
- spec.require_paths = ["lib"]
26
+ spec.require_paths = ['lib']
25
27
 
26
- spec.add_dependency "oj", "~> 3.7"
27
- spec.add_dependency "appsignal", "~> 2.7"
28
+ spec.add_dependency 'appsignal', '~> 2.7'
29
+ spec.add_dependency 'oj', '~> 3.7'
28
30
 
29
- spec.add_development_dependency "bundler", "~> 1.16"
30
- spec.add_development_dependency "rake", "~> 10.0"
31
- spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency 'bundler', '~> 1.16'
32
+ spec.add_development_dependency 'rake', '~> 10.0'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+ spec.add_development_dependency 'rubocop', '~> 0.67.2'
32
35
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seri
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - grdw
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-03 00:00:00.000000000 Z
11
+ date: 2019-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: oj
14
+ name: appsignal
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.7'
19
+ version: '2.7'
20
20
  type: :runtime
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: '3.7'
26
+ version: '2.7'
27
27
  - !ruby/object:Gem::Dependency
28
- name: appsignal
28
+ name: oj
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.7'
33
+ version: '3.7'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.7'
40
+ version: '3.7'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -80,7 +80,21 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
- description: A basic serializer which can suite as a replacement to other gems
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.67.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.67.2
97
+ description: A basic serializer
84
98
  email:
85
99
  - gerard@wetransfer.com
86
100
  executables: []
@@ -90,6 +104,7 @@ files:
90
104
  - ".circleci/config.yml"
91
105
  - ".gitignore"
92
106
  - ".rspec"
107
+ - ".rubocop.yml"
93
108
  - ".travis.yml"
94
109
  - CODE_OF_CONDUCT.md
95
110
  - Gemfile