cql_model 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,16 +3,14 @@ module Cql::Model::FinderMethods
3
3
 
4
4
  module ClassMethods
5
5
  def all
6
- query = "SELECT * FROM #{self.model_name.plural}"
6
+ query = "SELECT * FROM #{table_name}"
7
7
  execute(query)
8
8
  end
9
9
 
10
10
  def find(*args)
11
11
  value = args.to_a.flatten.join(',')
12
- key = self.primary_key
13
- table = self.model_name.plural
14
12
 
15
- query = "SELECT * FROM #{table} WHERE #{key} IN (#{value})"
13
+ query = "SELECT * FROM #{table_name} WHERE #{primary_key} IN (#{value})"
16
14
 
17
15
  if args[0].is_a?(Array) || args.size > 1
18
16
  execute(query).to_a
@@ -22,13 +20,11 @@ module Cql::Model::FinderMethods
22
20
  end
23
21
 
24
22
  def find_by(hash)
25
- table = self.model_name.plural
26
-
27
23
  clause = "WHERE "
28
24
  clause_pieces = hash.collect {|key,value| "#{key.to_s} = '#{value}'"}
29
25
  clause << clause_pieces.join(' AND ')
30
26
 
31
- query = "SELECT * FROM #{table} #{clause} ALLOW FILTERING"
27
+ query = "SELECT * FROM #{table_name} #{clause} ALLOW FILTERING"
32
28
 
33
29
  execute(query).to_a
34
30
  end
@@ -12,16 +12,26 @@ module Cql::Model::PersistenceMethods
12
12
 
13
13
  updates = updates.join(', ')
14
14
 
15
- table = self.class.model_name.plural
16
- primary_key = self.class.primary_key.to_s
17
-
18
- query = "UPDATE #{table} SET #{updates} WHERE #{primary_key} = #{quoted_primary_value}"
15
+ query = "UPDATE #{table_name} SET #{updates} WHERE #{primary_key} = #{quoted_primary_value}"
19
16
  Cql::Base.connection.execute(query)
20
17
 
21
18
  @persisted = true
22
19
  self
23
20
  end
24
21
 
22
+ def deleted?
23
+ @deleted
24
+ end
25
+
26
+ def delete
27
+ query = "DELETE FROM #{table_name} WHERE #{primary_key} = #{quoted_primary_value}"
28
+ Cql::Base.connection.execute(query)
29
+
30
+ @deleted = true
31
+ @persisted = false
32
+ self
33
+ end
34
+
25
35
  module ClassMethods
26
36
 
27
37
  end
@@ -1,7 +1,19 @@
1
1
  module Cql::Model::SchemaMethods
2
2
  extend ::ActiveSupport::Concern
3
3
 
4
+ def table_name
5
+ self.class.table_name
6
+ end
7
+
8
+ def primary_key
9
+ self.class.primary_key
10
+ end
11
+
4
12
  module ClassMethods
13
+ def table_name
14
+ @table_name ||= self.model_name.plural
15
+ end
16
+
5
17
  def columns
6
18
  @columns ||= {}
7
19
  end
@@ -1,5 +1,5 @@
1
1
  module Cql
2
2
  class Model
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
5
5
  end
data/lib/cql/model.rb CHANGED
@@ -40,6 +40,7 @@ module Cql
40
40
  @metadata = options[:metadata]
41
41
  @primary_value = attributes[self.class.primary_key.to_sym]
42
42
  @persisted = false
43
+ @deleted = false
43
44
 
44
45
  attributes.each do |key, value|
45
46
  attr_name = "@#{key.to_s}".to_sym
@@ -23,11 +23,24 @@ describe 'Cql::Model Finders' do
23
23
  end
24
24
  end
25
25
 
26
- describe '#update_attribute' do
26
+ describe '#delete' do
27
+ before :each do
28
+ @person.save
29
+ end
27
30
 
28
- end
31
+ it { @person.delete.must_be_instance_of Person }
29
32
 
30
- describe '#update_attributes' do
33
+ it do
34
+ @person.deleted?.must_equal false
35
+ @person.persisted?.must_equal true
36
+ @person.delete
37
+ @person.deleted?.must_equal true
38
+ @person.persisted?.must_equal false
39
+ end
31
40
 
41
+ it do
42
+ @person.delete
43
+ Person.find(123).must_be_nil
44
+ end
32
45
  end
33
46
  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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -166,7 +166,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
166
166
  version: '0'
167
167
  segments:
168
168
  - 0
169
- hash: 3909175812129280825
169
+ hash: -2300288373958290693
170
170
  required_rubygems_version: !ruby/object:Gem::Requirement
171
171
  none: false
172
172
  requirements:
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  version: '0'
176
176
  segments:
177
177
  - 0
178
- hash: 3909175812129280825
178
+ hash: -2300288373958290693
179
179
  requirements: []
180
180
  rubyforge_project:
181
181
  rubygems_version: 1.8.23