serialism 0.0.1 → 0.2.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
  SHA1:
3
- metadata.gz: 3a8da01fff2518ac843e20e413b6316fde7ac376
4
- data.tar.gz: 9ce3c2b30174352c5347510ae20bf1a0fe5774bf
3
+ metadata.gz: 811ef6562b6145708c14e43e3a1f0e251a612c62
4
+ data.tar.gz: ec066137b81c602616ff0658607a70845517aa42
5
5
  SHA512:
6
- metadata.gz: bf44d446ca14577c76edcaec7154eaa8dad1cdc32b34a7b409b19fd7ae06c69f6c5ff79a25420c58f55d0fe63d4dcbeb0fedbbb77bd2cce8338fda158e522fb3
7
- data.tar.gz: 8319e027cc4e276bab9e6073819ac17cc42d124ff41d396923bd384ce01327f0c037d7e44ebc58230deec06b70f552c3641a47980c490ad1c07303d9112f83e3
6
+ metadata.gz: 4a53c543ddd5ed7eb6963d7407ec2c3c65f98d248f4f3e27f3a743379d6c4087615d6bc2fb6474b55137a16b20bb2ffa40f6c8739f283b1134a7a391bd1f60c2
7
+ data.tar.gz: eca29236444736a482b97d08f8637c064bd1cc14c4b2e42ef08186c7994538551091f801a4ded5085fc342f26622f90c9a84e0efec74133361c126af20fe7471
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
+ /.rubocop-http*
3
4
  /Gemfile.lock
4
5
  /_yardoc/
5
6
  /coverage/
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ inherit_from:
5
+ - https://raw.githubusercontent.com/tedconf/code-style-guides/master/linters/rubocop/rubocop.yml
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ serialism
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.7
data/Guardfile CHANGED
@@ -24,8 +24,8 @@
24
24
  # * zeus: 'zeus rspec' (requires the server to be started separately)
25
25
  # * 'just' rspec: 'rspec'
26
26
 
27
- guard :rspec, cmd: "bundle exec rspec" do
28
- require "guard/rspec/dsl"
27
+ guard :rspec, cmd: 'bundle exec rspec' do
28
+ require 'guard/rspec/dsl'
29
29
  dsl = Guard::RSpec::Dsl.new(self)
30
30
 
31
31
  # Feel free to open issues for suggestions and improvements
data/Jenkinsfile ADDED
@@ -0,0 +1,3 @@
1
+ rubyBuild {
2
+ job_type = 'gem'
3
+ }
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
 
3
+ require 'rspec/core/rake_task'
4
+ desc 'Run all specs in spec directory (excluding plugin specs)'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ if ENV['COVERAGE']
8
+ require 'ci/reporter/rake/rspec'
9
+ task spec: 'ci:setup:rspec'
10
+ end
data/build/.gitkeep ADDED
File without changes
data/lib/serialism.rb CHANGED
@@ -1,6 +1,7 @@
1
- require "serialism/version"
2
- require "serialism/collection"
3
- require "serialism/serializer"
1
+ require 'serialism/version'
2
+ require 'serialism/collection'
3
+ require 'serialism/serializer'
4
4
 
5
+ # Serialism provides serialization of collections of objects.
5
6
  module Serialism
6
7
  end
@@ -1,5 +1,4 @@
1
1
  module Serialism
2
-
3
2
  # Combines a set of items and a serializer class.
4
3
  #
5
4
  # Example:
@@ -15,22 +14,21 @@ module Serialism
15
14
  # Serialism::Collection.new(a_bunch_of_foo_instances, serializer: FooSerializer).to_csv
16
15
  # #=> returns a CSV string
17
16
  class Collection
18
-
19
17
  attr_reader :items
20
18
 
21
19
  # create a new collection
22
20
  #
23
- # @param[Enumerable] items
21
+ # @param [Enumerable] items
24
22
  # A collection of items.
25
23
  # All member items should be encodable by `serializer`.
26
- # @param[Serialism::Serializer] serializer
24
+ # @param [Serialism::Serializer] serializer
27
25
  # The serializer class used to encode members of `items`.
28
- def initialize(items=[], serializer:)
29
- if ! serializer.respond_to?(:attributes)
30
- raise ArgumentError, "serializer must implement a class-level :attributes method"
26
+ def initialize(items = [], serializer:)
27
+ if !serializer.respond_to?(:attributes)
28
+ raise ArgumentError, 'serializer must implement a class-level :attributes method'
31
29
  end
32
- if ! serializer.instance_methods.include?(:render)
33
- raise ArgumentError, "serializer must implement an instance-level :render method"
30
+ if !serializer.instance_methods.include?(:render)
31
+ raise ArgumentError, 'serializer must implement an instance-level :render method'
34
32
  end
35
33
  @serializer = serializer
36
34
 
@@ -42,10 +40,10 @@ module Serialism
42
40
  # Replaces any previous items already in the collection.
43
41
  #
44
42
  # @param [#each] items an enumerable collection of items
45
- # @return [CsvCollection]
43
+ # @return [Serialism::Collection]
46
44
  def items=(items)
47
- raise ArgumentError, "argument must respond_to :each" if ! items.respond_to?(:each)
48
- raise ArgumentError, "argument must respond_to :map" if ! items.respond_to?(:map)
45
+ raise ArgumentError, 'argument must respond_to :each' if !items.respond_to?(:each)
46
+ raise ArgumentError, 'argument must respond_to :map' if !items.respond_to?(:map)
49
47
 
50
48
  @items = items
51
49
  self
@@ -55,7 +53,7 @@ module Serialism
55
53
  #
56
54
  # @return [Array]
57
55
  def attributes
58
- return [] if items.size == 0
56
+ return [] if items.empty?
59
57
 
60
58
  @serializer.attributes
61
59
  end
@@ -72,14 +70,8 @@ module Serialism
72
70
 
73
71
  CSV.generate do |csv|
74
72
  csv << attributes
75
- items.each do |t|
76
-
77
- row = @serializer.new(t).render.values.map do |cell|
78
- # convert complex cells to comma-separated strings
79
- cell.is_a?(Array) ? cell.join(',') : cell
80
- end
81
-
82
- csv << row
73
+ items.map do |i|
74
+ csv << render_row(i)
83
75
  end
84
76
  end
85
77
  end
@@ -87,8 +79,21 @@ module Serialism
87
79
  def to_json
88
80
  require 'json'
89
81
 
90
- JSON.dump(items.map {|t| @serializer.new(t).render })
82
+ JSON.dump(items.map { |t| @serializer.new(t).render })
91
83
  end
92
84
 
85
+ # this generates an array of arrays
86
+ # headers are [0] data starts at [1]
87
+ def to_a
88
+ [attributes] + items.map { |i| render_row(i) }
89
+ end
90
+
91
+ private
92
+
93
+ def render_row(row)
94
+ @serializer.new(row).render.values.map do |cell|
95
+ cell.is_a?(Array) ? cell.join(',') : cell
96
+ end
97
+ end
93
98
  end
94
99
  end
@@ -1,5 +1,4 @@
1
1
  module Serialism
2
-
3
2
  # Base class for concrete serializers to inherit from.
4
3
  #
5
4
  # Example:
@@ -23,14 +22,11 @@ module Serialism
23
22
  # serializer.render
24
23
  # # => {id: 12, computed: "computed - 12"}
25
24
  class Serializer
26
-
27
25
  attr_reader :object
28
26
 
29
27
  @attributes = []
30
28
  def self.attributes(*attrs)
31
- if attrs.size > 0
32
- @attributes = attrs
33
- end
29
+ @attributes = attrs if !attrs.empty?
34
30
  @attributes
35
31
  end
36
32
 
@@ -42,9 +38,9 @@ module Serialism
42
38
  #
43
39
  # @return [Hash] Keys are defined by the classes `attributes`.
44
40
  def render
45
- self.class.attributes.inject({}) do |memo,attr|
41
+ self.class.attributes.inject({}) do |memo, attr|
46
42
  if respond_to?(attr)
47
- memo[attr] = self.send(attr)
43
+ memo[attr] = send(attr)
48
44
  elsif object.respond_to?(attr)
49
45
  memo[attr] = object.send(attr)
50
46
  else
@@ -53,7 +49,5 @@ module Serialism
53
49
  memo
54
50
  end
55
51
  end
56
-
57
52
  end
58
-
59
53
  end
@@ -1,3 +1,3 @@
1
1
  module Serialism
2
- VERSION = "0.0.1"
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/script/jenkins.sh ADDED
@@ -0,0 +1,66 @@
1
+ #!/bin/bash
2
+
3
+ # Template Jenkins Wrapper for Language: 'Ruby'
4
+
5
+ # Setup our ruby and gemset
6
+ source /usr/local/rvm/scripts/rvm
7
+ type rvm | head -1
8
+
9
+ # Use our gemset (create it if it doesn't already exist)
10
+ ruby_version=`cat .ruby-version | tr -d '\n'`
11
+ ruby_gemset=`cat .ruby-gemset | tr -d '\n'`
12
+
13
+ rvm use "${ruby_version}@${ruby_gemset}" --create
14
+
15
+ # Make sure bundler is installed for this ruby version/gemset
16
+ gem list bundler -i >/dev/null 2>&1
17
+ if [ $? -eq 1 ]; then
18
+ echo "Installing bundler"
19
+ gem install bundler
20
+ fi
21
+
22
+ # Ensure we have ruby
23
+ ruby --version
24
+ echo "Ruby Gemset:"
25
+ rvm current
26
+ echo ""
27
+
28
+ # Print all commands after expansion. Note that you can put this earlier
29
+ # in the script, but rvm prints out a wall-o-text.
30
+ set -x
31
+
32
+ # Good to know the path
33
+ echo "PATH is ${PATH}"
34
+
35
+ # any non-normal exit status from now on should fail the build
36
+ set -e
37
+
38
+ # Update all our gems
39
+ bundle install
40
+
41
+ export COVERAGE=on
42
+ bundle exec rake spec
43
+
44
+ # check for style violations
45
+ # never fail the build at this step. let a jenkins post-build action do this
46
+ # if necessary
47
+ #
48
+ # output:
49
+ # print clang format to stdout, which ends up in jenkins console
50
+ # create html version in build/ for viewing from jenkins web UI.
51
+ # print checkstyle.xml for consumption by jenkins checkstyle plugin
52
+ bundle exec rubocop \
53
+ --require rubocop/formatter/checkstyle_formatter \
54
+ --display-cop-names \
55
+ --format clang \
56
+ --format html \
57
+ --out build/rubocop.html \
58
+ --format RuboCop::Formatter::CheckstyleFormatter \
59
+ --out tmp/checkstyle.xml || true
60
+
61
+ # Security check
62
+ ignores=""
63
+ bundle exec bundle-audit update
64
+ bundle exec bundle-audit check --ignore=${ignores}
65
+
66
+ exit 0
data/serialism.gemspec CHANGED
@@ -1,27 +1,30 @@
1
1
  # coding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'serialism/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "serialism"
8
+ spec.name = 'serialism'
8
9
  spec.version = Serialism::VERSION
9
- spec.authors = ["Alex Dean"]
10
- spec.email = ["alex@crackpot.org"]
11
- spec.summary = %q{Like ActiveModel::Serializer but smaller and not JSON-centric.}
12
- spec.homepage = ""
13
- spec.license = "MIT"
10
+ spec.authors = ['Alex Dean']
11
+ spec.email = ['github@mostlyalex.com']
12
+ spec.summary = 'Like ActiveModel::Serializer but smaller and not JSON-centric.'
13
+ spec.homepage = 'https://github.com/tedconf/serialism'
14
+ spec.license = 'MIT'
14
15
 
15
16
  spec.files = `git ls-files -z`.split("\x0")
16
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
- spec.require_paths = ["lib"]
19
-
20
- spec.add_development_dependency "bundler", "~> 1.7"
21
- spec.add_development_dependency "rake", "~> 10.0"
22
- spec.add_development_dependency "rspec", "~> 3.0.0"
23
- spec.add_development_dependency "guard-rspec"
24
- spec.add_development_dependency "ruby_gntp"
25
- spec.add_development_dependency "ci_reporter_rspec"
19
+ spec.require_paths = ['lib']
26
20
 
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ spec.add_development_dependency 'guard-rspec'
25
+ spec.add_development_dependency 'ruby_gntp'
26
+ spec.add_development_dependency 'ci_reporter_rspec'
27
+ spec.add_development_dependency 'bundler-audit'
28
+ spec.add_development_dependency 'rubocop'
29
+ spec.add_development_dependency 'rubocop-checkstyle_formatter'
27
30
  end
@@ -34,7 +34,12 @@ RSpec.describe Serialism::Collection, type: :model do
34
34
 
35
35
  expect {
36
36
  Serialism::Collection.new([], serializer: invalid_serializer)
37
- }.to raise_error(ArgumentError, 'serializer must implement a class-level :attributes method')
37
+ }.to(
38
+ raise_error(
39
+ ArgumentError,
40
+ 'serializer must implement a class-level :attributes method'
41
+ )
42
+ )
38
43
  end
39
44
 
40
45
  it 'should require serializer to implement instance-level render' do
@@ -75,6 +80,12 @@ RSpec.describe Serialism::Collection, type: :model do
75
80
  it 'should return array of attributes' do
76
81
  expect(collection.attributes).to eq serializer.attributes
77
82
  end
83
+
84
+ it 'should return empty array if no items exist' do
85
+ expect(
86
+ Serialism::Collection.new([], serializer: serializer).attributes
87
+ ).to eq []
88
+ end
78
89
  end
79
90
 
80
91
  describe 'to_csv' do
@@ -91,8 +102,8 @@ EOF
91
102
 
92
103
  it 'should encode complex cells as csv strings' do
93
104
  collection.items = [
94
- serialized.new([1,2,3]),
95
- serialized.new([4,5,6])
105
+ serialized.new([1, 2, 3]),
106
+ serialized.new([4, 5, 6])
96
107
  ]
97
108
 
98
109
  expected = <<-EOF
@@ -106,7 +117,37 @@ EOF
106
117
 
107
118
  describe 'to_json' do
108
119
  it 'should generate json' do
109
- expect(collection.to_json).to eq '[{"id":0,"computed":"computed - 0"},{"id":1,"computed":"computed - 1"},{"id":2,"computed":"computed - 2"}]'
120
+ expect(collection.to_json).to eq(
121
+ '[' \
122
+ '{"id":0,"computed":"computed - 0"},' \
123
+ '{"id":1,"computed":"computed - 1"},' \
124
+ '{"id":2,"computed":"computed - 2"}' \
125
+ ']'
126
+ )
127
+ end
128
+ end
129
+
130
+ describe 'to_a' do
131
+ it 'should generate an array' do
132
+ expected = [[:id, :computed],
133
+ [0, 'computed - 0'],
134
+ [1, 'computed - 1'],
135
+ [2, 'computed - 2']
136
+ ]
137
+ expect(collection.to_a).to eq expected
138
+ end
139
+
140
+ it 'should encode complex cells as csv strings' do
141
+ collection.items = [
142
+ serialized.new([1, 2, 3]),
143
+ serialized.new([4, 5, 6])
144
+ ]
145
+
146
+ expected = [[:id, :computed],
147
+ ["1,2,3", "computed - [1, 2, 3]"],
148
+ ["4,5,6", "computed - [4, 5, 6]"]
149
+ ]
150
+ expect(collection.to_a).to eq expected
110
151
  end
111
152
  end
112
153
 
@@ -28,10 +28,10 @@ RSpec.describe Serialism::Serializer, type: :model do
28
28
 
29
29
  it 'should prefer attribute implementations in the serializer' do
30
30
  values = subject.new(item).render
31
- expect(values[:computed]).to eq "computed by serializer - 1"
31
+ expect(values[:computed]).to eq 'computed by serializer - 1'
32
32
  end
33
33
 
34
- it 'should use attribute implementation in the object if not defined explicitly in serializer' do
34
+ it 'should use attribute implementation in object if not defined explicitly in serializer' do
35
35
  values = subject.new(item).render
36
36
  expect(values[:id]).to eq 1
37
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serialism
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Dean
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-25 00:00:00.000000000 Z
11
+ date: 2018-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 3.0.0
47
+ version: '3.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 3.0.0
54
+ version: '3.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: guard-rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -94,29 +94,77 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler-audit
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-checkstyle_formatter
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
97
139
  description:
98
140
  email:
99
- - alex@crackpot.org
141
+ - github@mostlyalex.com
100
142
  executables: []
101
143
  extensions: []
102
144
  extra_rdoc_files: []
103
145
  files:
104
146
  - ".gitignore"
105
147
  - ".rspec"
148
+ - ".rubocop.yml"
149
+ - ".ruby-gemset"
150
+ - ".ruby-version"
106
151
  - Gemfile
107
152
  - Guardfile
153
+ - Jenkinsfile
108
154
  - LICENSE.txt
109
155
  - README.md
110
156
  - Rakefile
157
+ - build/.gitkeep
111
158
  - lib/serialism.rb
112
159
  - lib/serialism/collection.rb
113
160
  - lib/serialism/serializer.rb
114
161
  - lib/serialism/version.rb
162
+ - script/jenkins.sh
115
163
  - serialism.gemspec
116
164
  - spec/lib/serialism/collection_spec.rb
117
165
  - spec/lib/serialism/serializer_spec.rb
118
166
  - spec/spec_helper.rb
119
- homepage: ''
167
+ homepage: https://github.com/tedconf/serialism
120
168
  licenses:
121
169
  - MIT
122
170
  metadata: {}
@@ -136,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
184
  version: '0'
137
185
  requirements: []
138
186
  rubyforge_project:
139
- rubygems_version: 2.4.3
187
+ rubygems_version: 2.5.2.3
140
188
  signing_key:
141
189
  specification_version: 4
142
190
  summary: Like ActiveModel::Serializer but smaller and not JSON-centric.