cot 0.1.0 → 0.1.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: e7af1b1d15636b337c3561a40786372db55c0400
4
- data.tar.gz: 3eee1cb6d6636ec6f1b644c78f268172896ac8b2
3
+ metadata.gz: 531b8bbb2c943a86b2d9041c9b084f5dd4c9bc06
4
+ data.tar.gz: f0cebfb0d17cffa7a1f69584662644da4fdfce42
5
5
  SHA512:
6
- metadata.gz: 75cda344dff86f9b42307a8ae9dbcc913a6c7f9de48a2590c6ae578532a38f20447917da9a25b4b52de1cb14e74144a6153ec2edd4bf29e534e606f3f0765bc9
7
- data.tar.gz: 417ea29c3e87cd919e063e4e7d55a3a344d81d4593cd1fec6cb5f0bb9554e289203ad6008b76bfd694e3c639a56b43bbb9f78307cd61321f7364caf299ca9d59
6
+ metadata.gz: f294e13567ac759dff0966a1fc821428d93a749c483de4bc1c0053636e0e13afb03cfd5f3f9c6d789e4e37c2994047fa5f7c8ee201e03b2da26462201cafc590
7
+ data.tar.gz: 2af05c7c27c7ccf7cf0cd39390f9e59c396e00511d68b415e85a25ad000565f3fddb00270dd33d92fb2e5d93e18b48fc858c42fdec917efc6ca958048f2d2be9
data/README.md CHANGED
@@ -1,4 +1,36 @@
1
1
  cot
2
2
  ===
3
3
 
4
- A gem that helps convert rest calls into models
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
+
6
+ ### Usage
7
+
8
+ Using cot is pretty simple, include it in your project and then have your class inherit from Cot::Frame.
9
+
10
+ Frame provides some helpful methods:
11
+ - Class Methods
12
+ - property adds the first parameter as an accessor on the object. There are two optional arguments, from and searchable. From indicates that the property has an alternate key in the incoming/outgoing data. Searchable adds the property to the search mappings.
13
+ - 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.
14
+ - Instance Methods
15
+ - defined\_properties returns a list of the defined properties
16
+ - properties\_mapping returns a hash containing all of the renamed properties. The keys are the values of the from argument and the values are the property name.
17
+ - inverted\_properties\_mapping returns an inverted hash containing all of the renamed properties. This is the same as properties\_mapping but the property names are the keys and the froms are the values
18
+ - search\_mappings returns a hash containing the search properties. If a from was provided then that is the key otherwise the key and the value will be the property name.
19
+ - inverted\_search\_mappings returns an inverted search\_mappings hash.
20
+ - serializable\_hash returns hash with the correct keys to post back. AKA, it reverts the keys to what the from arguments (if any) were.
21
+ - to\_json returns a json encoded version of serializable\_hash.
22
+
23
+ ```ruby
24
+ class ExampleObject < Cot::Frame
25
+ property :id
26
+ property :name, :searchable => true
27
+ property :company_name, :from => :companyName
28
+ search_property :created_at, :from => :createdOn
29
+ end
30
+
31
+ thingy = ExampleObject.new(id: :my_id, name: 'awesome name', createdOn: Time.now)
32
+ thingy.id # 5
33
+ thingy.name # awesome name
34
+ thingy.created_at # what time it is now
35
+ thingy.defined\_properties # [:id, :name, :created_at]
36
+ ```
@@ -10,6 +10,7 @@ module Cot
10
10
  end
11
11
 
12
12
  def exists?
13
+ #TODO: Have this key off a defined primary key instead of defaulting to id
13
14
  id
14
15
  end
15
16
 
@@ -0,0 +1,54 @@
1
+ RSpec::Matchers.define :set_search_property do |field|
2
+ match do |base|
3
+ if @from
4
+ base.search_mappings[field] == @from
5
+ else
6
+ base.search_mappings[field] == field
7
+ end
8
+ end
9
+
10
+ def from(from)
11
+ @from = from
12
+ self
13
+ end
14
+ end
15
+ RSpec::Matchers.define :set_property do |field|
16
+ match do |base|
17
+ @tests = {}
18
+ @tests[:attr_methods] = base.attr_methods.include?(field.to_sym)
19
+ if @from
20
+ @tests[:mappings] = base.mappings[@from.to_sym] == field
21
+ end
22
+ if @searchable
23
+ key = @from ? @from : field
24
+ @tests[:searchable] = base.search_mappings[field] == key
25
+ end
26
+ example = base.new
27
+ @tests[:reader] = example.respond_to?(field)
28
+ @tests[:accessor] = example.respond_to?("#{field}=")
29
+ @tests[:dirty] = example.respond_to?("#{field}_changed?")
30
+ @tests.values.all?
31
+ end
32
+
33
+ def from(from)
34
+ @from = from
35
+ self
36
+ end
37
+ def searchable
38
+ @searchable = true
39
+ self
40
+ end
41
+
42
+ description do
43
+ from_string = @from ? "from #{@from}" : ""
44
+ search_string = @searchable ? "to be searchable" : ""
45
+ "should set property #{field} #{from_string} #{search_string}"
46
+ end
47
+
48
+ failure_message_for_should do
49
+ failed = @tests.select{|k,v| !v}.keys
50
+ "Expected the property #{field} to be set, but the following attributes weren't set correctly #{failed}"
51
+ end
52
+
53
+ end
54
+
@@ -1,3 +1,3 @@
1
1
  module Cot
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
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.1.0
4
+ version: 0.1.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: 2013-08-22 00:00:00.000000000 Z
11
+ date: 2014-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -84,6 +84,7 @@ files:
84
84
  - cot.gemspec
85
85
  - lib/cot.rb
86
86
  - lib/cot/frame.rb
87
+ - lib/cot/rspec/matchers.rb
87
88
  - lib/cot/version.rb
88
89
  - spec/lib/cot/frame_spec.rb
89
90
  - spec/spec_helper.rb
@@ -106,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
107
  version: '0'
107
108
  requirements: []
108
109
  rubyforge_project:
109
- rubygems_version: 2.0.6
110
+ rubygems_version: 2.1.9
110
111
  signing_key:
111
112
  specification_version: 4
112
113
  summary: Simplifies creating models for rest based resources