cot 0.4.0 → 0.4.1

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: bce57021ecbcd6f8ea2c6952a36d4ea28b38bf99
4
- data.tar.gz: 26f2711ae9016dda7b0c633bae9e9f3a04821e44
3
+ metadata.gz: 80b8c78bd9fd2bedc92cfd64079c168662910a8b
4
+ data.tar.gz: d9f342d772816b891a3fa12d5db8a468e1bd0a58
5
5
  SHA512:
6
- metadata.gz: 9b38415ebef6aec060d976c7f235ecb20db19d823ec131a3367d600a37484e69b4525a6ebd3f0b0f37b7745893ec78e4de6cdd1095517caf664d8f08b68a550c
7
- data.tar.gz: ef4bc4c2b3129929629ae8ec2ff582eba6ac915b51d83d1008cc90f5367ba8ed5d4ce5759200a3d7632ae0b255e74c02a7ad7ba0a7f886b501c94832fcc00f60
6
+ metadata.gz: b6e988adce02b522f15553a385ecef83d990a08b2ae9b8065fdfc0fd3ce0089ec3a9f35c93f1719e46d936383f6300adcb6c0dfd2f335fb137fc40d181d94b21
7
+ data.tar.gz: 5f701c6224a24df45a82eee77fd87bf6fcb3a55788e37836bce51dec8988150dd7eccbca623764b1369d0eb0c996665df43b533c034e2509aea93e2c2fbb7589
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cot (0.3.0)
4
+ cot (0.4.0)
5
5
  activemodel
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -3,7 +3,58 @@ cot
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.
5
5
 
6
- ### Usage
6
+ ### Example
7
+
8
+ ```ruby
9
+ class NestedClass < Cot::Frame
10
+ property :parent_id
11
+ property :foo, from: :bar
12
+ end
13
+
14
+ class ExampleObject < Cot::Frame
15
+ property :id
16
+ property :name, :searchable => true
17
+ property :company_name, :from => :companyName
18
+ property :item do
19
+ from :place
20
+ value do |params|
21
+ NestedClass.new params.merge parent_id: id
22
+ end
23
+ end
24
+ enum :types do
25
+ entry :first
26
+ entry :third, value: 3
27
+ entry :fourth
28
+ end
29
+ search_property :created_at, :from => :createdOn
30
+ end
31
+
32
+ class ExampleCollection < Cot::Collection
33
+ def initialize(objects, options = {})
34
+ super ExampleObject, objects, options
35
+ end
36
+ end
37
+
38
+ thingy = ExampleObject.new(id: :my_id, name: 'awesome name', createdOn: Time.now, place: {bar: 'this is nested.foo'})
39
+ thingy.id # 5
40
+ thingy.name # awesome name
41
+ ExampleObject.types.first # 1
42
+ thingy.types.fourth # 4
43
+ thingy.item # NestedClass instance
44
+ thingy.item.foo # 'this is nested.foo'
45
+ thingy.created_at # what time it is now
46
+ thingy.defined_properties # [:id, :name, :created_at]
47
+
48
+ collection = ExampleCollection.new [{ id: :my_id, name: 'awesome name', createdOn: Time.now }, { id: :my_id, name: 'awesome name', createdOn: Time.now }], { default_attributes: { default: :attribute }
49
+ collection.first.name # 'awesome name'
50
+ collection.first.default # :attribute
51
+ collection.exists? # Do all of the entries exist?
52
+ collection.update_members [{ id: 1, name: 'new awesome name', createdOn: Time.now }, { id: 2, name: 'new awesome name', createdOn: Time.now }]
53
+ collection.first.name # 'new awesome name'
54
+ ```
55
+
56
+
57
+ ### Details
7
58
 
8
59
  Using cot is pretty simple. There are two main classes: Collection and Frame. Collections are basically big arrays that contain objects (presumably Frame objects). Collection provides some helper methods to manage the collection, but also delegates to Array, so each, map and all that good stuff are there as well. Frame allows you to declare how the object will convert a json payload into an object.
9
60
 
@@ -12,8 +63,8 @@ Frame provides some helpful methods:
12
63
  - property
13
64
  - The first parameter is the name of the property and it is added as a method to the object.
14
65
  - You can pass additional options in two ways, first you can pass a hash of options to property and secondly you can pass a block to property.
15
- - There are three optional arguments, value, from and searchable.
16
- - From indicates that the property has an alternate key in the incoming/outgoing data.
66
+ - There are three optional arguments, value, from and searchable.
67
+ - From indicates that the property has an alternate key in the incoming/outgoing data.
17
68
  - Searchable adds the property to the search mappings.
18
69
  - Value takes a block and overwrites the value of the property to be the result of the block
19
70
  - This is useful for nested objects.
@@ -21,6 +72,7 @@ Frame provides some helpful methods:
21
72
  - The block takes one parameter, which is the value of the hash for that key (what the value would have been if there was no value block).
22
73
  - search\_property adds the parameter to the search mapping. It takes an optional from argument which inidates the property has an alternate key in the incoming/outgoing data.
23
74
  - enum takes a name and a block
75
+ - Enums are defined on the instances and the class
24
76
  - The block expects a series of entries to be declared
25
77
  - enum starts counting at 1 by default
26
78
  - Each entry will have the value of 1 higher than the previous by default
@@ -49,42 +101,3 @@ Collection provides the following methods:
49
101
  - exists? returns true if *all* the members exist
50
102
  - changed? returns true if *any* of the members have changed
51
103
  - update\_members updates the members of the collection to based on the payload (this can add or remove members)
52
-
53
- ```ruby
54
- class ExampleObject < Cot::Frame
55
- property :id
56
- property :name, :searchable => true
57
- property :company_name, :from => :companyName
58
- property :item do
59
- from :place
60
- value do |params|
61
- MyClass.new params.merge parent_id: id
62
- end
63
- end
64
- enum :types do
65
- entry :first
66
- entry :third, value: 3
67
- entry :fourth
68
- end
69
- search_property :created_at, :from => :createdOn
70
- end
71
-
72
- class ExampleCollection < Cot::Collection
73
- def initialize(objects, options = {})
74
- super ExampleObject, objects, options
75
- end
76
- end
77
-
78
- thingy = ExampleObject.new(id: :my_id, name: 'awesome name', createdOn: Time.now)
79
- thingy.id # 5
80
- thingy.name # awesome name
81
- thingy.created_at # what time it is now
82
- thingy.defined\_properties # [:id, :name, :created_at]
83
-
84
- collection = ExampleCollection.new [{ id: :my_id, name: 'awesome name', createdOn: Time.now }, { id: :my_id, name: 'awesome name', createdOn: Time.now }], { default_attributes: { default: :attribute }
85
- collection.first.name # 'awesome name'
86
- collection.first.default # :attribute
87
- collection.exists? # Do all of the entries exist?
88
- collection.update_members [{ id: 1, name: 'new awesome name', createdOn: Time.now }, { id: 2, name: 'new awesome name', createdOn: Time.now }]
89
- collection.first.name # 'new awesome name'
90
- ```
@@ -17,6 +17,9 @@ module Cot
17
17
  def enum(name, &block)
18
18
  obj = Enum.new
19
19
  obj.instance_eval(&block)
20
+ define_singleton_method name do
21
+ obj
22
+ end
20
23
  define_method name do
21
24
  obj
22
25
  end
@@ -1,3 +1,3 @@
1
1
  module Cot
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.1'
3
3
  end
@@ -181,16 +181,32 @@ describe Cot::Frame do
181
181
  end
182
182
  @foo = TestObject.new
183
183
  end
184
- it 'sets the value starting at 1' do
185
- expect(@foo.types.first).to eq 1
186
- end
184
+ context 'object' do
185
+ it 'sets the value starting at 1' do
186
+ expect(@foo.types.first).to eq 1
187
+ end
187
188
 
188
- it 'allows the value to be set' do
189
- expect(@foo.types.third).to eq 3
189
+ it 'allows the value to be set' do
190
+ expect(@foo.types.third).to eq 3
191
+ end
192
+
193
+ it 'increments after the next value' do
194
+ expect(@foo.types.fourth).to eq 4
195
+ end
190
196
  end
191
197
 
192
- it 'increments after the next value' do
193
- expect(@foo.types.fourth).to eq 4
198
+ context 'class' do
199
+ it 'sets the value starting at 1' do
200
+ expect(TestObject.types.first).to eq 1
201
+ end
202
+
203
+ it 'allows the value to be set' do
204
+ expect(TestObject.types.third).to eq 3
205
+ end
206
+
207
+ it 'increments after the next value' do
208
+ expect(TestObject.types.fourth).to eq 4
209
+ end
194
210
  end
195
211
 
196
212
  it 'does not allow duplicates' 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.4.0
4
+ version: 0.4.1
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-09-30 00:00:00.000000000 Z
11
+ date: 2014-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel