riak-model 0.0.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -0
- data/CHANGELOG +2 -0
- data/Gemfile +10 -0
- data/LICENSE +20 -0
- data/README.textile +19 -0
- data/TODO.txt +2 -0
- data/deps.rip +5 -0
- data/examples/model.rb +0 -0
- data/gems/cache/code-cleaner-0.8.1.gem +0 -0
- data/gems/cache/nake-0.0.8.gem +0 -0
- data/gems/cache/rack-1.1.0.gem +0 -0
- data/gems/cache/riak-0.0.1.pre.gem +0 -0
- data/gems/cache/rspec-1.3.0.gem +0 -0
- data/gems/cache/term-ansicolor-1.0.4.gem +0 -0
- data/gems/cache/typhoeus-0.1.13.gem +0 -0
- data/lib/riak/link.rb +18 -0
- data/lib/riak/model.rb +175 -0
- data/lib/riak/model/version.rb +7 -0
- data/lib/riak/property.rb +23 -0
- data/riak-model.gemspec +39 -0
- data/riak-model.pre.gemspec +8 -0
- data/spec/riak/model_spec.rb +10 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +11 -0
- data/tasks.rb +37 -0
- metadata +87 -0
data/.gitignore
ADDED
data/CHANGELOG
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Jakub Stastny aka Botanicus
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
h1. About
|
2
|
+
|
3
|
+
Simple ODM for Riak. It provides simple validations, but don't expect anything fancy, point of this ODM is to be light-weight and fast. If you need something more robust, use "do_riak":http://github.com/botanicus/do_riak.
|
4
|
+
|
5
|
+
h1. Installation
|
6
|
+
|
7
|
+
* Prerelease: @gem install riak-model --prerelease@
|
8
|
+
* Stable version: @gem install riak-model@
|
9
|
+
|
10
|
+
h1. Usage
|
11
|
+
|
12
|
+
# TODO: write how to use your library
|
13
|
+
|
14
|
+
h1. Links
|
15
|
+
|
16
|
+
* "Source Code":http://github.com/botanicus/riak-model
|
17
|
+
* "Wiki":http://wiki.github.com/botanicus/riak-model
|
18
|
+
* "API Docs":http://rdoc.info/projects/botanicus/riak-model
|
19
|
+
* "Bug reporting":http://github.com/botanicus/riak-model/issues
|
data/TODO.txt
ADDED
data/deps.rip
ADDED
data/examples/model.rb
ADDED
File without changes
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/lib/riak/link.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Riak
|
4
|
+
class Link
|
5
|
+
attr_reader :object
|
6
|
+
def initialize(object)
|
7
|
+
@object = object
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_a
|
11
|
+
[object.bucket, object.key, object.key] # NOTE: the last one is probably name
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_json
|
15
|
+
JSON.generate(self.to_a)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/riak/model.rb
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "riak/interfaces/jiak"
|
4
|
+
|
5
|
+
module Riak
|
6
|
+
class Model
|
7
|
+
# Riak::Model.not_found = Rango::Exceptions::NotFound
|
8
|
+
# # and then you can use
|
9
|
+
# def show(id)
|
10
|
+
# render "post.html", post: Post.get!(id)
|
11
|
+
# end
|
12
|
+
# # rather than
|
13
|
+
# def show(id)
|
14
|
+
# post = Post.get(id)
|
15
|
+
# raise NotFound if post.nil?
|
16
|
+
# render "post.html", post: post
|
17
|
+
# end
|
18
|
+
def self.not_found
|
19
|
+
@@not_found ||= Class.new(StandardError)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.not_found=(klass)
|
23
|
+
@@not_found = klass
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.config
|
27
|
+
@@config ||= ["127.0.0.1", 8098]
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.client
|
31
|
+
@@client ||= Client.new(*config)
|
32
|
+
end
|
33
|
+
|
34
|
+
attr_writer :read_mask
|
35
|
+
def self.read_mask
|
36
|
+
@read_mask ||= "*"
|
37
|
+
end
|
38
|
+
|
39
|
+
attr_writer :write_mask
|
40
|
+
def self.write_mask
|
41
|
+
@write_mask ||= "*"
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.set_bucket_schema
|
45
|
+
options = {allowed_fields: allowed_fields, read_mask: read_mask, write_mask: write_mask}
|
46
|
+
client.set_bucket_schema(bucket, options)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.required_fields
|
50
|
+
self.properties.reduce(Array.new) do |array, pair|
|
51
|
+
array.push(pair.first) if pair.last.required?
|
52
|
+
array
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.allowed_fields
|
57
|
+
self.properties.keys
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.properties
|
61
|
+
@@properties ||= Hash.new
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.property(name, type, options = Hash.new)
|
65
|
+
attr_accessor name
|
66
|
+
self.properties[name] = Property.new(type, options)
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.schema(&block)
|
70
|
+
block.call
|
71
|
+
self.set_bucket_schema
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.list_bucket
|
75
|
+
symbolize_keys = lambda do |hash|
|
76
|
+
hash.reduce(Hash.new) do |hash, pair|
|
77
|
+
if pair.last.is_a?(Hash)
|
78
|
+
hash.merge(pair.first.to_sym => symbolize_keys.call(pair.last))
|
79
|
+
else
|
80
|
+
hash.merge(pair.first.to_sym => pair.last)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
symbolize_keys.call(client.list_bucket(bucket))
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.bucket
|
89
|
+
self.name.split("::").last.downcase # TODO: snake_case
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.create(properties = Hash.new)
|
93
|
+
self.new(properties).save
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.delete(key, rw = nil)
|
97
|
+
client.delete(bucket, key, rw)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Post.get(id) # blocking
|
101
|
+
# Post.get(id) { |post| render "post.html", post: post }
|
102
|
+
def self.get(key, &block)
|
103
|
+
unless block.nil? # async
|
104
|
+
raise NotImplementedError
|
105
|
+
else
|
106
|
+
properties = client.get(self.bucket, key)
|
107
|
+
self.new(properties[:object])
|
108
|
+
end
|
109
|
+
rescue # TODO: specify a class!
|
110
|
+
end
|
111
|
+
|
112
|
+
def self.get!(key)
|
113
|
+
self.get(key) || raise(NotFound, "Object with key #{key} doesn't exist!")
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.all
|
117
|
+
data = client.bucket(bucket)
|
118
|
+
data[:keys].map { |key| self.get(key) }
|
119
|
+
end
|
120
|
+
|
121
|
+
def initialize(properties = Hash.new)
|
122
|
+
properties.each do |key, value|
|
123
|
+
setter = "#{key}="
|
124
|
+
if self.respond_to?(setter)
|
125
|
+
self.send(setter, value)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def to_riak
|
131
|
+
{bucket: self.class.bucket, key: key, object: properties, links: links}
|
132
|
+
end
|
133
|
+
|
134
|
+
def properties
|
135
|
+
self.class.properties.keys.inject(Hash.new) do |hash, property|
|
136
|
+
hash.merge(property => self.send(property))
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def links
|
141
|
+
@links ||= begin
|
142
|
+
links = Array.new
|
143
|
+
def links.<<(object)
|
144
|
+
self.insert(self.length, Link.new(object))
|
145
|
+
end
|
146
|
+
|
147
|
+
def links.push(object)
|
148
|
+
self.insert(self.length, Link.new(object))
|
149
|
+
end
|
150
|
+
|
151
|
+
def links.unshift(object)
|
152
|
+
self.insert(0, Link.new(object))
|
153
|
+
end
|
154
|
+
|
155
|
+
links
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def save
|
160
|
+
self.class.client.store(self.to_riak)
|
161
|
+
end
|
162
|
+
|
163
|
+
def delete(rw = nil)
|
164
|
+
self.class.delete(self.key, rw)
|
165
|
+
end
|
166
|
+
|
167
|
+
# If the key is nil Riak will set the ID for you.
|
168
|
+
# Nevertheless you can rewrite it:
|
169
|
+
# def key
|
170
|
+
# @key ||= self.to_slug
|
171
|
+
# end
|
172
|
+
def key
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Riak
|
4
|
+
class Property
|
5
|
+
attr_reader :type, :options
|
6
|
+
def initialize(type, options)
|
7
|
+
@type, @options = type, options
|
8
|
+
end
|
9
|
+
|
10
|
+
def value
|
11
|
+
end
|
12
|
+
|
13
|
+
def original_value
|
14
|
+
end
|
15
|
+
|
16
|
+
def dirty?
|
17
|
+
end
|
18
|
+
|
19
|
+
def required?
|
20
|
+
options[:nullable] == false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/riak-model.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/usr/bin/env gem build
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
# Run ./riak-model.gemspec or gem build riak-model.gemspec
|
5
|
+
# NOTE: we can't use require_relative because when we run gem build, it use eval for executing this file
|
6
|
+
require File.join(File.dirname(__FILE__), "lib/riak/model/version")
|
7
|
+
|
8
|
+
Gem::Specification.new do |s|
|
9
|
+
s.name = "riak-model"
|
10
|
+
s.version = Riak::Model::VERSION
|
11
|
+
s.authors = ["Jakub Stastny aka Botanicus"]
|
12
|
+
s.homepage = "http://github.com/botanicus/riak-model"
|
13
|
+
s.summary = "Simple ODM for Riak."
|
14
|
+
s.description = "" # TODO: long description
|
15
|
+
s.cert_chain = nil
|
16
|
+
s.email = "knava.bestvinensis@gmail.com"
|
17
|
+
s.has_rdoc = true
|
18
|
+
|
19
|
+
# files
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
# Ruby version
|
24
|
+
s.required_ruby_version = ::Gem::Requirement.new(">= 1.9")
|
25
|
+
|
26
|
+
# runtime dependencies
|
27
|
+
s.add_dependency "riak"
|
28
|
+
|
29
|
+
begin
|
30
|
+
require "changelog"
|
31
|
+
rescue LoadError
|
32
|
+
warn "You have to have changelog gem installed for post install message"
|
33
|
+
else
|
34
|
+
s.post_install_message = CHANGELOG.new.version_changes
|
35
|
+
end
|
36
|
+
|
37
|
+
# RubyForge
|
38
|
+
s.rubyforge_project = "riak-model"
|
39
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
#!/usr/bin/env gem build
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
# You might think this is a terrible mess and guess what, you're
|
5
|
+
# right mate! However say thanks to authors of RubyGems, not me.
|
6
|
+
eval(File.read("riak-model.gemspec")).tap do |specification|
|
7
|
+
specification.version = "#{specification.version}.pre"
|
8
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# constants
|
4
|
+
SPEC_ROOT = File.dirname(__FILE__)
|
5
|
+
ROOT = File.expand_path(File.join(SPEC_ROOT, ".."))
|
6
|
+
LIBDIR = File.join(ROOT, "lib")
|
7
|
+
|
8
|
+
# load paths
|
9
|
+
$:.unshift(LIBDIR)
|
10
|
+
|
11
|
+
require "spec" # so you can run ruby spec/riak-model/whatever_spec.rb
|
data/tasks.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!./script/nake
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
begin
|
5
|
+
require_relative "gems/environment.rb"
|
6
|
+
rescue LoadError
|
7
|
+
abort "You have to install bundler and run gem bundle first!"
|
8
|
+
end
|
9
|
+
|
10
|
+
ENV["PATH"] = "script:#{ENV["PATH"]}"
|
11
|
+
|
12
|
+
require "nake/tasks/gem"
|
13
|
+
require "nake/tasks/spec"
|
14
|
+
require "nake/tasks/release"
|
15
|
+
|
16
|
+
require_relative "lib/riak/model/version"
|
17
|
+
|
18
|
+
begin
|
19
|
+
load "code-cleaner.nake"
|
20
|
+
Nake::Task["hooks:whitespace:install"].tap do |task|
|
21
|
+
task.config[:path] = "script"
|
22
|
+
task.config[:encoding] = "utf-8"
|
23
|
+
task.config[:whitelist] = '(bin/[^/]+|.+\.(rb|rake|nake|thor|task))$'
|
24
|
+
end
|
25
|
+
rescue LoadError
|
26
|
+
warn "If you want to contribute to Riak::Model, please install code-cleaner and then run ./tasks.rb hooks:whitespace:install to get Git pre-commit hook for removing trailing whitespace."
|
27
|
+
end
|
28
|
+
|
29
|
+
# Setup encoding, so all the operations
|
30
|
+
# with strings from another files will work
|
31
|
+
Encoding.default_internal = "utf-8"
|
32
|
+
Encoding.default_external = "utf-8"
|
33
|
+
|
34
|
+
Task[:build].config[:gemspec] = "riak-model.gemspec"
|
35
|
+
Task[:prerelease].config[:gemspec] = "riak-model.pre.gemspec"
|
36
|
+
Task[:release].config[:name] = "riak-model"
|
37
|
+
Task[:release].config[:version] = Riak::Model::VERSION
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: riak-model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.pre
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jakub Stastny aka Botanicus
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
date: 2010-01-12 00:00:00 +01:00
|
12
|
+
default_executable:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: riak
|
16
|
+
type: :runtime
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description: ""
|
25
|
+
email: knava.bestvinensis@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- CHANGELOG
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE
|
37
|
+
- README.textile
|
38
|
+
- TODO.txt
|
39
|
+
- deps.rip
|
40
|
+
- examples/model.rb
|
41
|
+
- gems/cache/code-cleaner-0.8.1.gem
|
42
|
+
- gems/cache/nake-0.0.8.gem
|
43
|
+
- gems/cache/rack-1.1.0.gem
|
44
|
+
- gems/cache/riak-0.0.1.pre.gem
|
45
|
+
- gems/cache/rspec-1.3.0.gem
|
46
|
+
- gems/cache/term-ansicolor-1.0.4.gem
|
47
|
+
- gems/cache/typhoeus-0.1.13.gem
|
48
|
+
- lib/riak/link.rb
|
49
|
+
- lib/riak/model.rb
|
50
|
+
- lib/riak/model/version.rb
|
51
|
+
- lib/riak/property.rb
|
52
|
+
- riak-model.gemspec
|
53
|
+
- riak-model.pre.gemspec
|
54
|
+
- spec/riak/model_spec.rb
|
55
|
+
- spec/spec.opts
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
- tasks.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://github.com/botanicus/riak-model
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message: "[\e[32mVersion 0.0.1\e[0m] Initial import\n"
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "1.9"
|
72
|
+
version:
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.3.1
|
78
|
+
version:
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project: riak-model
|
82
|
+
rubygems_version: 1.3.5
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Simple ODM for Riak.
|
86
|
+
test_files: []
|
87
|
+
|