cot 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b47b0825bf582a0dded1ff4736bef69945d6f485
4
- data.tar.gz: f10875feef7630460c617c154dd6564bb1eb210a
3
+ metadata.gz: b54755a09317578c48c93d680043d3e6bcf1caab
4
+ data.tar.gz: a264c498df609509981b8c5afb837a211ad84d36
5
5
  SHA512:
6
- metadata.gz: b1c6d69f790c266a5e73a29d4ed74dbb491d0f2111a3f1224c03abcc87e4520822ebd094e8a6979a77ede993cfb64a5e78187ce1a506468c5c8a62e0ba5f20e2
7
- data.tar.gz: 155a93d43a4b96ed4f71d3e4e08653c251b4235ba4d35f53d61396327136e103b3a8972b66e809bd96a9896764f29bbe24d52e85e2275412a36b5c42fcb696b7
6
+ metadata.gz: 5f71356f473c1de0f94513b553dd8222977744489434808c90de5fa9f130019cb710278b7df826f37b6365d532712886148a28e027cd88c8bda0c5729dfc7cf3
7
+ data.tar.gz: d25fc72929dd6e80b7ece0d5ccbd02854a8bb49df4376a17d188ae11b1ec8209187a2c9d2717f10fcc8602e7fdad1489b443568e86950231d33b2837fbed54de
data/.ackrc ADDED
@@ -0,0 +1,3 @@
1
+ --ignore-dir=coverage
2
+ --ignore-dir=out
3
+ --color
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.0.0-p247
1
+ ruby-2.0.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cot (0.2.0)
4
+ cot (0.3.0)
5
5
  activemodel
6
6
 
7
7
  GEM
@@ -17,6 +17,8 @@ GEM
17
17
  thread_safe (~> 0.1)
18
18
  tzinfo (~> 1.1)
19
19
  ast (2.0.0)
20
+ astrolabe (1.3.0)
21
+ parser (>= 2.2.0.pre.3, < 3.0)
20
22
  builder (3.2.2)
21
23
  diff-lcs (1.2.5)
22
24
  docile (1.1.5)
@@ -24,7 +26,7 @@ GEM
24
26
  json (1.8.1)
25
27
  minitest (5.4.0)
26
28
  multi_json (1.10.1)
27
- parser (2.2.0.pre.3)
29
+ parser (2.2.0.pre.4)
28
30
  ast (>= 1.1, < 3.0)
29
31
  slop (~> 3.4, >= 3.4.5)
30
32
  powerpack (0.0.9)
@@ -44,9 +46,9 @@ GEM
44
46
  rspec-mocks (3.0.2)
45
47
  rspec-support (~> 3.0.0)
46
48
  rspec-support (3.0.2)
47
- rubocop (0.24.1)
48
- json (>= 1.7.7, < 2)
49
- parser (>= 2.2.0.pre.3, < 3.0)
49
+ rubocop (0.26.0)
50
+ astrolabe (~> 1.3)
51
+ parser (>= 2.2.0.pre.4, < 3.0)
50
52
  powerpack (~> 0.0.6)
51
53
  rainbow (>= 1.99.1, < 3.0)
52
54
  ruby-progressbar (~> 1.4)
@@ -62,7 +64,7 @@ GEM
62
64
  multi_json
63
65
  simplecov-html (~> 0.8.0)
64
66
  simplecov-html (0.8.0)
65
- slop (3.5.0)
67
+ slop (3.6.0)
66
68
  thread_safe (0.3.4)
67
69
  tzinfo (1.2.1)
68
70
  thread_safe (~> 0.1)
@@ -75,6 +77,6 @@ DEPENDENCIES
75
77
  cot!
76
78
  rspec
77
79
  rspec-its
78
- rubocop
80
+ rubocop (>= 0.26.0)
79
81
  shoulda
80
82
  simplecov
data/cot.gemspec CHANGED
@@ -22,6 +22,6 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency 'bundler', '>= 1.0.0'
23
23
  s.add_development_dependency 'rspec', '>= 0'
24
24
  s.add_development_dependency 'rspec-its', '>= 0'
25
- s.add_development_dependency 'rubocop', '>= 0'
25
+ s.add_development_dependency 'rubocop', '>= 0.26.0'
26
26
  s.add_development_dependency 'simplecov', '>= 0'
27
27
  end
data/lib/cot.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'active_model'
2
+ require 'active_support/core_ext/hash/indifferent_access'
2
3
 
3
4
  require 'cot/version'
4
5
  require 'cot/frame'
@@ -1,8 +1,10 @@
1
1
  module Cot
2
2
  class Collection < SimpleDelegator
3
- def initialize(klass, objects, sub_key = false)
3
+ def initialize(klass, objects, options = {})
4
+ options = { sub_key: options } unless options.is_a?(Hash)
5
+ @options = options.with_indifferent_access
6
+ @options[:default_attributes] = {} unless @options[:default_attributes].is_a?(Hash)
4
7
  @klass = klass
5
- @sub_key = sub_key
6
8
  @objects = objects
7
9
 
8
10
  # If you pass in different types of things here we can't be friends
@@ -24,7 +26,7 @@ module Cot
24
26
  end
25
27
 
26
28
  def errors
27
- Hash[@objects.reject { |x| x.valid? }.map { |x| [x.id, x.errors] }]
29
+ Hash[@objects.reject(&:valid?).map { |x| [x.id, x.errors] }]
28
30
  end
29
31
 
30
32
  def update_members(payload)
@@ -41,10 +43,10 @@ module Cot
41
43
  def initialize_objects(objects)
42
44
  @objects = []
43
45
  @objects = objects.map do |payload|
44
- if @sub_key
45
- @klass.new payload[@sub_key]
46
+ if @options[:sub_key]
47
+ @klass.new @options[:default_attributes].merge(payload.fetch(@options[:sub_key], {}))
46
48
  else
47
- @klass.new payload
49
+ @klass.new @options[:default_attributes].merge(payload || {})
48
50
  end
49
51
  end
50
52
 
@@ -46,7 +46,7 @@ RSpec::Matchers.define :set_property do |field|
46
46
  end
47
47
 
48
48
  failure_message do
49
- failed = @tests.keys.select { |v| !v }
49
+ failed = @tests.select { |_, v| !v }.keys
50
50
  "Expected the property #{field} to be set, but the following attributes weren't set correctly #{failed}"
51
51
  end
52
52
 
data/lib/cot/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cot
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -81,11 +81,24 @@ describe Cot::Collection do
81
81
  expect(coll.first).to be_kind_of FakeDouble
82
82
  end
83
83
 
84
- it 'takes an optional sub_key to pull the object out of the payload' do
84
+ it 'takes an optional sub_key option to pull the object out of the payload' do
85
+ coll = Cot::Collection.new FakeDouble, [{ inner: { fooy: :bar } }, { inner: { asdf: :fdas } }], sub_key: :inner
86
+ expect(coll.first).to be_kind_of FakeDouble
87
+ expect(coll.first.fooy).to eq :bar
88
+ end
89
+
90
+ it 'takes an optional default_attributes option to add set attributes in every object.' do
91
+ coll = Cot::Collection.new FakeDouble, [{ fooy: :bar }, { asdf: :fdas }], default_attributes: { foo: :baz }
92
+ expect(coll).to all be_kind_of FakeDouble
93
+ expect(coll.map(&:foo).uniq).to eq [:baz]
94
+ end
95
+
96
+ it 'support a legacy optional sub_key parameter to pull the object out of the payload' do
85
97
  coll = Cot::Collection.new FakeDouble, [{ inner: { fooy: :bar } }, { inner: { asdf: :fdas } }], :inner
86
98
  expect(coll.first).to be_kind_of FakeDouble
87
99
  expect(coll.first.fooy).to eq :bar
88
100
  end
101
+
89
102
  end
90
103
 
91
104
  context 'update members' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Henrich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-08 00:00:00.000000000 Z
11
+ date: 2014-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - '>='
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: 0.26.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - '>='
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: 0.26.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: simplecov
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -115,6 +115,7 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
+ - .ackrc
118
119
  - .gitignore
119
120
  - .rubocop.yml
120
121
  - .ruby-gemset
@@ -153,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
154
  version: '0'
154
155
  requirements: []
155
156
  rubyforge_project:
156
- rubygems_version: 2.1.9
157
+ rubygems_version: 2.2.2
157
158
  signing_key:
158
159
  specification_version: 4
159
160
  summary: Simplifies creating models for rest based resources