sandra 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +13 -0
  3. data/lib/sandra.rb +95 -0
  4. metadata +88 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 [name of plugin creator]
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 ADDED
@@ -0,0 +1,13 @@
1
+ Sandra
2
+ ======
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2011 [name of plugin creator], released under the MIT license
@@ -0,0 +1,95 @@
1
+ require 'active_model'
2
+ require 'cassandra'
3
+ require 'sandra/key_validator'
4
+
5
+ module Sandra
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ base.extend(ActiveModel::Naming)
9
+ base.class_eval do
10
+ include ActiveModel::Validations
11
+ include ActiveModel::Conversion
12
+ attr_accessor :attributes, :new_record
13
+ def initialize(attrs = {})
14
+ @attributes = attrs.stringify_keys
15
+ @new_record = true
16
+ end
17
+ end
18
+ end
19
+
20
+ def new_record?
21
+ new_record
22
+ end
23
+
24
+ def persisted?
25
+ false
26
+ end
27
+
28
+ def save
29
+ if valid?
30
+ attrs = attributes.dup
31
+ key = attrs.delete(self.class.key)
32
+ self.class.insert(key, attrs)
33
+ new_record = false
34
+ true
35
+ else
36
+ false
37
+ end
38
+ end
39
+
40
+ module ClassMethods
41
+ def column(col_name)
42
+ define_method col_name do
43
+ attr = col_name.to_s
44
+ attributes[attr]
45
+ end
46
+ define_method "#{col_name}=" do |val|
47
+ attr = col_name.to_s
48
+ attributes[attr] = val
49
+ end
50
+ end
51
+
52
+ def establish_connection(options = {})
53
+ connection_options = YAML.load_file("#{::Rails.root.to_s}/config/sandra.yml")[Rails.env].merge(options)
54
+ keyspace = connection_options["keyspace"]
55
+ host = "#{connection_options["host"]}:#{connection_options["port"]}"
56
+ @connection = Cassandra.new(keyspace, host)
57
+ end
58
+
59
+ def connection
60
+ @connection || establish_connection
61
+ end
62
+
63
+ def get(key)
64
+ hash = connection.get("User", key)
65
+ unless hash.empty?
66
+ obj = self.new(hash)
67
+ obj.username = key
68
+ obj.new_record = false
69
+ obj
70
+ else
71
+ nil
72
+ end
73
+ end
74
+
75
+ def insert(key, columns = {})
76
+ connection.insert("User", key, columns)
77
+ end
78
+
79
+ def key_attribute(name)
80
+ @key = name
81
+ validates name, :presence => true, :key => true
82
+ column name
83
+ end
84
+
85
+ def key
86
+ @key
87
+ end
88
+
89
+ def create(columns = {})
90
+ obj = self.new(columns)
91
+ obj.save
92
+ obj
93
+ end
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sandra
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Charles Max Wood
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-08-08 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: cassandra
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: activemodel
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id003
48
+ description: Provides an Object Relational interface to Cassandra
49
+ email: chuck@teachmetocode.com
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files: []
55
+
56
+ files:
57
+ - README
58
+ - MIT-LICENSE
59
+ - lib/sandra.rb
60
+ homepage: http://teachmetocode.com/
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options: []
65
+
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.6
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: ORM for Cassandra in Ruby
87
+ test_files: []
88
+