ocm 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .idea/
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ocm (0.0.1)
5
+ iron_cache
6
+ jsonable
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ iron_cache (1.1.2)
12
+ iron_core (>= 0.2.0)
13
+ iron_core (0.3.3)
14
+ rest (>= 2.0.0)
15
+ jsonable (0.0.3)
16
+ mime-types (1.19)
17
+ minitest (3.3.0)
18
+ net-http-persistent (2.7)
19
+ rake (0.9.2.2)
20
+ rest (2.0.2)
21
+ net-http-persistent
22
+ rest-client (>= 0.3.0)
23
+ rest-client (1.6.7)
24
+ mime-types (>= 1.16)
25
+ test-unit (2.5.1)
26
+ uber_config (0.0.6)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ minitest
33
+ ocm!
34
+ rake
35
+ test-unit
36
+ uber_config
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012, Iron.io, Inc. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions are met:
5
+
6
+ * Redistributions of source code must retain the above copyright notice,
7
+ this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright notice,
9
+ this list of conditions and the following disclaimer in the documentation
10
+ and/or other materials provided with the distribution.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
19
+ OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
21
+ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ OCM - An object-cache-mapper for IronCache.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ p ENV['gem']
10
+ end
11
+
12
+ task :default => :test
13
+
14
+ require 'rdoc/task'
15
+ Rake::RDocTask.new do |rdoc|
16
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
17
+
18
+ rdoc.rdoc_dir = 'doc'
19
+ rdoc.title = " #{version}"
20
+ rdoc.rdoc_files.include('README*')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/lib/ocm/idable.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'securerandom'
2
+
3
+ module Ocm
4
+ module Idable
5
+
6
+ def id
7
+ @id
8
+ end
9
+
10
+ def id=(id)
11
+ @id = id
12
+ end
13
+
14
+ def self.generate_id
15
+ SecureRandom.uuid()
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,113 @@
1
+ require 'iron_cache'
2
+
3
+ module Ocm
4
+ class Orm
5
+
6
+ def initialize(options={})
7
+ @iron_cache = IronCache::Client.new(options)
8
+ @cache = @iron_cache.cache(options[:cache_name] || "ocm")
9
+ end
10
+
11
+ def key_for(idable, id=nil)
12
+ if id
13
+ # idable should be a class
14
+ "#{idable.name.downcase}_#{id}"
15
+ else
16
+ "#{idable.class.name.downcase}_#{idable.id}"
17
+ end
18
+ end
19
+
20
+ def save(idable)
21
+ unless idable.id
22
+ idable.id = Idable.generate_id
23
+ end
24
+ put(key_for(idable), idable)
25
+
26
+ end
27
+
28
+ def save_list(key, array)
29
+ put(key, array)
30
+ end
31
+
32
+ def get_list(key)
33
+ messages = get(key)
34
+ #puts 'got messages: ' + messages.inspect
35
+ if messages.nil?
36
+ messages = []
37
+ end
38
+ messages
39
+ end
40
+
41
+ def prepend_to_list(key, item)
42
+ messages = get_list(key)
43
+ messages.unshift(item)
44
+ put(key, messages)
45
+ end
46
+
47
+ def append_to_list(key, item)
48
+ messages = get_list(key)
49
+ messages.push(item)
50
+ put(key, messages)
51
+ end
52
+
53
+ def find(clazz, id)
54
+ get(key_for(clazz, id))
55
+ end
56
+
57
+ def get(key)
58
+ val = @cache.get(key)
59
+ #puts 'got val: ' + val.inspect
60
+ if val.nil?
61
+ puts "GOT NIL for key #{key}"
62
+ return nil
63
+ end
64
+ val = val.value
65
+ #puts "got from cache #{val}"
66
+ begin
67
+ val = JSON.parse(val)
68
+ if val.is_a?(Hash) && val['string']
69
+ val = val['string']
70
+ end
71
+ rescue => ex
72
+ puts "CAUGHT: #{ex.message}"
73
+ end
74
+ val
75
+ end
76
+
77
+ def put(key, value, options={})
78
+ if value.is_a?(String)
79
+ # need to wrap it
80
+ value = {string: value}.to_json
81
+ else
82
+ value = value.to_json
83
+ end
84
+ #puts 'value jsoned: ' + value.inspect
85
+ @cache.put(key, value, options)
86
+ end
87
+
88
+ def delete(key)
89
+ @cache.delete(key)
90
+ end
91
+
92
+ def increment(key, value=1)
93
+ puts 'INC'
94
+ begin
95
+ ret = @cache.increment(key)
96
+ puts 'INC RET ' + ret.inspect
97
+ return ret.value
98
+ rescue Rest::HttpError, IronCore::ResponseError => ex
99
+ p ex
100
+ puts "couldn't increment #{key}"
101
+ puts ex.message
102
+ p ex.backtrace
103
+ if ex.code == 404
104
+ puts "setting value in cache"
105
+ settings.iron_cache.put(key, 1)
106
+ end
107
+ puts "done increment"
108
+ return 1
109
+ end
110
+ puts 'end inc'
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,37 @@
1
+ require 'jsonable'
2
+ require 'ostruct'
3
+ require_relative 'idable'
4
+
5
+ module Ocm
6
+ module Ormable
7
+
8
+ def self.included(by)
9
+ puts "included by #{by}"
10
+ by.class_eval do
11
+ include Idable
12
+ include Jsonable
13
+ end
14
+ by.extend(ClassMethods)
15
+ end
16
+
17
+ module ClassMethods
18
+ # put class methods here
19
+ end
20
+
21
+ def initialize(options={})
22
+ options.each_pair do |key,value|
23
+ var_name = "@#{key}"
24
+ # self.class.__send__(:attr_accessor, var_name)
25
+ # self.__send__("")
26
+ instance_variable_set var_name, value
27
+ # send(:define_method, var_name)
28
+ end
29
+ # @struct = OpenStruct.new(options)
30
+ end
31
+
32
+ #def method_missing(method, *args, &block)
33
+ # @source.send(method, *args, &block)
34
+ #end
35
+
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Ocm
2
+ VERSION = "0.0.2"
3
+ end
data/lib/ocm.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative 'ocm/version'
2
+ require_relative 'ocm/ormable'
3
+ require_relative 'ocm/iron_cache_orm'
data/ocm.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ require File.expand_path('../lib/ocm/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Travis Reeder"]
5
+ gem.email = ["treeder@gmail.com"]
6
+ gem.description = "ORM for IronCache"
7
+ gem.summary = "ORM for IronCache"
8
+ gem.homepage = "https://github.com/treeder/"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "ocm"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = Ocm::VERSION
16
+
17
+ gem.required_rubygems_version = ">= 1.3.6"
18
+ gem.required_ruby_version = Gem::Requirement.new(">= 1.9")
19
+ gem.add_runtime_dependency "iron_cache" #, ">= 0.1.0"
20
+ gem.add_runtime_dependency "jsonable"
21
+
22
+ gem.add_development_dependency "test-unit"
23
+ gem.add_development_dependency "minitest"
24
+ gem.add_development_dependency "rake"
25
+ gem.add_development_dependency "uber_config"
26
+
27
+ end
data/test/contact.rb ADDED
@@ -0,0 +1,6 @@
1
+ class Contact
2
+ include Ocm::Ormable
3
+
4
+ attr_accessor :email, :name, :company, :salesforce_id, :status, :action, :result
5
+ end
6
+
data/test/test_base.rb ADDED
@@ -0,0 +1,18 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+ require 'yaml'
4
+ begin
5
+ require File.join(File.dirname(__FILE__), '../lib/ocm')
6
+ rescue Exception => ex
7
+ puts "Could NOT load gem: " + ex.message
8
+ raise ex
9
+ end
10
+
11
+ class TestBase < Test::Unit::TestCase
12
+
13
+ def setup
14
+ puts 'setup'
15
+
16
+ end
17
+
18
+ end
data/test/test_ocm.rb ADDED
@@ -0,0 +1,55 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+ require 'yaml'
4
+ require_relative 'test_base'
5
+ require_relative 'contact'
6
+
7
+ class TestOcm < TestBase
8
+ def setup
9
+ super
10
+
11
+
12
+ end
13
+
14
+ def test_basics
15
+ orm = Ocm::Orm.new
16
+
17
+ contact = Contact.new(:name=>"t2")
18
+ puts contact.name
19
+ contact.name = "travis"
20
+ puts contact.name
21
+ orm.save(contact)
22
+ p contact
23
+ id = contact.id
24
+ contact2 = orm.find(Contact, id)
25
+ p contact2
26
+ assert_equal id, contact2.id
27
+ assert_equal contact.name, contact2.name
28
+
29
+ list_name = "my_list"
30
+ list = []
31
+ 10.times do |i|
32
+ c = Contact.new
33
+ c.name = "name #{i}"
34
+ orm.save(c)
35
+ list << c
36
+ end
37
+ orm.save_list(list_name, list)
38
+
39
+ list2 = orm.get_list(list_name)
40
+ list2.each_with_index do |c,i|
41
+ assert_equal list[i].id, c.id
42
+ assert_equal list[i].name, c.name
43
+ end
44
+
45
+ c10 = Contact.new(name: "name 10")
46
+ orm.append_to_list(list_name, c10)
47
+ list3 = orm.get_list(list_name)
48
+ p list3
49
+ assert_equal c10.name, list3[10].name
50
+
51
+
52
+ end
53
+
54
+ end
55
+
data/test/tmp.rb ADDED
@@ -0,0 +1,27 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+ require 'yaml'
4
+ require_relative 'test_base'
5
+
6
+ class TestTemp < TestBase
7
+ def setup
8
+ super
9
+
10
+ end
11
+
12
+ def test_500
13
+ puts '500'
14
+ begin
15
+ puts 'in block'
16
+ response = @rest.get("http://rest-test.iron.io/code/500")
17
+ assert false, "shouldn't get here"
18
+ rescue => ex
19
+ p ex
20
+ assert ex.is_a?(Rest::HttpError)
21
+ assert ex.response
22
+ assert ex.response.body
23
+ assert ex.code == 500
24
+ end
25
+ end
26
+ end
27
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-22 00:00:00.000000000 Z
12
+ date: 2012-09-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: iron_cache
@@ -113,7 +113,23 @@ email:
113
113
  executables: []
114
114
  extensions: []
115
115
  extra_rdoc_files: []
116
- files: []
116
+ files:
117
+ - .gitignore
118
+ - Gemfile
119
+ - Gemfile.lock
120
+ - LICENSE
121
+ - README.md
122
+ - Rakefile
123
+ - lib/ocm.rb
124
+ - lib/ocm/idable.rb
125
+ - lib/ocm/iron_cache_orm.rb
126
+ - lib/ocm/ormable.rb
127
+ - lib/ocm/version.rb
128
+ - ocm.gemspec
129
+ - test/contact.rb
130
+ - test/test_base.rb
131
+ - test/test_ocm.rb
132
+ - test/tmp.rb
117
133
  homepage: https://github.com/treeder/
118
134
  licenses: []
119
135
  post_install_message:
@@ -138,4 +154,8 @@ rubygems_version: 1.8.24
138
154
  signing_key:
139
155
  specification_version: 3
140
156
  summary: ORM for IronCache
141
- test_files: []
157
+ test_files:
158
+ - test/contact.rb
159
+ - test/test_base.rb
160
+ - test/test_ocm.rb
161
+ - test/tmp.rb