baza_models 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1953b9a3edb12121b165ba16b4ad833bda5529b
4
- data.tar.gz: 18960bc46ba6f08b171186781844a9938ba48e1a
3
+ metadata.gz: d7af27121360692e3093505352f08c8f855cef0d
4
+ data.tar.gz: f926660c9fb19321c9cfc68e93b04bcd5dc06a26
5
5
  SHA512:
6
- metadata.gz: f1232a900ec342d84e63c264bd7399fa6d9bc37672c42e4a038b4bf4f8194b9afd4f97e343e64d55ab6898f1c8c73139e66114cde10128f77d3990f665360dc3
7
- data.tar.gz: f6ba88d75b1623ae7cfdb16f90fa1749c9cc2c8f41110b72f27792699e14a2d5cc03e36eb40acaba5313bdba8e3d78634bc0fa70a196a0f800e82c765abbd194
6
+ metadata.gz: 9c2f9cf0eecae07319682e1dff0413dc09809c75981ade008bf34648660a4f8f9478d03fef7209fc04d331074432e48e15c15e76847b56b6e036b9e196d6023b
7
+ data.tar.gz: 5dff49f599cb002d1e8731af809ae8c42c19b6920e0a801cd0846b8fa2adc8ab595292f42d6d43502e30838c95907b1681014cc0224a030ba9996ce9c151b6de
data/Gemfile CHANGED
@@ -5,7 +5,7 @@ source "http://rubygems.org"
5
5
 
6
6
  gem "array_enumerator"
7
7
  gem "baza", "~> 0.0.20"
8
- gem "string-cases"
8
+ gem "string-cases", ">= 0.0.2"
9
9
 
10
10
  # Add dependencies to develop your gem here.
11
11
  # Include everything needed to run rake, tests, features, etc.
data/Gemfile.lock CHANGED
@@ -114,7 +114,7 @@ GEM
114
114
  simplecov-html (~> 0.10.0)
115
115
  simplecov-html (0.10.0)
116
116
  sqlite3 (1.3.10)
117
- string-cases (0.0.1)
117
+ string-cases (0.0.2)
118
118
  thread_safe (0.3.5)
119
119
  thread_safe (0.3.5-java)
120
120
  tins (1.6.0)
@@ -140,7 +140,7 @@ DEPENDENCIES
140
140
  rspec (~> 3.3.0)
141
141
  rubocop
142
142
  sqlite3
143
- string-cases
143
+ string-cases (>= 0.0.2)
144
144
 
145
145
  BUNDLED WITH
146
- 1.10.6
146
+ 1.11.2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/baza_models.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: baza_models 0.0.1 ruby lib
5
+ # stub: baza_models 0.0.2 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "baza_models"
9
- s.version = "0.0.1"
9
+ s.version = "0.0.2"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["kaspernj"]
14
- s.date = "2015-12-30"
14
+ s.date = "2016-01-06"
15
15
  s.description = "ActiveRecord like models for the Baza database framework"
16
16
  s.email = "k@spernj.org"
17
17
  s.extra_rdoc_files = [
@@ -104,7 +104,7 @@ Gem::Specification.new do |s|
104
104
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
105
105
  s.add_runtime_dependency(%q<array_enumerator>, [">= 0"])
106
106
  s.add_runtime_dependency(%q<baza>, ["~> 0.0.20"])
107
- s.add_runtime_dependency(%q<string-cases>, [">= 0"])
107
+ s.add_runtime_dependency(%q<string-cases>, [">= 0.0.2"])
108
108
  s.add_development_dependency(%q<rspec>, ["~> 3.3.0"])
109
109
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
110
110
  s.add_development_dependency(%q<bundler>, ["~> 1.0"])
@@ -118,7 +118,7 @@ Gem::Specification.new do |s|
118
118
  else
119
119
  s.add_dependency(%q<array_enumerator>, [">= 0"])
120
120
  s.add_dependency(%q<baza>, ["~> 0.0.20"])
121
- s.add_dependency(%q<string-cases>, [">= 0"])
121
+ s.add_dependency(%q<string-cases>, [">= 0.0.2"])
122
122
  s.add_dependency(%q<rspec>, ["~> 3.3.0"])
123
123
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
124
124
  s.add_dependency(%q<bundler>, ["~> 1.0"])
@@ -133,7 +133,7 @@ Gem::Specification.new do |s|
133
133
  else
134
134
  s.add_dependency(%q<array_enumerator>, [">= 0"])
135
135
  s.add_dependency(%q<baza>, ["~> 0.0.20"])
136
- s.add_dependency(%q<string-cases>, [">= 0"])
136
+ s.add_dependency(%q<string-cases>, [">= 0.0.2"])
137
137
  s.add_dependency(%q<rspec>, ["~> 3.3.0"])
138
138
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
139
139
  s.add_dependency(%q<bundler>, ["~> 1.0"])
@@ -85,7 +85,7 @@ private
85
85
  end
86
86
 
87
87
  @db.select(relation.fetch(:table_name), id: model_ids) do |model_data|
88
- model = ::Object.const_get(relation.fetch(:class_name)).new(model_data)
88
+ model = ::Object.const_get(relation.fetch(:class_name)).new(model_data, init: true)
89
89
 
90
90
  orig_model_id = model_id_mappings.fetch(model_data.fetch(:id))
91
91
  orig_model = @models.detect { |array_model| array_model.id == orig_model_id }
@@ -101,9 +101,9 @@ private
101
101
  result = {models: []}
102
102
 
103
103
  @db.select(relation.fetch(:table_name), relation.fetch(:foreign_key) => model_ids) do |model_data|
104
- model = ::Object.const_get(relation.fetch(:class_name)).new(model_data)
104
+ model = ::Object.const_get(relation.fetch(:class_name)).new(model_data, init: true)
105
105
 
106
- orig_model_id = model_data.fetch(relation.fetch(:foreign_key))
106
+ orig_model_id = model_data.fetch(relation.fetch(:foreign_key).to_sym)
107
107
  orig_model = @models.detect { |array_model| array_model.id == orig_model_id }
108
108
 
109
109
  orig_model.autoloads[autoload_name] ||= []
@@ -119,7 +119,7 @@ private
119
119
  result = {models: []}
120
120
 
121
121
  @db.select(relation.fetch(:table_name), relation.fetch(:foreign_key) => model_ids) do |model_data|
122
- model = ::Object.const_get(relation.fetch(:class_name)).new(model_data)
122
+ model = ::Object.const_get(relation.fetch(:class_name)).new(model_data, init: true)
123
123
 
124
124
  orig_model_id = model_data.fetch(relation.fetch(:foreign_key))
125
125
  orig_model = @models.detect { |array_model| array_model.id == orig_model_id }
@@ -30,6 +30,7 @@ class BazaModels::Model
30
30
 
31
31
  # Define all callback methods.
32
32
  CALLBACK_TYPES = [
33
+ :after_initialize, :after_find,
33
34
  :before_create, :after_create, :before_save, :after_save, :before_destroy, :after_destroy,
34
35
  :before_validation, :after_validation, :before_validation_on_create, :after_validation_on_create,
35
36
  :before_validation_on_update, :after_validation_on_update
@@ -53,9 +54,8 @@ class BazaModels::Model
53
54
  end
54
55
  end
55
56
 
56
-
57
57
  QUERY_METHODS = [
58
- :all, :any?, :empty?, :none?, :count, :first, :find_first, :last, :length, :select, :includes,
58
+ :all, :any?, :empty?, :none?, :count, :find, :first, :find_first, :last, :length, :select, :includes,
59
59
  :joins, :group, :where, :order, :limit, :to_a, :accessible_by
60
60
  ]
61
61
  QUERY_METHODS.each do |query_method|
@@ -81,13 +81,13 @@ class BazaModels::Model
81
81
  @new_record = false
82
82
  else
83
83
  @new_record = true
84
+ fire_callbacks(:after_initialize)
84
85
  end
85
86
  end
86
87
 
87
88
  # rubocop:disable Style/TrivialAccessors
88
89
  def new_record?
89
90
  # rubocop:enable Style/TrivialAccessors
90
-
91
91
  @new_record
92
92
  end
93
93
 
@@ -114,6 +114,10 @@ class BazaModels::Model
114
114
  BazaModels::BazaOrmAdapter.new(class: self)
115
115
  end
116
116
 
117
+ def self.transaction(&blk)
118
+ @db.transaction(&blk)
119
+ end
120
+
117
121
  class << self
118
122
  attr_writer :db, :table_name
119
123
  end
@@ -36,7 +36,8 @@ module BazaModels::Model::BelongsToRelations
36
36
  class_name = StringCases.snake_to_camel(relation_name)
37
37
  end
38
38
 
39
- Object.const_get(class_name).find(@data.fetch(relation.fetch(:foreign_key)))
39
+ foreign_id = @data.fetch(relation.fetch(:foreign_key))
40
+ Object.const_get(class_name).find(foreign_id) if foreign_id
40
41
  end
41
42
  end
42
43
 
@@ -7,12 +7,12 @@ module BazaModels::Model::Manipulation
7
7
  if args[:validate] == false || valid?
8
8
  new_record = new_record?
9
9
  fire_callbacks(:before_save)
10
- self.updated_at = Time.now if has_attribute?(:updated_at)
10
+ self.updated_at = Time.now if changed? && has_attribute?(:updated_at)
11
11
 
12
12
  if new_record
13
13
  status = create
14
14
  else
15
- db.update(table_name, @changes, id: id)
15
+ db.update(table_name, @changes, id: id) if changed?
16
16
  status = true
17
17
  end
18
18
 
@@ -11,9 +11,7 @@ module BazaModels::Model::Queries
11
11
  end
12
12
 
13
13
  def find_by(where_hash)
14
- row = db.select(table_name, where_hash, limit: 1).fetch
15
- return new(row, init: true) if row
16
- false
14
+ where(where_hash).first
17
15
  end
18
16
 
19
17
  def find_by!(where_hash)
@@ -64,7 +64,19 @@ class BazaModels::Query
64
64
  end
65
65
 
66
66
  def find(id)
67
- clone.where(id: id).limit(1).to_enum.first
67
+ model = clone.where(id: id).limit(1).to_enum.first
68
+
69
+ if model
70
+ model.__send__(:fire_callbacks, :after_find)
71
+ else
72
+ raise BazaModels::Errors::RecordNotFound
73
+ end
74
+
75
+ model
76
+ end
77
+
78
+ def find_by(args)
79
+ clone.where(args).limit(1).to_enum.first
68
80
  end
69
81
 
70
82
  def first
@@ -89,14 +101,18 @@ class BazaModels::Query
89
101
  query.reverse_order.to_enum.first
90
102
  end
91
103
 
92
- def select(select)
93
- if select.is_a?(Symbol)
94
- @selects << "`#{@model.table_name}`.`#{select}`"
104
+ def select(select = nil, &blk)
105
+ if !select && blk
106
+ to_enum.select(&blk)
95
107
  else
96
- @selects << select
97
- end
108
+ if select.is_a?(Symbol)
109
+ @selects << "`#{@model.table_name}`.`#{select}`"
110
+ else
111
+ @selects << select
112
+ end
98
113
 
99
- self
114
+ self
115
+ end
100
116
  end
101
117
 
102
118
  def offset(offset)
@@ -128,7 +144,7 @@ class BazaModels::Query
128
144
  elsif arg.is_a?(TrueClass)
129
145
  arg = "1"
130
146
  else
131
- arg = "'#{@db.esc(arg)}'"
147
+ arg = @db.sqlval(arg)
132
148
  end
133
149
 
134
150
  str.sub!("?", arg)
@@ -141,10 +157,10 @@ class BazaModels::Query
141
157
  args.each do |key, value|
142
158
  if value.is_a?(Hash)
143
159
  value.each do |hash_key, hash_value|
144
- @wheres << "`#{key}`.`#{hash_key}` = '#{@db.esc(hash_value)}'"
160
+ @wheres << "`#{key}`.`#{key_convert(hash_key, hash_value)}` = #{@db.sqlval(value_convert(hash_value))}"
145
161
  end
146
162
  else
147
- @wheres << "`#{@model.table_name}`.`#{key}` = '#{@db.esc(value)}'"
163
+ @wheres << "`#{@model.table_name}`.`#{key_convert(key, value)}` = #{@db.sqlval(value_convert(value))}"
148
164
  end
149
165
  end
150
166
  end
@@ -193,6 +209,10 @@ class BazaModels::Query
193
209
  self
194
210
  end
195
211
 
212
+ def map(&blk)
213
+ to_enum.map(&blk)
214
+ end
215
+
196
216
  def to_enum
197
217
  return autoloaded_cache if should_use_autoload?
198
218
 
@@ -249,7 +269,7 @@ class BazaModels::Query
249
269
  end
250
270
 
251
271
  def find_first(args)
252
- where(args).first
272
+ clone.where(args).first
253
273
  end
254
274
 
255
275
  def to_a
@@ -416,7 +436,7 @@ private
416
436
 
417
437
  def any_wheres_other_than_relation?
418
438
  if @_previous_model && @_relation && @wheres.length == 1
419
- looks_like = "`#{@_relation.fetch(:table_name)}`.`#{@_relation.fetch(:foreign_key)}` = '#{@_previous_model.id}'"
439
+ looks_like = "`#{@_relation.fetch(:table_name)}`.`#{@_relation.fetch(:foreign_key)}` = #{@_previous_model.id}"
420
440
 
421
441
  return false if @wheres.first == looks_like
422
442
  end
@@ -444,4 +464,14 @@ private
444
464
  limit: @limit
445
465
  )
446
466
  end
467
+
468
+ def key_convert(key, value)
469
+ return "#{key}_id" if value.is_a?(BazaModels::Model)
470
+ key
471
+ end
472
+
473
+ def value_convert(value)
474
+ return value.id if value.is_a?(BazaModels::Model)
475
+ value
476
+ end
447
477
  end
@@ -29,6 +29,7 @@ describe BazaModels::Autoloader do
29
29
  expect(role.user).to eq user
30
30
  expect(role.autoloads).to have_received(:[]).with(:user)
31
31
  expect(role.autoloads[:user]).to eq user
32
+ expect(role.autoloads[:user].id).to eq user.id
32
33
  end
33
34
 
34
35
  it "autoloads via includes on has_one relations" do
@@ -40,7 +40,7 @@ describe BazaModels::Model::HasManyRelations do
40
40
 
41
41
  it "supports class_name and proc-arguments" do
42
42
  expect(user.admin_roles.to_a).to eq [role_admin]
43
- expect(user.admin_roles.to_sql).to eq "SELECT `roles`.* FROM `roles` WHERE `roles`.`user_id` = '#{user.id}' AND `roles`.`role` = 'administrator'"
43
+ expect(user.admin_roles.to_sql).to eq "SELECT `roles`.* FROM `roles` WHERE `roles`.`user_id` = #{user.id} AND `roles`.`role` = 'administrator'"
44
44
  end
45
45
  end
46
46
  end
@@ -19,6 +19,7 @@ describe BazaModels::Model::Manipulation do
19
19
  expect(user.updated_at).to_not eq nil
20
20
  old_updated_at = user.updated_at
21
21
  sleep 1
22
+ user.email = "test2@example.com"
22
23
  user.save!
23
24
  expect(user.updated_at).to_not eq old_updated_at
24
25
  end
@@ -22,7 +22,7 @@ describe BazaModels::Model::Queries do
22
22
 
23
23
  it "returns false when nothing is found" do
24
24
  user_found = User.find_by(id: 1, email: "test@example.com")
25
- expect(user_found).to eq false
25
+ expect(user_found).to eq nil
26
26
  end
27
27
 
28
28
  it "#find_by!" do
@@ -89,6 +89,18 @@ describe "BazaModels::Model" do
89
89
  expect(user.after_destroy_called).to eq 1
90
90
  end
91
91
 
92
+ it "#after_initialize" do
93
+ expect(user.instance_variable_get(:@after_initialize_called)).to eq 1
94
+ end
95
+
96
+ it "#after_find" do
97
+ user.save!
98
+
99
+ user_found = User.find(user.id)
100
+ expect(user_found.instance_variable_get(:@after_find_called)).to eq 1
101
+ expect(user.instance_variable_get(:@after_find_called)).to eq nil
102
+ end
103
+
92
104
  it "can use callbacks as blocks" do
93
105
  expect(user.before_save_block_called).to eq nil
94
106
  user.save!
@@ -99,7 +99,7 @@ describe BazaModels::Query do
99
99
  end
100
100
 
101
101
  it "#find" do
102
- expect(User.all.find(1)).to eq nil
102
+ expect { User.all.find(1) }.to raise_error(BazaModels::Errors::RecordNotFound)
103
103
  user.save!
104
104
  expect(User.all.find(user.id)).to eq user
105
105
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baza_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-30 00:00:00.000000000 Z
11
+ date: 2016-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: array_enumerator
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 0.0.2
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 0.0.2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement