curator 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/curator/configuration.rb +3 -0
- data/lib/curator/migrator.rb +1 -1
- data/lib/curator/railtie.rb +6 -4
- data/lib/curator/repository.rb +17 -34
- data/lib/curator/resettable_riak/configuration.rb +7 -0
- data/lib/curator/resettable_riak/data_store.rb +50 -0
- data/lib/curator/riak/configuration.rb +11 -0
- data/lib/curator/riak/data_store.rb +54 -52
- data/lib/curator.rb +21 -5
- metadata +15 -11
data/lib/curator/migrator.rb
CHANGED
@@ -16,7 +16,7 @@ module Curator
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def _all_migrations
|
19
|
-
files = Dir.glob("#{File.join(Curator.migrations_path, @collection_name)}/*.rb")
|
19
|
+
files = Dir.glob("#{File.join(Curator.config.migrations_path, @collection_name)}/*.rb")
|
20
20
|
|
21
21
|
files.map do |file|
|
22
22
|
load file
|
data/lib/curator/railtie.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
module Curator
|
2
2
|
class Railtie < Rails::Railtie
|
3
3
|
initializer "railtie.configure_rails_initialization" do |app|
|
4
|
-
Curator.
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
Curator.configure(:riak) do |config|
|
5
|
+
config.bucket_prefix = app.class.name.split("::").first.underscore
|
6
|
+
config.environment = Rails.env
|
7
|
+
config.migrations_path = Rails.root.join('db', 'migrate')
|
8
|
+
config.riak_config_file = Rails.root.join('config', 'riak.yml')
|
9
|
+
end
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
data/lib/curator/repository.rb
CHANGED
@@ -13,7 +13,7 @@ module Curator
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def data_store
|
16
|
-
@data_store ||=
|
16
|
+
@data_store ||= Curator.data_store
|
17
17
|
end
|
18
18
|
|
19
19
|
def data_store=(store)
|
@@ -24,10 +24,6 @@ module Curator
|
|
24
24
|
data_store.delete(collection_name, object.id)
|
25
25
|
end
|
26
26
|
|
27
|
-
def encrypted_entity
|
28
|
-
@encrypted_entity = true
|
29
|
-
end
|
30
|
-
|
31
27
|
def find_by_created_at(start_time, end_time)
|
32
28
|
_find_by_index(collection_name, :created_at, _format_time_for_index(start_time).._format_time_for_index(end_time))
|
33
29
|
end
|
@@ -36,6 +32,10 @@ module Curator
|
|
36
32
|
_find_by_index(collection_name, :updated_at, _format_time_for_index(start_time).._format_time_for_index(end_time))
|
37
33
|
end
|
38
34
|
|
35
|
+
def find_by_version(version)
|
36
|
+
_find_by_index(collection_name, :version, version)
|
37
|
+
end
|
38
|
+
|
39
39
|
def find_by_id(id)
|
40
40
|
if hash = data_store.find_by_key(collection_name, id)
|
41
41
|
_deserialize(hash[:key], hash[:data])
|
@@ -59,6 +59,11 @@ module Curator
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def save(object)
|
62
|
+
_update_timestamps(object)
|
63
|
+
save_without_timestamps(object)
|
64
|
+
end
|
65
|
+
|
66
|
+
def save_without_timestamps(object)
|
62
67
|
hash = {
|
63
68
|
:collection_name => collection_name,
|
64
69
|
:value => _serialize(object),
|
@@ -110,22 +115,6 @@ module Curator
|
|
110
115
|
object
|
111
116
|
end
|
112
117
|
|
113
|
-
def _encrypted_attributes(object, attributes)
|
114
|
-
return attributes unless _encrypted_entity?
|
115
|
-
|
116
|
-
encryption_key = EncryptionKeyRepository.find_active
|
117
|
-
plaintext = attributes.to_json
|
118
|
-
ciphertext = encryption_key.encrypt(plaintext)
|
119
|
-
{
|
120
|
-
:encryption_key_id => encryption_key.id,
|
121
|
-
:encrypted_data => Base64.encode64(ciphertext)
|
122
|
-
}
|
123
|
-
end
|
124
|
-
|
125
|
-
def _encrypted_entity?
|
126
|
-
@encrypted_entity == true
|
127
|
-
end
|
128
|
-
|
129
118
|
def _format_time_for_index(time)
|
130
119
|
time.to_json.gsub('"', '')
|
131
120
|
end
|
@@ -138,25 +127,19 @@ module Curator
|
|
138
127
|
index_values = _indexed_fields.map { |field| [field, object.send(field)] }
|
139
128
|
index_values += [
|
140
129
|
[:created_at, _format_time_for_index(object.send(:created_at))],
|
141
|
-
[:updated_at, _format_time_for_index(object.send(:updated_at))]
|
130
|
+
[:updated_at, _format_time_for_index(object.send(:updated_at))],
|
131
|
+
[:version, object.version]
|
142
132
|
]
|
143
133
|
Hash[index_values]
|
144
134
|
end
|
145
135
|
|
146
136
|
def _serialize(object)
|
147
|
-
|
148
|
-
|
149
|
-
timestamp = Time.now.utc
|
150
|
-
|
151
|
-
updated_at = timestamp
|
152
|
-
created_at = object.created_at || timestamp
|
153
|
-
|
154
|
-
object.created_at = created_at
|
155
|
-
object.updated_at = updated_at
|
156
|
-
attributes[:created_at] = created_at
|
157
|
-
attributes[:updated_at] = updated_at
|
137
|
+
serialize(object).reject { |key, val| val.nil? }
|
138
|
+
end
|
158
139
|
|
159
|
-
|
140
|
+
def _update_timestamps(object)
|
141
|
+
object.updated_at = Time.now.utc
|
142
|
+
object.created_at ||= object.updated_at
|
160
143
|
end
|
161
144
|
end
|
162
145
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'active_support/core_ext/kernel/reporting'
|
2
|
+
|
3
|
+
module Curator
|
4
|
+
module ResettableRiak
|
5
|
+
class DataStore < Riak::DataStore
|
6
|
+
def self.bucket_prefix
|
7
|
+
job = "#{ENV['JOB_NAME'].gsub(/[^[:alnum:]]/, '_')}" if ENV['JOB_NAME'].present?
|
8
|
+
[Curator.config.bucket_prefix, job, Curator.config.environment].compact.join(':')
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.exclude_from_reset
|
12
|
+
@exclude_from_reset = true
|
13
|
+
yield
|
14
|
+
@exclude_from_reset = false
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.remove_all_keys
|
18
|
+
silence_warnings do
|
19
|
+
buckets = client.buckets.select { |bucket| bucket.name.start_with?(DataStore.bucket_prefix) }
|
20
|
+
buckets.each do |bucket|
|
21
|
+
bucket.keys do |keys|
|
22
|
+
keys.each { |key| bucket.delete(key) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.reset!
|
29
|
+
@bucket_names ||= {}
|
30
|
+
deletable_buckets = @bucket_names.each do |bucket_name, keys|
|
31
|
+
bucket = _bucket(bucket_name)
|
32
|
+
keys.each {|key| bucket.delete(key)}
|
33
|
+
end
|
34
|
+
@bucket_names = {}
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.save(options)
|
38
|
+
result = super
|
39
|
+
|
40
|
+
unless @exclude_from_reset
|
41
|
+
@bucket_names ||= {}
|
42
|
+
@bucket_names[options[:collection_name]] ||= []
|
43
|
+
@bucket_names[options[:collection_name]] << result.key
|
44
|
+
end
|
45
|
+
|
46
|
+
result
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -1,71 +1,73 @@
|
|
1
1
|
require 'riak'
|
2
2
|
require 'yaml'
|
3
3
|
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
module Curator
|
5
|
+
module Riak
|
6
|
+
class DataStore
|
7
|
+
def self.client
|
8
|
+
return @client if @client
|
9
|
+
yml_config = YAML.load(File.read(Curator.config.riak_config_file))[Curator.config.environment]
|
10
|
+
@client = ::Riak::Client.new(yml_config)
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
def self.delete(bucket_name, key)
|
14
|
+
bucket = _bucket(bucket_name)
|
15
|
+
object = bucket.get(key)
|
16
|
+
object.delete
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
def self.ping
|
20
|
+
client.ping
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
def self.save(options)
|
24
|
+
bucket = _bucket(options[:collection_name])
|
25
|
+
object = ::Riak::RObject.new(bucket, options[:key])
|
26
|
+
object.content_type = "application/json"
|
27
|
+
object.data = options[:value]
|
28
|
+
options.fetch(:index, {}).each do |index_name, index_value|
|
29
|
+
object.indexes["#{index_name}_bin"] << index_value
|
30
|
+
end
|
31
|
+
object.store
|
29
32
|
end
|
30
|
-
object.store
|
31
|
-
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
34
|
+
def self.find_by_key(bucket_name, key)
|
35
|
+
bucket = _bucket(bucket_name)
|
36
|
+
begin
|
37
|
+
object = bucket.get(key)
|
38
|
+
{ :key => object.key, :data => object.data } unless object.data.empty?
|
39
|
+
rescue ::Riak::HTTPFailedRequest => failed_request
|
40
|
+
raise failed_request unless failed_request.not_found?
|
41
|
+
end
|
40
42
|
end
|
41
|
-
end
|
42
43
|
|
43
|
-
|
44
|
-
|
44
|
+
def self.find_by_index(bucket_name, index_name, query)
|
45
|
+
return [] if query.nil?
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
bucket = _bucket(bucket_name)
|
48
|
+
begin
|
49
|
+
keys = _find_key_by_index(bucket, index_name.to_s, query)
|
50
|
+
keys.map { |key| find_by_key(bucket_name, key) }
|
51
|
+
rescue ::Riak::HTTPFailedRequest => failed_request
|
52
|
+
raise failed_request unless failed_request.not_found?
|
53
|
+
end
|
52
54
|
end
|
53
|
-
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
56
|
+
def self._bucket(name)
|
57
|
+
client.bucket(_bucket_name(name))
|
58
|
+
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
def self._bucket_name(name)
|
61
|
+
bucket_prefix + ":" + name
|
62
|
+
end
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
64
|
+
def self.bucket_prefix
|
65
|
+
"#{Curator.config.bucket_prefix}:#{Curator.config.environment}"
|
66
|
+
end
|
66
67
|
|
67
|
-
|
68
|
-
|
68
|
+
def self._find_key_by_index(bucket, index_name, query)
|
69
|
+
bucket.get_index("#{index_name}_bin", query)
|
70
|
+
end
|
69
71
|
end
|
70
72
|
end
|
71
73
|
end
|
data/lib/curator.rb
CHANGED
@@ -4,16 +4,32 @@ require 'curator/migration'
|
|
4
4
|
require 'curator/migrator'
|
5
5
|
require 'curator/model'
|
6
6
|
require 'curator/repository'
|
7
|
+
require 'curator/configuration'
|
8
|
+
require 'curator/riak/configuration'
|
7
9
|
require 'curator/riak/data_store'
|
10
|
+
require 'curator/resettable_riak/configuration'
|
11
|
+
require 'curator/resettable_riak/data_store'
|
8
12
|
require 'curator/railtie' if defined?(Rails)
|
9
13
|
|
10
14
|
module Curator
|
11
15
|
class << self
|
12
|
-
|
16
|
+
attr_reader :config
|
13
17
|
end
|
14
18
|
|
15
|
-
self.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
+
def self.configure(data_store, &block)
|
20
|
+
path = "curator/#{data_store.to_s}/configuration"
|
21
|
+
@config = path.camelize.constantize.new
|
22
|
+
yield(@config) if block_given?
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.data_store
|
26
|
+
config.data_store
|
27
|
+
end
|
28
|
+
|
29
|
+
self.configure(:riak) do |config|
|
30
|
+
config.environment = 'development'
|
31
|
+
config.migrations_path = File.expand_path(File.dirname(__FILE__) + "/../db/migrate")
|
32
|
+
config.bucket_prefix = 'curator'
|
33
|
+
config.riak_config_file = File.expand_path(File.dirname(__FILE__) + "/../config/riak.yml")
|
34
|
+
end
|
19
35
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: curator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70336976526480 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70336976526480
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activemodel
|
27
|
-
requirement: &
|
27
|
+
requirement: &70336976525860 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 3.0.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70336976525860
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: json
|
38
|
-
requirement: &
|
38
|
+
requirement: &70336976525440 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70336976525440
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: riak-client
|
49
|
-
requirement: &
|
49
|
+
requirement: &70336976524760 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,18 +54,22 @@ dependencies:
|
|
54
54
|
version: 1.0.0
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70336976524760
|
58
58
|
description: Model and repository framework
|
59
59
|
email: code@getbraintree.com
|
60
60
|
executables: []
|
61
61
|
extensions: []
|
62
62
|
extra_rdoc_files: []
|
63
63
|
files:
|
64
|
+
- lib/curator/configuration.rb
|
64
65
|
- lib/curator/migration.rb
|
65
66
|
- lib/curator/migrator.rb
|
66
67
|
- lib/curator/model.rb
|
67
68
|
- lib/curator/railtie.rb
|
68
69
|
- lib/curator/repository.rb
|
70
|
+
- lib/curator/resettable_riak/configuration.rb
|
71
|
+
- lib/curator/resettable_riak/data_store.rb
|
72
|
+
- lib/curator/riak/configuration.rb
|
69
73
|
- lib/curator/riak/data_store.rb
|
70
74
|
- lib/curator.rb
|
71
75
|
homepage: http://github.com/braintree/curator
|
@@ -88,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
92
|
version: '0'
|
89
93
|
requirements: []
|
90
94
|
rubyforge_project:
|
91
|
-
rubygems_version: 1.8.
|
95
|
+
rubygems_version: 1.8.11
|
92
96
|
signing_key:
|
93
97
|
specification_version: 3
|
94
98
|
summary: Model and repository framework
|