cql_model 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,7 +25,11 @@ Or install it yourself as:
25
25
  Cql::Base.establish_connection(host: '127.0.0.1')
26
26
 
27
27
  class Person < Cql::Model
28
- # Define your class
28
+ primary_key :id
29
+
30
+ column :first_name, String
31
+ column :last_name, String
32
+ column :dob, Date
29
33
  end
30
34
 
31
35
  ## Contributing
data/cql_model.gemspec CHANGED
@@ -20,8 +20,10 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency 'cql-rb', '~> 1.0.0'
22
22
  spec.add_dependency 'activemodel', '~> 4.0.0.rc1'
23
+ spec.add_dependency 'activesupport', '~> 4.0.0.rc1'
23
24
 
24
25
  spec.add_development_dependency 'minitest', '~> 4.2.0'
26
+ spec.add_development_dependency 'simplecov', '~> 0.7.1'
25
27
  spec.add_development_dependency 'bundler', '~> 1.3'
26
28
  spec.add_development_dependency 'rake'
27
29
  end
@@ -0,0 +1,17 @@
1
+ module Cql::Model::FinderMethods
2
+ extend ::ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def all
6
+ query = "SELECT * FROM #{self.model_name.plural}"
7
+ cql_results = Cql::Base.connection.execute(query)
8
+ Cql::Model::QueryResult.new(cql_results, self)
9
+ end
10
+
11
+ def find(primary_key_value)
12
+ query = "SELECT * FROM #{self.model_name.plural} WHERE #{self.primary_key} = #{primary_key_value}"
13
+ cql_results = Cql::Base.connection.execute(query)
14
+ Cql::Model::QueryResult.new(cql_results, self).first
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ class Cql::Model::QueryResult
2
+ include Enumerable
3
+
4
+ attr_reader :metadata
5
+
6
+ def initialize(results, klass)
7
+ @metadata = results.metadata
8
+ @rows = results.collect {|result| klass.new(result, metadata: @metadata)}
9
+ end
10
+
11
+ def empty?
12
+ @rows.nil? || @rows.empty?
13
+ end
14
+
15
+ def each(&block)
16
+ @rows.each(&block)
17
+ end
18
+ alias_method :each_row, :each
19
+ end
@@ -0,0 +1,22 @@
1
+ module Cql::Model::SchemaMethods
2
+ extend ::ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def columns
6
+ @@columns ||= {}
7
+ end
8
+
9
+ def primary_key(key_name = nil)
10
+ @@primary_key ||= key_name.nil? ? 'id' : key_name.to_s
11
+ end
12
+
13
+ def column(attribute_name, ruby_class, options = {})
14
+ column_name = options[:column_name] || attribute_name
15
+
16
+ columns[column_name.to_sym] = {
17
+ attribute_name: attribute_name.to_sym,
18
+ klass: ruby_class.to_s.constantize
19
+ }.merge(options)
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  module Cql
2
2
  class Model
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
data/lib/cql/model.rb CHANGED
@@ -1,8 +1,14 @@
1
- require 'cql/model/version'
2
- require 'cql/base'
3
-
4
1
  require 'active_model'
5
2
 
3
+ require 'active_support/concern'
4
+ require 'active_support/core_ext'
5
+
6
+ require 'cql/base'
7
+ require 'cql/model/version'
8
+ require 'cql/model/schema_methods'
9
+ require 'cql/model/finder_methods'
10
+ require 'cql/model/query_result'
11
+
6
12
  module Cql
7
13
  class Model
8
14
  extend ActiveModel::Naming
@@ -15,5 +21,26 @@ module Cql
15
21
  include ActiveModel::Serialization
16
22
  #include ActiveModel::Translation
17
23
  include ActiveModel::Validations
24
+
25
+ include Cql::Model::SchemaMethods
26
+ include Cql::Model::FinderMethods
27
+
28
+ def initialize(attributes = {}, options = {})
29
+ self.class.columns.each do |key, config|
30
+ class_eval do
31
+ attr_reader config[:attribute_name]
32
+ attr_writer config[:attribute_name] unless config[:read_only]
33
+ end
34
+ end
35
+
36
+ @metadata = options[:metadata]
37
+
38
+ attributes.each do |key, value|
39
+ attr_name = "@#{key.to_s}".to_sym
40
+ instance_variable_set(attr_name, value)
41
+ end
42
+
43
+ self
44
+ end
18
45
  end
19
46
  end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cql::Model do
4
+ before :all do
5
+ Cql::Base.establish_connection(host: '127.0.0.1')
6
+ Cql::Base.connection.use('cql_model_test')
7
+ end
8
+
9
+ describe 'Finders' do
10
+ describe '#all' do
11
+ it { Person.all.must_be_instance_of Cql::Model::QueryResult }
12
+ it { Person.all.first.must_be_instance_of Person }
13
+ end
14
+
15
+ describe '#find' do
16
+ it { Person.find(1).must_be_instance_of Person }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cql::Model::QueryResult do
4
+ before :all do
5
+ Cql::Base.establish_connection(host: '127.0.0.1')
6
+ Cql::Base.connection.use('cql_model_test')
7
+ end
8
+
9
+ let(:empty_result) do
10
+ query = 'SELECT * FROM events'
11
+ result = Cql::Base.connection.execute(query)
12
+ Cql::Model::QueryResult.new(result, Event)
13
+ end
14
+
15
+ let(:full_result) do
16
+ query = 'SELECT * FROM people'
17
+ result = Cql::Base.connection.execute(query)
18
+ Cql::Model::QueryResult.new(result, Person)
19
+ end
20
+
21
+ describe '#empty?' do
22
+ it { full_result.empty?.must_equal false }
23
+ it { empty_result.must_be_empty }
24
+ end
25
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,34 @@
1
+ require 'bundler/setup'
2
+ require 'simplecov'; SimpleCov.start
3
+
1
4
  require 'minitest/spec'
2
5
  require 'minitest/autorun'
3
6
 
4
7
  require 'cql_model'
8
+
9
+ unless ENV['COVERAGE'] == 'no'
10
+ require 'simplecov'
11
+ SimpleCov.start do
12
+ add_filter "/spec/"
13
+ add_group 'Source', 'lib'
14
+ end
15
+ end
16
+
17
+ require 'support/setup_test_keyspace'
18
+
19
+ setup_cql_test
20
+
21
+ class Event < Cql::Model
22
+ primary_key :id
23
+
24
+ column :location, String
25
+ column :date, Date
26
+ end
27
+
28
+ class Person < Cql::Model
29
+ primary_key :id
30
+
31
+ column :first_name, String
32
+ column :last_name, String
33
+ column :birth_date, Date, column_name: :dob
34
+ end
@@ -0,0 +1,53 @@
1
+ require 'cql'
2
+
3
+ def setup_cql_test
4
+ cql_client = Cql::Client.connect(host: '127.0.0.1')
5
+
6
+ begin
7
+ cql_client.use('cql_model_test')
8
+ rescue Cql::QueryError
9
+ keyspace_definition = <<-KSDEF
10
+ CREATE KEYSPACE cql_model_test
11
+ WITH replication = {
12
+ 'class': 'SimpleStrategy',
13
+ 'replication_factor': 1
14
+ }
15
+ KSDEF
16
+
17
+ cql_client.execute(keyspace_definition)
18
+ cql_client.use('cql_model_test')
19
+ end
20
+
21
+ begin
22
+ table_definition = <<-TABLEDEF
23
+ CREATE TABLE people (
24
+ id INT,
25
+ first_name VARCHAR,
26
+ last_name VARCHAR,
27
+ dob TIMESTAMP,
28
+ PRIMARY KEY (id)
29
+ )
30
+ TABLEDEF
31
+
32
+ cql_client.execute(table_definition)
33
+ rescue Exception
34
+ end
35
+
36
+ cql_client.execute("INSERT INTO people (id,first_name,last_name,dob) VALUES (1,'John','Doe','1942-06-08')")
37
+ cql_client.execute("INSERT INTO people (id,first_name,last_name) VALUES (2,'Jane','Doe')")
38
+ cql_client.execute("INSERT INTO people (id,first_name,last_name) VALUES (3,'Will','Smith')")
39
+
40
+ begin
41
+ table_definition = <<-TABLEDEF
42
+ CREATE TABLE events (
43
+ id INT,
44
+ location VARCHAR,
45
+ date TIMESTAMP,
46
+ PRIMARY KEY (id)
47
+ )
48
+ TABLEDEF
49
+
50
+ cql_client.execute(table_definition)
51
+ rescue Exception
52
+ end
53
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cql_model
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: 2013-06-22 00:00:00.000000000 Z
12
+ date: 2013-06-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cql-rb
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
45
  version: 4.0.0.rc1
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 4.0.0.rc1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0.rc1
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: minitest
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -59,6 +75,22 @@ dependencies:
59
75
  - - ~>
60
76
  - !ruby/object:Gem::Version
61
77
  version: 4.2.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 0.7.1
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 0.7.1
62
94
  - !ruby/object:Gem::Dependency
63
95
  name: bundler
64
96
  requirement: !ruby/object:Gem::Requirement
@@ -106,11 +138,17 @@ files:
106
138
  - cql_model.gemspec
107
139
  - lib/cql/base.rb
108
140
  - lib/cql/model.rb
141
+ - lib/cql/model/finder_methods.rb
142
+ - lib/cql/model/query_result.rb
143
+ - lib/cql/model/schema_methods.rb
109
144
  - lib/cql/model/version.rb
110
145
  - lib/cql_model.rb
111
146
  - spec/active_model_lint_spec.rb
112
147
  - spec/cql/base_spec.rb
148
+ - spec/cql/model/finders_spec.rb
149
+ - spec/cql/model/query_result_spec.rb
113
150
  - spec/spec_helper.rb
151
+ - spec/support/setup_test_keyspace.rb
114
152
  homepage: https://github.com/plainprogrammer/cql_model
115
153
  licenses:
116
154
  - MIT
@@ -126,7 +164,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
164
  version: '0'
127
165
  segments:
128
166
  - 0
129
- hash: -838785113870071967
167
+ hash: 2224793988251735129
130
168
  required_rubygems_version: !ruby/object:Gem::Requirement
131
169
  none: false
132
170
  requirements:
@@ -135,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
173
  version: '0'
136
174
  segments:
137
175
  - 0
138
- hash: -838785113870071967
176
+ hash: 2224793988251735129
139
177
  requirements: []
140
178
  rubyforge_project:
141
179
  rubygems_version: 1.8.23
@@ -145,4 +183,7 @@ summary: ActiveModel implementation on cql-rb
145
183
  test_files:
146
184
  - spec/active_model_lint_spec.rb
147
185
  - spec/cql/base_spec.rb
186
+ - spec/cql/model/finders_spec.rb
187
+ - spec/cql/model/query_result_spec.rb
148
188
  - spec/spec_helper.rb
189
+ - spec/support/setup_test_keyspace.rb