cot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e7af1b1d15636b337c3561a40786372db55c0400
4
+ data.tar.gz: 3eee1cb6d6636ec6f1b644c78f268172896ac8b2
5
+ SHA512:
6
+ metadata.gz: 75cda344dff86f9b42307a8ae9dbcc913a6c7f9de48a2590c6ae578532a38f20447917da9a25b4b52de1cb14e74144a6153ec2edd4bf29e534e606f3f0765bc9
7
+ data.tar.gz: 417ea29c3e87cd919e063e4e7d55a3a344d81d4593cd1fec6cb5f0bb9554e289203ad6008b76bfd694e3c639a56b43bbb9f78307cd61321f7364caf299ca9d59
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ .*swp
@@ -0,0 +1 @@
1
+ cot
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p247
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cot (0.1.0)
5
+ activemodel
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.0.0)
11
+ activesupport (= 4.0.0)
12
+ builder (~> 3.1.0)
13
+ activesupport (4.0.0)
14
+ i18n (~> 0.6, >= 0.6.4)
15
+ minitest (~> 4.2)
16
+ multi_json (~> 1.3)
17
+ thread_safe (~> 0.1)
18
+ tzinfo (~> 0.3.37)
19
+ atomic (1.1.13)
20
+ builder (3.1.4)
21
+ diff-lcs (1.2.4)
22
+ i18n (0.6.5)
23
+ minitest (4.7.5)
24
+ multi_json (1.7.9)
25
+ rspec (2.14.1)
26
+ rspec-core (~> 2.14.0)
27
+ rspec-expectations (~> 2.14.0)
28
+ rspec-mocks (~> 2.14.0)
29
+ rspec-core (2.14.5)
30
+ rspec-expectations (2.14.2)
31
+ diff-lcs (>= 1.1.3, < 2.0)
32
+ rspec-mocks (2.14.3)
33
+ shoulda (3.5.0)
34
+ shoulda-context (~> 1.0, >= 1.0.1)
35
+ shoulda-matchers (>= 1.4.1, < 3.0)
36
+ shoulda-context (1.1.5)
37
+ shoulda-matchers (2.3.0)
38
+ activesupport (>= 3.0.0)
39
+ thread_safe (0.1.2)
40
+ atomic
41
+ tzinfo (0.3.37)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ bundler (>= 1.0.0)
48
+ cot!
49
+ rspec
50
+ shoulda
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Joseph Henrich
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ cot
2
+ ===
3
+
4
+ A gem that helps convert rest calls into models
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new('spec') do |spec|
7
+ spec.pattern = 'spec/**/*_spec.rb'
8
+ spec.rspec_opts = '--tag ~broken'
9
+ end
10
+
11
+
12
+ task :default => :spec
13
+ task :test => :spec
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cot/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'cot'
7
+ s.version = Cot::VERSION
8
+
9
+ s.authors = ["Joseph Henrich"]
10
+ s.email = ['crimsonknave@gmail.com']
11
+ s.homepage = %q{http://github.com/crimsonknave/cot}
12
+ s.summary = %q{Simplifies creating models for rest based resources}
13
+ s.description = %q{Simplifies creating models for rest based resources}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'activemodel'
21
+ s.add_development_dependency 'shoulda', ">= 0"
22
+ s.add_development_dependency 'bundler', ">= 1.0.0"
23
+ s.add_development_dependency 'rspec', ">= 0"
24
+ end
25
+
@@ -0,0 +1,12 @@
1
+ require 'active_model'
2
+
3
+ require 'cot/version'
4
+ require 'cot/frame'
5
+ require 'json'
6
+
7
+ module Cot
8
+
9
+ def version_string
10
+ "Cot version #{Cot::VERSION}"
11
+ end
12
+ end
@@ -0,0 +1,105 @@
1
+ module Cot
2
+ class Frame
3
+ class << self
4
+ attr_accessor :mappings, :inverted_mappings, :attr_methods, :search_mappings, :inverted_search_mappings
5
+ end
6
+ include ActiveModel::Dirty
7
+
8
+ def initialize(payload={})
9
+ @data = convert_keys payload
10
+ end
11
+
12
+ def exists?
13
+ id
14
+ end
15
+
16
+ def defined_properties
17
+ self.class.attr_methods
18
+ end
19
+ def properties_mapping
20
+ self.class.mappings
21
+ end
22
+
23
+ def inverted_properties_mapping
24
+ self.class.inverted_mappings ||= properties_mapping.invert
25
+ end
26
+
27
+ def search_mappings
28
+ self.class.search_mappings
29
+ end
30
+
31
+ def inverted_search_mappings
32
+ self.class.inverted_search_mappings ||= search_mappings.invert
33
+ end
34
+
35
+ def self.search_property(name, args={})
36
+ @search_mappings ||= {}
37
+
38
+ key = args[:from] ? args[:from] : name
39
+ @search_mappings[name] = key
40
+ end
41
+
42
+ # TODO: Create an enum declaration that will automagically map a symbol to
43
+ # another value (such as an int) so that the user of the library doesn't need
44
+ # to know what number scheduled status is (for example)
45
+ def self.enum(name, args={})
46
+ raise "enum is not yet implemented"
47
+ end
48
+
49
+ def self.property(name, args={})
50
+ @mappings ||= {}
51
+ @attr_methods ||= []
52
+ @search_mappings ||= {}
53
+ key = args[:from]
54
+ @mappings[key.to_sym] = name if key
55
+ @search_mappings[name] = key ? key : name if args[:searchable]
56
+ attr_methods << name.to_sym
57
+
58
+ define_method name do
59
+ self[name]
60
+ end
61
+
62
+ define_method "#{name}=" do |value|
63
+ send("#{name}_will_change!") unless value == self[name]
64
+ self[name] = value
65
+ end
66
+ define_attribute_method name
67
+ end
68
+
69
+ def [](key)
70
+ @data[convert_key key]
71
+ end
72
+
73
+ def []=(key, value)
74
+ @data[convert_key key] = value
75
+ end
76
+
77
+ def to_json
78
+ serializable_hash.to_json
79
+ end
80
+
81
+ def serializable_hash
82
+ attrs = {}
83
+ defined_properties.each do |m|
84
+ attrs[inverted_properties_mapping.fetch(m,m)] = self[m]
85
+ end
86
+ attrs
87
+ end
88
+
89
+ private
90
+ def convert_key(key)
91
+ key = key.to_sym
92
+ properties_mapping.fetch(key, key).to_sym
93
+ end
94
+
95
+ def convert_keys(hash)
96
+ return {} unless hash
97
+ {}.tap do |ret|
98
+ hash.each_pair do |k,v|
99
+ ret[convert_key k] = v
100
+ end
101
+ end
102
+ end
103
+
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ module Cot
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+ describe Cot::Frame do
3
+ before :each do
4
+ class TestObject < Cot::Frame
5
+ property :foo, :from => :bar
6
+ property :id
7
+ search_property :john, :from => :crichton
8
+ end
9
+ @foo = TestObject.new(:bar => 'this will be foo', :id => 5)
10
+ end
11
+ subject { @foo }
12
+ its(:to_json) { should be_kind_of String }
13
+ its(:serializable_hash) { should be_kind_of Hash }
14
+ its(:serializable_hash) { should have(2).keys }
15
+ it 'should have more serialziable tests'
16
+ its(:id) { should eq 5 }
17
+ its(:foo) { should eq 'this will be foo' }
18
+
19
+ context 'exists?' do
20
+ it 'should be true if id is present' do
21
+ @foo.exists?.should be_true
22
+ end
23
+ it 'should be false if id is not present' do
24
+ foo = TestObject.new(:foo => 5)
25
+ foo.exists?.should be_false
26
+ end
27
+ end
28
+ context 'defined_properties' do
29
+ it 'should include foo' do
30
+ @foo.defined_properties.should include :foo
31
+ end
32
+ it 'should be an array' do
33
+ @foo.defined_properties.should be_kind_of Array
34
+ end
35
+ end
36
+ context 'properties_mapping' do
37
+ it 'should have bar => foo' do
38
+ @foo.properties_mapping.should have_key :bar
39
+ @foo.properties_mapping[:bar].should eq :foo
40
+ end
41
+ end
42
+ context 'inverted_properties_mapping' do
43
+ it 'should have foo => bar' do
44
+ @foo.inverted_properties_mapping.should have_key :foo
45
+ @foo.inverted_properties_mapping[:foo].should eq :bar
46
+ end
47
+ end
48
+ context 'search_mappings' do
49
+ it 'should have john => crichton' do
50
+ @foo.search_mappings.should have_key :john
51
+ @foo.search_mappings[:john].should eq :crichton
52
+ end
53
+ end
54
+ context 'inverted_search_mappings' do
55
+ it 'should have crichton => john' do
56
+ @foo.inverted_search_mappings.should have_key :crichton
57
+ @foo.inverted_search_mappings[:crichton].should eq :john
58
+ end
59
+ end
60
+ context 'search_property' do
61
+ it 'should add to search_mappings' do
62
+ TestObject.search_mappings.should have_key :john
63
+ TestObject.search_mappings[:john].should be :crichton
64
+ end
65
+ end
66
+ context 'property' do
67
+ it 'should add to mappings' do
68
+ TestObject.mappings.should have_key :bar
69
+ TestObject.mappings[:bar].should be :foo
70
+ end
71
+ it 'should create accessor methods' do
72
+ foo = TestObject.new
73
+ foo.should respond_to :foo
74
+ foo.should respond_to :foo=
75
+ end
76
+
77
+ it 'should add to attr_methods' do
78
+ TestObject.attr_methods.should include(:foo)
79
+ end
80
+
81
+ it 'accessor methods should use []' do
82
+ foo = TestObject.new
83
+ foo.should_receive('[]').once.and_return 'this is foo'
84
+ foo.foo.should eq 'this is foo'
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,9 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+
3
+ require 'cot'
4
+ require 'rspec'
5
+
6
+ RSpec.configure do |config|
7
+ config.color_enabled = true
8
+ config.formatter = 'documentation'
9
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Henrich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: shoulda
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
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Simplifies creating models for rest based resources
70
+ email:
71
+ - crimsonknave@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .ruby-gemset
78
+ - .ruby-version
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - cot.gemspec
85
+ - lib/cot.rb
86
+ - lib/cot/frame.rb
87
+ - lib/cot/version.rb
88
+ - spec/lib/cot/frame_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: http://github.com/crimsonknave/cot
91
+ licenses: []
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Simplifies creating models for rest based resources
113
+ test_files:
114
+ - spec/lib/cot/frame_spec.rb
115
+ - spec/spec_helper.rb