cot 0.5.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c694dae2bac88f952a49ce4b4c7b5663edbd0fb7
4
- data.tar.gz: 4001ebb79a3a8da5bd4b6828a814c9f7d408d728
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzRhMGE4M2I0NmZlYjUyMTRkMjdlMzQ2MzNkNjUzNGM0Y2M5MjI1MA==
5
+ data.tar.gz: !binary |-
6
+ OWFhMTM2MmI3NjQ1ZTE3OTFhMDMwZTA2YzY3NDVhNDJkN2QwOWYyZg==
5
7
  SHA512:
6
- metadata.gz: c5eba0c2e611e055dd3129491d25144f303fa5f740a790afdffd7b7ae8181000c9668c71ea915005a91ce93c6f52e491cbaaa8432609b8f7a5212a32e7552439
7
- data.tar.gz: 1d42ba345c4223f9dab0ae101f73e78a0829f6fda56ba4b8109bd9f8eebfe5dd498c70456c3706151b68652dea024b396a870700611ce72be0dfaa062dc662bc
8
+ metadata.gz: !binary |-
9
+ MWM5ZTA2OTJjMWFmY2IxOTA5MTE1NmNlNGY4OTdmM2U5Y2Q0NmJhYzdiNmEw
10
+ OWFjNDAwNGZjZDViMDNhZTk0YjY5N2IxMjVkMThlMGEwYmNmMzRlYTU1MzUz
11
+ YTI2MTQ0ODNkMjA4MzY2OGZmMGYxZGQ2ZDIxYzVmYjQxOWVmMmU=
12
+ data.tar.gz: !binary |-
13
+ ZWQ2MTJlNzFiMzNmNTkyMmI3NDRjZDg2NmEzMTY0M2RlYWZiMGYwZjBkYmE5
14
+ NDgwMTYzMDI0NTBmYmY3OGUwMzE5Nzc2Y2Q4OTc1NGIxM2Q3ODZkNDg2YmRl
15
+ ODE0ZTBlNjk3NGVhOTQ4ZWZjMzY2ZGZlZGQzNDNhM2NmOGU0ZmE=
@@ -1 +1 @@
1
- ruby-2.0.0
1
+ ruby-2.1.2
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ #- 2.0.0
5
+ - 2.2.0
6
+ script:
7
+ - bundle exec rake
8
+ - bundle exec rake mutant
9
+ deploy:
10
+ provider: rubygems
11
+ api_key:
12
+ secure: LWCk/pKVdaX6qUWGsg+QxImR1nbB0nYzUd7LBSxbY96YXdjjED1pFdwjf2tY184hUBQF+LupWwVjEfPOkRBmGH290qjF4/Bz5z4vs1ZS+k77owMR4aeiVptS05BOUOVLq2CcAuNiVVB3hCRcXAKdd0wFFco4pXqLUAj3GlOjhl4=
13
+ gem: cot
14
+ on:
15
+ tags: true
16
+ repo: crimsonknave/cot
@@ -1,73 +1,125 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cot (0.4.3)
4
+ cot (0.5.2)
5
5
  activemodel
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activemodel (4.1.4)
11
- activesupport (= 4.1.4)
10
+ abstract_type (0.0.7)
11
+ activemodel (4.2.1)
12
+ activesupport (= 4.2.1)
12
13
  builder (~> 3.1)
13
- activesupport (4.1.4)
14
- i18n (~> 0.6, >= 0.6.9)
14
+ activesupport (4.2.1)
15
+ i18n (~> 0.7)
15
16
  json (~> 1.7, >= 1.7.7)
16
17
  minitest (~> 5.1)
17
- thread_safe (~> 0.1)
18
+ thread_safe (~> 0.3, >= 0.3.4)
18
19
  tzinfo (~> 1.1)
20
+ adamantium (0.2.0)
21
+ ice_nine (~> 0.11.0)
22
+ memoizable (~> 0.4.0)
23
+ anima (0.2.0)
24
+ abstract_type (~> 0.0.7)
25
+ adamantium (~> 0.1)
26
+ equalizer (~> 0.0.8)
19
27
  ast (2.0.0)
20
28
  astrolabe (1.3.0)
21
29
  parser (>= 2.2.0.pre.3, < 3.0)
22
30
  builder (3.2.2)
31
+ concord (0.1.5)
32
+ adamantium (~> 0.2.0)
33
+ equalizer (~> 0.0.9)
23
34
  diff-lcs (1.2.5)
24
35
  docile (1.1.5)
25
- i18n (0.6.11)
26
- json (1.8.1)
27
- minitest (5.4.0)
28
- multi_json (1.10.1)
29
- parser (2.2.0.pre.4)
36
+ equalizer (0.0.11)
37
+ i18n (0.7.0)
38
+ ice_nine (0.11.1)
39
+ json (1.8.2)
40
+ memoizable (0.4.2)
41
+ thread_safe (~> 0.3, >= 0.3.1)
42
+ minitest (5.5.1)
43
+ morpher (0.2.3)
44
+ abstract_type (~> 0.0.7)
45
+ adamantium (~> 0.2.0)
46
+ anima (~> 0.2.0)
47
+ ast (~> 2.0.0)
48
+ concord (~> 0.1.4)
49
+ equalizer (~> 0.0.9)
50
+ ice_nine (~> 0.11.0)
51
+ procto (~> 0.0.2)
52
+ multi_json (1.11.0)
53
+ mutant (0.7.8)
54
+ abstract_type (~> 0.0.7)
55
+ adamantium (~> 0.2.0)
56
+ anima (~> 0.2.0)
57
+ ast (~> 2.0)
58
+ concord (~> 0.1.5)
59
+ diff-lcs (~> 1.2)
60
+ equalizer (~> 0.0.9)
61
+ ice_nine (~> 0.11.1)
62
+ memoizable (~> 0.4.2)
63
+ morpher (~> 0.2.3)
64
+ parallel (~> 1.3)
65
+ parser (~> 2.2.0.2)
66
+ procto (~> 0.0.2)
67
+ unparser (~> 0.2.2)
68
+ mutant-rspec (0.7.8)
69
+ mutant (~> 0.7.8)
70
+ rspec-core (>= 3.0.0, < 3.3.0)
71
+ parallel (1.4.1)
72
+ parser (2.2.0.3)
30
73
  ast (>= 1.1, < 3.0)
31
- slop (~> 3.4, >= 3.4.5)
32
- powerpack (0.0.9)
74
+ powerpack (0.1.0)
75
+ procto (0.0.2)
33
76
  rainbow (2.0.0)
34
- rspec (3.0.0)
35
- rspec-core (~> 3.0.0)
36
- rspec-expectations (~> 3.0.0)
37
- rspec-mocks (~> 3.0.0)
38
- rspec-core (3.0.2)
39
- rspec-support (~> 3.0.0)
40
- rspec-expectations (3.0.2)
77
+ rake (10.4.2)
78
+ rspec (3.2.0)
79
+ rspec-core (~> 3.2.0)
80
+ rspec-expectations (~> 3.2.0)
81
+ rspec-mocks (~> 3.2.0)
82
+ rspec-core (3.2.2)
83
+ rspec-support (~> 3.2.0)
84
+ rspec-expectations (3.2.0)
85
+ diff-lcs (>= 1.2.0, < 2.0)
86
+ rspec-support (~> 3.2.0)
87
+ rspec-its (1.2.0)
88
+ rspec-core (>= 3.0.0)
89
+ rspec-expectations (>= 3.0.0)
90
+ rspec-mocks (3.2.1)
41
91
  diff-lcs (>= 1.2.0, < 2.0)
42
- rspec-support (~> 3.0.0)
43
- rspec-its (1.0.1)
44
- rspec-core (>= 2.99.0.beta1)
45
- rspec-expectations (>= 2.99.0.beta1)
46
- rspec-mocks (3.0.2)
47
- rspec-support (~> 3.0.0)
48
- rspec-support (3.0.2)
49
- rubocop (0.26.0)
92
+ rspec-support (~> 3.2.0)
93
+ rspec-support (3.2.2)
94
+ rubocop (0.29.1)
50
95
  astrolabe (~> 1.3)
51
- parser (>= 2.2.0.pre.4, < 3.0)
52
- powerpack (~> 0.0.6)
96
+ parser (>= 2.2.0.1, < 3.0)
97
+ powerpack (~> 0.1)
53
98
  rainbow (>= 1.99.1, < 3.0)
54
99
  ruby-progressbar (~> 1.4)
55
- ruby-progressbar (1.5.1)
100
+ ruby-progressbar (1.7.5)
56
101
  shoulda (3.5.0)
57
102
  shoulda-context (~> 1.0, >= 1.0.1)
58
103
  shoulda-matchers (>= 1.4.1, < 3.0)
59
104
  shoulda-context (1.2.1)
60
- shoulda-matchers (2.6.1)
105
+ shoulda-matchers (2.8.0)
61
106
  activesupport (>= 3.0.0)
62
- simplecov (0.9.0)
107
+ simplecov (0.9.2)
63
108
  docile (~> 1.1.0)
64
- multi_json
65
- simplecov-html (~> 0.8.0)
66
- simplecov-html (0.8.0)
67
- slop (3.6.0)
68
- thread_safe (0.3.4)
69
- tzinfo (1.2.1)
109
+ multi_json (~> 1.0)
110
+ simplecov-html (~> 0.9.0)
111
+ simplecov-html (0.9.0)
112
+ thread_safe (0.3.5)
113
+ tzinfo (1.2.2)
70
114
  thread_safe (~> 0.1)
115
+ unparser (0.2.2)
116
+ abstract_type (~> 0.0.7)
117
+ adamantium (~> 0.2.0)
118
+ concord (~> 0.1.5)
119
+ diff-lcs (~> 1.2.5)
120
+ equalizer (~> 0.0.9)
121
+ parser (~> 2.2.0.2)
122
+ procto (~> 0.0.2)
71
123
 
72
124
  PLATFORMS
73
125
  ruby
@@ -75,8 +127,11 @@ PLATFORMS
75
127
  DEPENDENCIES
76
128
  bundler (>= 1.0.0)
77
129
  cot!
130
+ mutant
131
+ mutant-rspec
132
+ rake
78
133
  rspec
79
- rspec-its
80
- rubocop (>= 0.26.0)
134
+ rspec-its (>= 1)
135
+ rubocop
81
136
  shoulda
82
137
  simplecov
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- cot
1
+ cot [![Build Status](https://travis-ci.org/crimsonknave/cot.svg?branch=master)](https://travis-ci.org/crimsonknave/cot) [![Gem Version](https://badge.fury.io/rb/cot.svg)](http://badge.fury.io/rb/cot)
2
2
  ===
3
3
 
4
4
  Cot is a gem designed to help convert rest based resources into ruby objects. Currently it only handles converting the responses into objects and doesn't deal with the requests themselves, there are plenty of gems for that out there.
data/Rakefile CHANGED
@@ -2,19 +2,21 @@
2
2
 
3
3
  require 'bundler/gem_tasks'
4
4
  require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RuboCop::RakeTask.new
5
8
 
6
9
  RSpec::Core::RakeTask.new('spec') do |spec|
7
10
  spec.pattern = 'spec/**/*_spec.rb'
8
11
  spec.rspec_opts = '--tag ~broken'
9
12
  end
10
13
 
11
- require 'rubocop'
12
- if defined? RuboCop
13
- desc 'run rubocop'
14
- task :rubocop do
15
- puts `rubocop`
16
- end
17
- end
18
-
19
14
  task default: [:spec, :rubocop]
20
15
  task test: :spec
16
+
17
+ require 'mutant'
18
+ desc 'Run mutation tests using mutant'
19
+ task :mutant do
20
+ result = Mutant::CLI.run(%w( -Ilib -rcot --use rspec Cot* ))
21
+ fail unless result == Mutant::CLI::EXIT_SUCCESS
22
+ end
@@ -19,10 +19,14 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ['lib']
20
20
 
21
21
  s.add_dependency 'activemodel'
22
+
23
+ s.add_development_dependency 'rake'
22
24
  s.add_development_dependency 'shoulda', '>= 0'
23
25
  s.add_development_dependency 'bundler', '>= 1.0.0'
24
26
  s.add_development_dependency 'rspec', '>= 0'
25
- s.add_development_dependency 'rspec-its', '>= 0'
26
- s.add_development_dependency 'rubocop', '>= 0.26.0'
27
+ s.add_development_dependency 'rspec-its', '>= 1'
28
+ s.add_development_dependency 'rubocop'
27
29
  s.add_development_dependency 'simplecov', '>= 0'
30
+ s.add_development_dependency 'mutant'
31
+ s.add_development_dependency 'mutant-rspec'
28
32
  end
@@ -11,15 +11,14 @@ module Cot
11
11
  def initialize(*params)
12
12
  parse_params(params)
13
13
 
14
- puts self.class.klass.inspect
15
14
  # If you pass in different types of things here we can't be friends
16
- initialize_objects(@objects) unless @objects.first.is_a? self.class.klass
15
+ initialize_objects(@objects) unless @objects.first.instance_of? self.class.klass
17
16
 
18
17
  super @objects
19
18
  end
20
19
 
21
20
  def serializable_hash
22
- @objects.map(&:serializable_hash)
21
+ map(&:serializable_hash)
23
22
  end
24
23
 
25
24
  def to_json
@@ -27,11 +26,11 @@ module Cot
27
26
  end
28
27
 
29
28
  def exists?
30
- @objects.map(&:exists?).all?
29
+ map(&:exists?).all?
31
30
  end
32
31
 
33
32
  def errors
34
- Hash[@objects.reject(&:valid?).map { |x| [x.id, x.errors] }]
33
+ Hash[reject(&:valid?).map { |x| [x.id, x.errors] }]
35
34
  end
36
35
 
37
36
  def update_members(payload)
@@ -40,7 +39,7 @@ module Cot
40
39
  end
41
40
 
42
41
  def changed?
43
- @objects.map(&:changed?).include? true
42
+ map(&:changed?).include? true
44
43
  end
45
44
 
46
45
  private
@@ -48,9 +47,9 @@ module Cot
48
47
  def parse_params(params)
49
48
  until params.empty?
50
49
  item = params.shift
51
- if item.class == Class
50
+ if item.instance_of? Class
52
51
  self.class.klass = item
53
- elsif item.class == Array
52
+ elsif item.instance_of? Array
54
53
  @objects = item
55
54
  else
56
55
  options = item
@@ -61,21 +60,17 @@ module Cot
61
60
  end
62
61
 
63
62
  def parse_options(options)
64
- options = { sub_key: options } unless options.is_a?(Hash)
63
+ options = { sub_key: options } unless options.instance_of?(Hash)
65
64
  @options = options.with_indifferent_access
66
- @options[:default_attributes] = {} unless @options[:default_attributes].is_a?(Hash)
65
+ @options[:default_attributes] = {} unless @options[:default_attributes]
67
66
  self.class.set_default_values
68
67
  @options.merge! self.class.options
69
68
  end
70
69
 
71
70
  def initialize_objects(objects)
72
- @objects = []
73
71
  @objects = objects.map do |payload|
74
- if @options[:sub_key]
75
- self.class.klass.new @options[:default_attributes].merge(payload.fetch(@options[:sub_key], {}))
76
- else
77
- self.class.klass.new @options[:default_attributes].merge(payload || {})
78
- end
72
+ payload = payload.fetch(@options[:sub_key], {}) if @options[:sub_key]
73
+ self.class.klass.new @options[:default_attributes].merge(payload)
79
74
  end
80
75
 
81
76
  # Set the delegator methods to point to the new objects array
@@ -12,7 +12,7 @@ module Cot
12
12
  fail "#{name} tried to set value #{value}, which is already used by #{used[value]}. Enum values are unique."
13
13
  end
14
14
 
15
- self.class.send :define_method, name do
15
+ self.class.__send__ :define_method, name do
16
16
  value
17
17
  end
18
18
 
@@ -21,7 +21,7 @@ module Cot
21
21
  end
22
22
 
23
23
  def [](key)
24
- send key
24
+ public_send key
25
25
  end
26
26
  end
27
27
  end
@@ -6,28 +6,18 @@ module Cot
6
6
 
7
7
  include ActiveModel::Dirty
8
8
 
9
- def initialize(payload = {})
9
+ def initialize(payload = nil)
10
10
  @errors = {}
11
11
 
12
12
  @data = convert_keys payload
13
13
 
14
- @data.each do |k, v|
15
- if self.class.value_blocks[k]
16
- block = self.class.value_blocks[k]
17
- @data[k] = instance_exec(v, &block)
18
- end
19
- end
14
+ add_value_blocks
20
15
 
21
- defined_properties.each do |prop|
22
- if @data[prop].nil? && self.class.missing_blocks[prop]
23
- block = self.class.missing_blocks[prop]
24
- @data[prop] = instance_exec(self, &block)
25
- end
26
- end
16
+ add_missing_blocks
27
17
  end
28
18
 
29
19
  def exists?
30
- send self.class.primary_key
20
+ public_send self.class.primary_key
31
21
  end
32
22
 
33
23
  def defined_properties
@@ -35,7 +25,15 @@ module Cot
35
25
  end
36
26
 
37
27
  def properties_mapping
38
- self.class.mappings
28
+ self.class.mappings || {}
29
+ end
30
+
31
+ def value_blocks
32
+ self.class.value_blocks || {}
33
+ end
34
+
35
+ def missing_blocks
36
+ self.class.missing_blocks || {}
39
37
  end
40
38
 
41
39
  def inverted_properties_mapping
@@ -55,11 +53,11 @@ module Cot
55
53
  end
56
54
 
57
55
  def []=(key, value)
58
- if self.class.value_blocks[key]
59
- block = self.class.value_blocks[key]
56
+ if value_blocks[key]
57
+ block = value_blocks[key]
60
58
  value = instance_exec(value, &block)
61
59
  end
62
- @data[convert_key key] = value
60
+ @data[key] = value
63
61
  end
64
62
 
65
63
  def valid?
@@ -73,11 +71,8 @@ module Cot
73
71
  def serializable_hash(options = {})
74
72
  attrs = {}
75
73
  properties_list = defined_properties
76
- if options[:only]
77
- properties_list &= Array(options[:only]).map(&:to_sym)
78
- elsif options[:except]
79
- properties_list -= Array(options[:except]).map(&:to_sym)
80
- end
74
+ properties_list &= Array(options[:only]).map(&:to_sym) if options[:only]
75
+ properties_list -= Array(options[:except]).map(&:to_sym)
81
76
  properties_list.each do |m|
82
77
  attrs[inverted_properties_mapping.fetch(m, m)] = self[m]
83
78
  end
@@ -86,9 +81,27 @@ module Cot
86
81
 
87
82
  private
88
83
 
84
+ def add_missing_blocks
85
+ defined_properties.each do |prop|
86
+ if self[prop].nil? && missing_blocks[prop]
87
+ block = missing_blocks[prop]
88
+ self[prop] = instance_exec(&block)
89
+ end
90
+ end
91
+ end
92
+
93
+ def add_value_blocks
94
+ @data.each do |k, v|
95
+ if value_blocks[k]
96
+ block = value_blocks[k]
97
+ @data[k] = instance_exec(v, &block)
98
+ end
99
+ end
100
+ end
101
+
89
102
  def convert_key(key)
90
103
  key = key.to_sym
91
- properties_mapping.fetch(key, key).to_sym
104
+ properties_mapping.fetch(key, key)
92
105
  end
93
106
 
94
107
  def convert_keys(hash)
@@ -10,8 +10,6 @@ module Cot
10
10
  :mappings
11
11
 
12
12
  def search_property(name, args = {})
13
- @search_mappings ||= {}
14
-
15
13
  key = args[:from] ? args[:from] : name
16
14
  @search_mappings[name] = key
17
15
  end
@@ -42,15 +40,15 @@ module Cot
42
40
  private
43
41
 
44
42
  def set_blocks(name, prop)
45
- @value_blocks[name] = prop.value if prop.value
46
- @missing_blocks[name] = prop.missing if prop.missing
43
+ @value_blocks[name] = prop.value
44
+ @missing_blocks[name] = prop.missing
47
45
  end
48
46
 
49
47
  def set_mappings(name, prop)
50
48
  key = prop.from
51
- @mappings[key.to_sym] = name if key
49
+ @mappings[key] = name if key
52
50
  @search_mappings[name] = key ? key : name if prop.searchable
53
- attr_methods << name.to_sym
51
+ attr_methods << name
54
52
  end
55
53
 
56
54
  # Can't seem to get an intialize in for the class, so we need to set these
@@ -70,7 +68,7 @@ module Cot
70
68
  end
71
69
 
72
70
  define_method "#{name}=" do |value|
73
- send("#{name}_will_change!") unless value == self[name]
71
+ public_send("#{name}_will_change!") unless value.eql?(self[name])
74
72
  self[name] = value
75
73
  end
76
74
  define_attribute_method name
@@ -49,5 +49,4 @@ RSpec::Matchers.define :set_property do |field|
49
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
-
53
52
  end
@@ -1,3 +1,3 @@
1
1
  module Cot
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.2'
3
3
  end
@@ -16,7 +16,6 @@ describe Cot::Collection do
16
16
  end
17
17
 
18
18
  it 'should respond to array methods' do
19
-
20
19
  # In theory we'd test more, but that's good enough for me
21
20
  expect(collection).to respond_to :[]
22
21
  expect(collection).to respond_to :first
@@ -81,13 +80,21 @@ describe Cot::Collection do
81
80
  expect(coll.first.fooy).to eq :bar
82
81
  end
83
82
 
83
+ it 'takes options as strings and symbols' do
84
+ coll = Cot::Collection.new FakeDouble,
85
+ [{ inner: { fooy: :bar } }, { inner: { asdf: :fdas } }],
86
+ 'sub_key' => :inner
87
+ expect(coll.first).to be_kind_of FakeDouble
88
+ expect(coll.first.fooy).to eq :bar
89
+ end
90
+
84
91
  it 'takes an optional default_attributes option to add set attributes in every object.' do
85
92
  coll = Cot::Collection.new FakeDouble, [{ fooy: :bar }, { asdf: :fdas }], default_attributes: { foo: :baz }
86
93
  expect(coll).to all be_kind_of FakeDouble
87
94
  expect(coll.map(&:foo).uniq).to eq [:baz]
88
95
  end
89
96
 
90
- it 'support a legacy optional sub_key parameter to pull the object out of the payload' do
97
+ it 'supports a legacy optional sub_key parameter to pull the object out of the payload' do
91
98
  coll = Cot::Collection.new FakeDouble, [{ inner: { fooy: :bar } }, { inner: { asdf: :fdas } }], :inner
92
99
  expect(coll.first).to be_kind_of FakeDouble
93
100
  expect(coll.first.fooy).to eq :bar
@@ -96,7 +103,17 @@ describe Cot::Collection do
96
103
 
97
104
  context 'without options' do
98
105
  it 'does not process the objects if they are already the correct class' do
99
- coll = Cot::Collection.new FakeDouble, [FakeDouble.new(fooy: :bar), FakeDouble.new(asdf: :fdas)]
106
+ coll = Cot::Collection.new [FakeDouble.new(fooy: :bar), FakeDouble.new(asdf: :fdas)]
107
+ expect(coll.first).to be_kind_of FakeDouble
108
+ end
109
+
110
+ it 'does not set the subkey if none is provided' do
111
+ coll = Cot::Collection.new [FakeDouble.new(fooy: :bar), FakeDouble.new(asdf: :fdas)]
112
+ expect(coll.instance_variable_get :@options).to_not have_key :sub_key
113
+ end
114
+
115
+ it 'can accept empty hashes' do
116
+ coll = Cot::Collection.new FakeDouble, [{}]
100
117
  expect(coll.first).to be_kind_of FakeDouble
101
118
  end
102
119
 
@@ -23,4 +23,49 @@ describe Cot::Enum do
23
23
  expect(enum[:third]).to eq 3
24
24
  expect(enum[:fourth]).to eq 4
25
25
  end
26
+
27
+ it 'sets used keys and values' do
28
+ expect(TestObject.types.used).to have_key 1
29
+ expect(TestObject.types.used[1]).to eq :first
30
+ end
31
+
32
+ it 'does not allow duplicates' do
33
+ expect do
34
+ class TestObject < Cot::Frame
35
+ enum :types do
36
+ entry :first
37
+ entry :first_again, value: 1
38
+ end
39
+ end
40
+ end.to raise_error StandardError,
41
+ 'first_again tried to set value 1, which is already used by first. Enum values are unique.'
42
+ end
43
+
44
+ context 'objext' do
45
+ it 'sets the value starting at 1' do
46
+ expect(@foo.types.first).to eq 1
47
+ end
48
+
49
+ it 'allows the value to be set' do
50
+ expect(@foo.types.third).to eq 3
51
+ end
52
+
53
+ it 'increments after the next value' do
54
+ expect(@foo.types.fourth).to eq 4
55
+ end
56
+ end
57
+
58
+ context 'class' do
59
+ it 'sets the value starting at 1' do
60
+ expect(TestObject.types.first).to eq 1
61
+ end
62
+
63
+ it 'allows the value to be set' do
64
+ expect(TestObject.types.third).to eq 3
65
+ end
66
+
67
+ it 'increments after the next value' do
68
+ expect(TestObject.types.fourth).to eq 4
69
+ end
70
+ end
26
71
  end
@@ -2,15 +2,16 @@ require 'spec_helper'
2
2
  describe Cot::Frame do
3
3
  before :each do
4
4
  class TestObject < Cot::Frame
5
- property :foo, from: :bar
5
+ property :foo, from: :bar, searchable: true
6
6
  property :id
7
+ property :only, searchable: true
7
8
  search_property :john, from: :crichton
9
+ search_property :pilot
8
10
  end
9
- @foo = TestObject.new(bar: 'this will be foo', id: 5)
11
+ @foo = TestObject.new(bar: 'this will be foo', id: 5, only: 3)
10
12
  end
11
13
  subject { @foo }
12
14
  its(:to_json) { should be_kind_of String }
13
- it 'needs more serialziable tests'
14
15
  its(:id) { should eq 5 }
15
16
  its(:foo) { should eq 'this will be foo' }
16
17
 
@@ -21,7 +22,7 @@ describe Cot::Frame do
21
22
  context 'serializable_hash' do
22
23
  its(:serializable_hash) { should be_kind_of Hash }
23
24
  it 'has two keys' do
24
- expect(subject.serializable_hash.size).to eq 2
25
+ expect(subject.serializable_hash.size).to eq 3
25
26
  end
26
27
 
27
28
  it 'should accept an option hash' do
@@ -30,6 +31,12 @@ describe Cot::Frame do
30
31
  end.to_not raise_error
31
32
  end
32
33
 
34
+ it 'does not require an option hash' do
35
+ expect do
36
+ subject.serializable_hash
37
+ end.to_not raise_error
38
+ end
39
+
33
40
  context 'only option' do
34
41
  it 'should return properties specified' do
35
42
  expect(subject.serializable_hash(only: :foo).size).to eq 1
@@ -43,15 +50,27 @@ describe Cot::Frame do
43
50
 
44
51
  context 'except option' do
45
52
  it 'should not return properties specified' do
46
- expect(subject.serializable_hash(except: :foo).size).to eq 1
53
+ expect(subject.serializable_hash(except: :foo).size).to eq 2
47
54
  expect(subject.serializable_hash(except: :foo)[:id]).to eq 5
48
- expect(subject.serializable_hash(except: [:foo, :id]).size).to eq 0
49
- expect(subject.serializable_hash(except: 'foo').size).to eq 1
55
+ expect(subject.serializable_hash(except: [:foo, :id]).size).to eq 1
56
+ expect(subject.serializable_hash(except: 'foo').size).to eq 2
50
57
  expect(subject.serializable_hash(except: 'foo')[:id]).to eq 5
51
58
  end
52
59
  end
53
60
  end
54
61
 
62
+ context 'valid?' do
63
+ it 'returns true if there are no errors' do
64
+ expect(@foo).to receive(:errors).and_return []
65
+ expect(@foo.valid?).to be true
66
+ end
67
+
68
+ it 'returns false if there is an error' do
69
+ expect(@foo).to receive(:errors).and_return [1]
70
+ expect(@foo.valid?).to be false
71
+ end
72
+ end
73
+
55
74
  context 'exists?' do
56
75
  it 'is true if id is present' do
57
76
  expect(@foo.exists?).to be_truthy
@@ -70,8 +89,52 @@ describe Cot::Frame do
70
89
  it 'is an array' do
71
90
  expect(@foo.defined_properties).to be_kind_of Array
72
91
  end
92
+
93
+ it 'defaults to []' do
94
+ class EmptyObject < Cot::Frame
95
+ end
96
+ foo = EmptyObject.new
97
+ expect(foo.defined_properties).to eq []
98
+ end
99
+ end
100
+
101
+ context 'value_blocks' do
102
+ it 'defaults to {}' do
103
+ class EmptyObject < Cot::Frame
104
+ end
105
+ foo = EmptyObject.new
106
+ expect(foo.value_blocks).to eq({})
107
+ end
108
+
109
+ it 'has nil for no values' do
110
+ expect(@foo.value_blocks).to eq(foo: nil, id: nil, only: nil)
111
+ end
73
112
  end
113
+
114
+ context 'missing_blocks' do
115
+ it 'defaults to {}' do
116
+ class EmptyObject < Cot::Frame
117
+ end
118
+ foo = EmptyObject.new
119
+ expect(foo.missing_blocks).to eq({})
120
+ end
121
+
122
+ it 'has nil for no values' do
123
+ expect(@foo.missing_blocks).to eq(foo: nil, id: nil, only: nil)
124
+ end
125
+ end
126
+
74
127
  context 'properties_mapping' do
128
+ it 'defaults to {}' do
129
+ class EmptyObject < Cot::Frame
130
+ end
131
+ foo = EmptyObject.new
132
+ expect(foo.properties_mapping).to eq({})
133
+ end
134
+ it 'does not set if there is no from' do
135
+ expect(@foo.properties_mapping).to_not have_key nil
136
+ end
137
+
75
138
  it 'has bar => foo' do
76
139
  expect(@foo.properties_mapping).to have_key :bar
77
140
  expect(@foo.properties_mapping[:bar]).to eq :foo
@@ -99,6 +162,8 @@ describe Cot::Frame do
99
162
  it 'adds to search_mappings' do
100
163
  expect(TestObject.search_mappings).to have_key :john
101
164
  expect(TestObject.search_mappings[:john]).to be :crichton
165
+ expect(TestObject.search_mappings).to have_key :pilot
166
+ expect(TestObject.search_mappings[:pilot]).to be :pilot
102
167
  end
103
168
  end
104
169
  context 'property' do
@@ -107,6 +172,24 @@ describe Cot::Frame do
107
172
  expect(TestObject.mappings[:bar]).to be :foo
108
173
  end
109
174
 
175
+ it 'adds to search properties when searchable is true' do
176
+ expect(TestObject.search_mappings).to have_key :only
177
+ expect(TestObject.search_mappings[:only]).to be :only
178
+ expect(TestObject.search_mappings).to have_key :foo
179
+ expect(TestObject.search_mappings[:foo]).to be :bar
180
+ end
181
+
182
+ it 'does not add to search mappings if not searchable' do
183
+ expect(TestObject.search_mappings).to_not have_key :id
184
+ end
185
+
186
+ it 'works for strings and symbols' do
187
+ foo1 = TestObject.new(bar: 'this will be foo', 'id' => 1)
188
+ foo2 = TestObject.new(bar: 'this will be foo', id: 2)
189
+ expect(foo1.id).to eq 1
190
+ expect(foo2.id).to eq 2
191
+ end
192
+
110
193
  it 'creates accessor methods' do
111
194
  foo = TestObject.new
112
195
  expect(foo).to respond_to :foo
@@ -133,6 +216,7 @@ describe Cot::Frame do
133
216
  end
134
217
  class TestObject < Cot::Frame
135
218
  property :my_id, from: :key, primary: true
219
+ property :foo, from: :bar
136
220
  property :blank do
137
221
  missing do
138
222
  "this was blank #{my_id}"
@@ -159,8 +243,24 @@ describe Cot::Frame do
159
243
  expect(TestObject.search_mappings[:thing]).to be :stuff
160
244
  end
161
245
 
162
- it 'stores missing' do
163
- expect(@foo.blank).to eq 'this was blank 42'
246
+ context 'missing' do
247
+ it 'does not call the block if the value is provided' do
248
+ foo = TestObject.new(blank: 'blank')
249
+ expect(foo.blank).to eq 'blank'
250
+ end
251
+
252
+ it 'does not set the block if missing is false' do
253
+ foo = TestObject.new(blank: 'blank')
254
+ expect(foo.foo).to be_nil
255
+ end
256
+
257
+ it 'calls the value if there is a block' do
258
+ expect(@foo.blank).to eq 'this was blank 42'
259
+ end
260
+
261
+ it 'returns nil if there is no block' do
262
+ expect(@foo.id).to be_nil
263
+ end
164
264
  end
165
265
 
166
266
  it 'sets the primary key' do
@@ -172,11 +272,44 @@ describe Cot::Frame do
172
272
  expect(@foo.thing.params[:passed]).to eq 42
173
273
  end
174
274
 
175
- it 'sets the value on []=' do
176
- bar = TestObject.new(key: 42)
177
- bar.thing = { key: 'this will be in foo' }
178
- expect(bar.thing).to be_kind_of Foo
179
- expect(bar.thing.params[:passed]).to eq 42
275
+ context '[]' do
276
+ it 'accesses an unmapped key' do
277
+ expect(@foo[:blank]).to eq 'this was blank 42'
278
+ end
279
+
280
+ it 'accesses a mapped key' do
281
+ expect(@foo[:my_id]).to eq 42
282
+ end
283
+ end
284
+
285
+ context '[]=' do
286
+ it 'sets the value block' do
287
+ bar = TestObject.new(key: 42)
288
+ bar.thing = { key: 'this will be in foo' }
289
+ expect(bar.thing).to be_kind_of Foo
290
+ expect(bar.thing.params[:passed]).to eq 42
291
+ expect(bar[:stuff]).to be_kind_of Foo
292
+ expect(bar[:thing]).to be_kind_of Foo
293
+ end
294
+
295
+ it 'notifys that the field will change if the value is different' do
296
+ bar = TestObject.new(key: 42, foo: 'thing')
297
+ expect(bar).to receive(:foo_will_change!).and_return true
298
+ bar.foo = 'baz'
299
+ end
300
+
301
+ it 'does not notify that the field will change if the value is the same' do
302
+ bar = TestObject.new(key: 42, foo: 'baz')
303
+ expect(bar).to_not receive(:foo_will_change!)
304
+ bar.foo = 'baz'
305
+ end
306
+
307
+ it 'sets the value if it is not a block' do
308
+ bar = TestObject.new(key: 42)
309
+ bar.foo = 'baz'
310
+ expect(bar.foo).to eq 'baz'
311
+ expect(bar[:bar]).to eq 'baz'
312
+ end
180
313
  end
181
314
  end
182
315
  end
@@ -194,54 +327,4 @@ describe Cot::Frame do
194
327
  expect(@foo.errors[:status]).to eq 200
195
328
  end
196
329
  end
197
- context 'enum' do
198
- before :each do
199
- class TestObject < Cot::Frame
200
- enum :types do
201
- entry :first
202
- entry :third, value: 3
203
- entry :fourth
204
- end
205
- end
206
- @foo = TestObject.new
207
- end
208
- context 'object' do
209
- it 'sets the value starting at 1' do
210
- expect(@foo.types.first).to eq 1
211
- end
212
-
213
- it 'allows the value to be set' do
214
- expect(@foo.types.third).to eq 3
215
- end
216
-
217
- it 'increments after the next value' do
218
- expect(@foo.types.fourth).to eq 4
219
- end
220
- end
221
-
222
- context 'class' do
223
- it 'sets the value starting at 1' do
224
- expect(TestObject.types.first).to eq 1
225
- end
226
-
227
- it 'allows the value to be set' do
228
- expect(TestObject.types.third).to eq 3
229
- end
230
-
231
- it 'increments after the next value' do
232
- expect(TestObject.types.fourth).to eq 4
233
- end
234
- end
235
-
236
- it 'does not allow duplicates' do
237
- expect do
238
- class TestObject < Cot::Frame
239
- enum :types do
240
- entry :first
241
- entry :first_again, value: 1
242
- end
243
- end
244
- end.to raise_error StandardError
245
- end
246
- end
247
330
  end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+ describe Cot::Property do
3
+ it 'can set the primary attribute' do
4
+ prop = described_class.new
5
+ expect(prop.primary?).to be_nil
6
+ prop.primary
7
+ expect(prop.primary?).to be true
8
+ end
9
+ end
@@ -9,7 +9,15 @@ require 'cot'
9
9
  require 'rspec'
10
10
  require 'rspec/its'
11
11
 
12
+ require 'timeout'
13
+
12
14
  RSpec.configure do |config|
13
15
  config.color = true
14
16
  config.formatter = 'documentation'
17
+ # Add a timeout so mutant can't create infinite loops
18
+ config.around do |example|
19
+ Timeout.timeout(5) do
20
+ example.run
21
+ end
22
+ end
15
23
  end
metadata CHANGED
@@ -1,111 +1,153 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.2
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-11-06 00:00:00.000000000 Z
11
+ date: 2015-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
25
39
  - !ruby/object:Gem::Version
26
40
  version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: shoulda
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - '>='
45
+ - - ! '>='
32
46
  - !ruby/object:Gem::Version
33
47
  version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - '>='
52
+ - - ! '>='
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - '>='
59
+ - - ! '>='
46
60
  - !ruby/object:Gem::Version
47
61
  version: 1.0.0
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - '>='
66
+ - - ! '>='
53
67
  - !ruby/object:Gem::Version
54
68
  version: 1.0.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - '>='
73
+ - - ! '>='
60
74
  - !ruby/object:Gem::Version
61
75
  version: '0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - '>='
80
+ - - ! '>='
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rspec-its
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - '>='
87
+ - - ! '>='
74
88
  - !ruby/object:Gem::Version
75
- version: '0'
89
+ version: '1'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - '>='
94
+ - - ! '>='
81
95
  - !ruby/object:Gem::Version
82
- version: '0'
96
+ version: '1'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rubocop
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
- - - '>='
101
+ - - ! '>='
88
102
  - !ruby/object:Gem::Version
89
- version: 0.26.0
103
+ version: '0'
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
- - - '>='
108
+ - - ! '>='
95
109
  - !ruby/object:Gem::Version
96
- version: 0.26.0
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: simplecov
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
- - - '>='
115
+ - - ! '>='
102
116
  - !ruby/object:Gem::Version
103
117
  version: '0'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
- - - '>='
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: mutant
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'
139
+ - !ruby/object:Gem::Dependency
140
+ name: mutant-rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ! '>='
109
151
  - !ruby/object:Gem::Version
110
152
  version: '0'
111
153
  description: Simplifies creating models for rest based resources
@@ -121,6 +163,7 @@ files:
121
163
  - .ruby-gemset
122
164
  - .ruby-version
123
165
  - .simplecov
166
+ - .travis.yml
124
167
  - Gemfile
125
168
  - Gemfile.lock
126
169
  - LICENSE
@@ -139,6 +182,7 @@ files:
139
182
  - spec/lib/cot/collection_spec.rb
140
183
  - spec/lib/cot/enum_spec.rb
141
184
  - spec/lib/cot/frame_spec.rb
185
+ - spec/lib/cot/property_spec.rb
142
186
  - spec/spec_helper.rb
143
187
  homepage: http://github.com/crimsonknave/cot
144
188
  licenses:
@@ -150,22 +194,18 @@ require_paths:
150
194
  - lib
151
195
  required_ruby_version: !ruby/object:Gem::Requirement
152
196
  requirements:
153
- - - '>='
197
+ - - ! '>='
154
198
  - !ruby/object:Gem::Version
155
199
  version: '0'
156
200
  required_rubygems_version: !ruby/object:Gem::Requirement
157
201
  requirements:
158
- - - '>='
202
+ - - ! '>='
159
203
  - !ruby/object:Gem::Version
160
204
  version: '0'
161
205
  requirements: []
162
206
  rubyforge_project:
163
- rubygems_version: 2.2.2
207
+ rubygems_version: 2.4.5
164
208
  signing_key:
165
209
  specification_version: 4
166
210
  summary: Simplifies creating models for rest based resources
167
- test_files:
168
- - spec/lib/cot/collection_spec.rb
169
- - spec/lib/cot/enum_spec.rb
170
- - spec/lib/cot/frame_spec.rb
171
- - spec/spec_helper.rb
211
+ test_files: []