baza_models 0.0.2 → 0.0.3

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: d7af27121360692e3093505352f08c8f855cef0d
4
- data.tar.gz: f926660c9fb19321c9cfc68e93b04bcd5dc06a26
3
+ metadata.gz: b4c06e71c9d91e97504d17b5a469f2c941c68e26
4
+ data.tar.gz: 9d0cc2326a8ce71b1f0b67cf2193b28c1e264e98
5
5
  SHA512:
6
- metadata.gz: 9c2f9cf0eecae07319682e1dff0413dc09809c75981ade008bf34648660a4f8f9478d03fef7209fc04d331074432e48e15c15e76847b56b6e036b9e196d6023b
7
- data.tar.gz: 5dff49f599cb002d1e8731af809ae8c42c19b6920e0a801cd0846b8fa2adc8ab595292f42d6d43502e30838c95907b1681014cc0224a030ba9996ce9c151b6de
6
+ metadata.gz: c762af7cad3328bd461d34e07939be0ff54e87518eba020676111777d7e84425ce88f3b90fb625bfdcee635202a29e9fe5403556511238b3ed3c11c4464ffd26
7
+ data.tar.gz: cb735d831b9c233a4031465feb2258689eb57ee688a20e7ab3e96d5b88b682d05dab606eaa9e66c83624a86c7bdbf6ef5b9602378d69b06a5a6412ec1502dea5
data/Gemfile CHANGED
@@ -3,9 +3,10 @@ source "http://rubygems.org"
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
5
 
6
- gem "array_enumerator"
6
+ gem "array_enumerator", ">= 0.0.10"
7
7
  gem "baza", "~> 0.0.20"
8
- gem "string-cases", ">= 0.0.2"
8
+ gem "string-cases", ">= 0.0.3"
9
+ gem "auto_autoloader", ">= 0.0.1"
9
10
 
10
11
  # Add dependencies to develop your gem here.
11
12
  # Include everything needed to run rake, tests, features, etc.
@@ -1,9 +1,8 @@
1
1
  GIT
2
2
  remote: git://github.com/kaspernj/best_practice_project.git
3
- revision: 2493cc7b6afc89705e22e4eed8e52f92d309555b
3
+ revision: 35df59c22465901fd31f841af7716ce770236831
4
4
  specs:
5
- best_practice_project (0.0.5)
6
- psych (~> 2.0.0)
5
+ best_practice_project (0.0.7)
7
6
 
8
7
  GEM
9
8
  remote: http://rubygems.org/
@@ -19,6 +18,8 @@ GEM
19
18
  ast (2.2.0)
20
19
  astrolabe (1.3.1)
21
20
  parser (~> 2.2)
21
+ auto_autoloader (0.0.1)
22
+ string-cases
22
23
  baza (0.0.20)
23
24
  array_enumerator (~> 0.0.10)
24
25
  datet (~> 0.0.25)
@@ -81,7 +82,6 @@ GEM
81
82
  parser (2.2.3.0)
82
83
  ast (>= 1.1, < 3.0)
83
84
  powerpack (0.1.1)
84
- psych (2.0.17)
85
85
  rack (1.6.4)
86
86
  rainbow (2.0.0)
87
87
  rake (10.4.2)
@@ -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.2)
117
+ string-cases (0.0.3)
118
118
  thread_safe (0.3.5)
119
119
  thread_safe (0.3.5-java)
120
120
  tins (1.6.0)
@@ -127,7 +127,8 @@ PLATFORMS
127
127
  ruby
128
128
 
129
129
  DEPENDENCIES
130
- array_enumerator
130
+ array_enumerator (>= 0.0.10)
131
+ auto_autoloader (>= 0.0.1)
131
132
  baza (~> 0.0.20)
132
133
  best_practice_project!
133
134
  bundler (~> 1.0)
@@ -140,7 +141,7 @@ DEPENDENCIES
140
141
  rspec (~> 3.3.0)
141
142
  rubocop
142
143
  sqlite3
143
- string-cases (>= 0.0.2)
144
+ string-cases (>= 0.0.3)
144
145
 
145
146
  BUNDLED WITH
146
147
  1.11.2
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- [![Build Status](https://img.shields.io/shippable/5506810c5ab6cc13529b84bf.svg)](https://app.shippable.com/projects/5506810c5ab6cc13529b84bf/builds/latest)
2
1
  [![Code Climate](https://codeclimate.com/github/kaspernj/baza_models/badges/gpa.svg)](https://codeclimate.com/github/kaspernj/baza_models)
3
2
  [![Test Coverage](https://codeclimate.com/github/kaspernj/baza_models/badges/coverage.svg)](https://codeclimate.com/github/kaspernj/baza_models)
3
+ [![Build Status](https://img.shields.io/shippable/5506810c5ab6cc13529b84bf.svg)](https://app.shippable.com/projects/5506810c5ab6cc13529b84bf/builds/latest)
4
4
 
5
5
  # BazaModels
6
6
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -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.2 ruby lib
5
+ # stub: baza_models 0.0.3 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "baza_models"
9
- s.version = "0.0.2"
9
+ s.version = "0.0.3"
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 = "2016-01-06"
14
+ s.date = "2016-01-07"
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 = [
@@ -53,6 +53,7 @@ Gem::Specification.new do |s|
53
53
  "lib/baza_models/query/not.rb",
54
54
  "lib/baza_models/ransacker.rb",
55
55
  "lib/baza_models/test_database_cleaner.rb",
56
+ "lib/baza_models/validators.rb",
56
57
  "lib/baza_models/validators/base_validator.rb",
57
58
  "lib/baza_models/validators/confirmation_validator.rb",
58
59
  "lib/baza_models/validators/format_validator.rb",
@@ -102,9 +103,10 @@ Gem::Specification.new do |s|
102
103
  s.specification_version = 4
103
104
 
104
105
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
105
- s.add_runtime_dependency(%q<array_enumerator>, [">= 0"])
106
+ s.add_runtime_dependency(%q<array_enumerator>, [">= 0.0.10"])
106
107
  s.add_runtime_dependency(%q<baza>, ["~> 0.0.20"])
107
- s.add_runtime_dependency(%q<string-cases>, [">= 0.0.2"])
108
+ s.add_runtime_dependency(%q<string-cases>, [">= 0.0.3"])
109
+ s.add_runtime_dependency(%q<auto_autoloader>, [">= 0.0.1"])
108
110
  s.add_development_dependency(%q<rspec>, ["~> 3.3.0"])
109
111
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
110
112
  s.add_development_dependency(%q<bundler>, ["~> 1.0"])
@@ -116,9 +118,10 @@ Gem::Specification.new do |s|
116
118
  s.add_development_dependency(%q<rubocop>, [">= 0"])
117
119
  s.add_development_dependency(%q<orm_adapter>, [">= 0"])
118
120
  else
119
- s.add_dependency(%q<array_enumerator>, [">= 0"])
121
+ s.add_dependency(%q<array_enumerator>, [">= 0.0.10"])
120
122
  s.add_dependency(%q<baza>, ["~> 0.0.20"])
121
- s.add_dependency(%q<string-cases>, [">= 0.0.2"])
123
+ s.add_dependency(%q<string-cases>, [">= 0.0.3"])
124
+ s.add_dependency(%q<auto_autoloader>, [">= 0.0.1"])
122
125
  s.add_dependency(%q<rspec>, ["~> 3.3.0"])
123
126
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
124
127
  s.add_dependency(%q<bundler>, ["~> 1.0"])
@@ -131,9 +134,10 @@ Gem::Specification.new do |s|
131
134
  s.add_dependency(%q<orm_adapter>, [">= 0"])
132
135
  end
133
136
  else
134
- s.add_dependency(%q<array_enumerator>, [">= 0"])
137
+ s.add_dependency(%q<array_enumerator>, [">= 0.0.10"])
135
138
  s.add_dependency(%q<baza>, ["~> 0.0.20"])
136
- s.add_dependency(%q<string-cases>, [">= 0.0.2"])
139
+ s.add_dependency(%q<string-cases>, [">= 0.0.3"])
140
+ s.add_dependency(%q<auto_autoloader>, [">= 0.0.1"])
137
141
  s.add_dependency(%q<rspec>, ["~> 3.3.0"])
138
142
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
139
143
  s.add_dependency(%q<bundler>, ["~> 1.0"])
@@ -1,15 +1,7 @@
1
- class BazaModels
2
- path = "#{File.realpath(File.dirname(__FILE__))}/baza_models"
1
+ require "auto_autoloader"
3
2
 
4
- autoload :Autoloader, "#{path}/autoloader"
5
- autoload :BazaOrmAdapter, "#{path}/baza_orm_adapter"
6
- autoload :CanCanAdapter, "#{path}/can_can_adapter"
7
- autoload :ClassTranslation, "#{path}/class_translation"
8
- autoload :Errors, "#{path}/errors"
9
- autoload :Model, "#{path}/model"
10
- autoload :Ransacker, "#{path}/ransacker"
11
- autoload :Query, "#{path}/query"
12
- autoload :TestDatabaseCleaner, "#{path}/test_database_cleaner"
3
+ class BazaModels
4
+ AutoAutoloader.autoload_sub_classes(self, __FILE__)
13
5
 
14
6
  class << self
15
7
  attr_accessor :primary_db
@@ -18,15 +10,4 @@ class BazaModels
18
10
  BazaModels::CanCanAdapter # Loading the model will make CanCan aware because of inherited hook CanCan implements
19
11
  end
20
12
  end
21
-
22
- class Validators
23
- path = "#{File.realpath(File.dirname(__FILE__))}/baza_models/validators"
24
-
25
- autoload :BaseValidator, "#{path}/base_validator"
26
- autoload :ConfirmationValidator, "#{path}/confirmation_validator"
27
- autoload :FormatValidator, "#{path}/format_validator"
28
- autoload :LengthValidator, "#{path}/length_validator"
29
- autoload :PresenceValidator, "#{path}/presence_validator"
30
- autoload :UniquenessValidator, "#{path}/uniqueness_validator"
31
- end
32
13
  end
@@ -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, init: true)
88
+ model = StringCases.constantize(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,7 +101,7 @@ 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, init: true)
104
+ model = StringCases.constantize(relation.fetch(:class_name)).new(model_data, init: true)
105
105
 
106
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 }
@@ -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, init: true)
122
+ model = StringCases.constantize(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 }
@@ -4,7 +4,20 @@ class BazaModels::ClassTranslation
4
4
  end
5
5
 
6
6
  def class_name_snake
7
- @class_name_snake ||= StringCases.camel_to_snake(@klass.name)
7
+ @class_name_snake ||= StringCases.camel_to_snake(@klass.name.split("::").last)
8
+ end
9
+
10
+ def class_name_snake_with_parents
11
+ unless @class_name_snake_with_parents
12
+ name = @klass.name
13
+ .split("::")
14
+ .map { |name_part| StringCases.camel_to_snake(name_part) }
15
+ .join("\\")
16
+
17
+ @class_name_snake_with_parents = name
18
+ end
19
+
20
+ @class_name_snake_with_parents
8
21
  end
9
22
 
10
23
  def human(args = {})
@@ -15,15 +28,15 @@ class BazaModels::ClassTranslation
15
28
  end
16
29
 
17
30
  keys = [
18
- "baza_models.models.#{class_name_snake}.#{count_key}",
19
- "activerecord.models.#{class_name_snake}.#{count_key}"
31
+ "baza_models.models.#{class_name_snake_with_parents}.#{count_key}",
32
+ "activerecord.models.#{class_name_snake_with_parents}.#{count_key}"
20
33
  ]
21
34
 
22
35
  keys.each do |key|
23
36
  return I18n.t(key) if I18n.exists?(key)
24
37
  end
25
38
 
26
- @klass.name
39
+ @klass.name.split("::").last
27
40
  end
28
41
 
29
42
  def param_key
@@ -1,18 +1,7 @@
1
1
  require "string-cases"
2
2
 
3
3
  class BazaModels::Model
4
- path = "#{File.dirname(__FILE__)}/model"
5
-
6
- autoload :BelongsToRelations, "#{path}/belongs_to_relations"
7
- autoload :CustomValidations, "#{path}/custom_validations"
8
- autoload :Delegation, "#{path}/delegation"
9
- autoload :HasManyRelations, "#{path}/has_many_relations"
10
- autoload :HasOneRelations, "#{path}/has_one_relations"
11
- autoload :Manipulation, "#{path}/manipulation"
12
- autoload :Queries, "#{path}/queries"
13
- autoload :Scopes, "#{path}/scopes"
14
- autoload :TranslationFunctionality, "#{path}/translation_functionality"
15
- autoload :Validations, "#{path}/validations"
4
+ AutoAutoloader.autoload_sub_classes(self, __FILE__)
16
5
 
17
6
  include BelongsToRelations
18
7
  include Delegation
@@ -30,7 +19,7 @@ class BazaModels::Model
30
19
 
31
20
  # Define all callback methods.
32
21
  CALLBACK_TYPES = [
33
- :after_initialize, :after_find,
22
+ :after_initialize, :after_find, :before_update, :after_update,
34
23
  :before_create, :after_create, :before_save, :after_save, :before_destroy, :after_destroy,
35
24
  :before_validation, :after_validation, :before_validation_on_create, :after_validation_on_create,
36
25
  :before_validation_on_update, :after_validation_on_update
@@ -308,7 +297,12 @@ protected
308
297
  if callback_data[:block]
309
298
  instance_eval(&callback_data.fetch(:block))
310
299
  elsif callback_data[:method_name]
311
- __send__(callback_data[:method_name], *callback_data.fetch(:args))
300
+ method_obj = method(callback_data.fetch(:method_name))
301
+
302
+ pass_args = callback_data.fetch(:args)
303
+ pass_args = [] if method_obj.arity == 0
304
+
305
+ __send__(callback_data[:method_name], *pass_args)
312
306
  else
313
307
  raise "Didn't know how to perform callbacks for #{name}"
314
308
  end
@@ -37,7 +37,7 @@ module BazaModels::Model::BelongsToRelations
37
37
  end
38
38
 
39
39
  foreign_id = @data.fetch(relation.fetch(:foreign_key))
40
- Object.const_get(class_name).find(foreign_id) if foreign_id
40
+ StringCases.constantize(class_name).find(foreign_id) if foreign_id
41
41
  end
42
42
  end
43
43
 
@@ -29,7 +29,7 @@ module BazaModels::Model::HasManyRelations
29
29
  if args && args[:class_name]
30
30
  relation[:class_name] = args.fetch(:class_name)
31
31
  else
32
- relation[:class_name] = StringCases.snake_to_camel(relation_name.to_s.gsub(/s$/, ""))
32
+ relation[:class_name] = StringCases.snake_to_camel(StringCases.singularize(relation_name))
33
33
  end
34
34
 
35
35
  @has_many_relations ||= []
@@ -39,7 +39,7 @@ module BazaModels::Model::HasManyRelations
39
39
  @relationships[relation_name] = relation
40
40
 
41
41
  define_method(relation_name) do
42
- class_instance = Object.const_get(relation.fetch(:class_name))
42
+ class_instance = StringCases.constantize(relation.fetch(:class_name))
43
43
  query = class_instance.where(relation.fetch(:foreign_key) => id)
44
44
  query._previous_model = self
45
45
  query._relation = relation
@@ -48,7 +48,7 @@ module BazaModels::Model::HasManyRelations
48
48
  query = query.instance_exec(&arg) if arg.is_a?(Proc)
49
49
  end
50
50
 
51
- return query
51
+ query
52
52
  end
53
53
  end
54
54
  end
@@ -44,7 +44,7 @@ module BazaModels::Model::HasOneRelations
44
44
  if relation[:args][:through]
45
45
  __send__(relation[:args][:through]).__send__(relation_name)
46
46
  else
47
- class_instance = Object.const_get(relation.fetch(:class_name))
47
+ class_instance = StringCases.constantize(relation.fetch(:class_name))
48
48
 
49
49
  query = class_instance.where(relation.fetch(:foreign_key) => id)
50
50
  query._previous_model = self
@@ -12,7 +12,9 @@ module BazaModels::Model::Manipulation
12
12
  if new_record
13
13
  status = create
14
14
  else
15
+ fire_callbacks(:before_update)
15
16
  db.update(table_name, @changes, id: id) if changed?
17
+ fire_callbacks(:after_update)
16
18
  status = true
17
19
  end
18
20
 
@@ -1,10 +1,7 @@
1
1
  require "array_enumerator"
2
2
 
3
3
  class BazaModels::Query
4
- path = "#{File.dirname(__FILE__)}/query"
5
-
6
- autoload :Inspector, "#{path}/inspector"
7
- autoload :Not, "#{path}/not"
4
+ AutoAutoloader.autoload_sub_classes(self, __FILE__)
8
5
 
9
6
  attr_accessor :_previous_model, :_relation
10
7
 
@@ -65,7 +65,7 @@ private
65
65
  BazaModels::Query::Inspector.new(
66
66
  query: @query,
67
67
  argument: value,
68
- model: Object.const_get(relationship.fetch(:class_name)),
68
+ model: StringCases.constantize(relationship.fetch(:class_name)),
69
69
  joins: @joins,
70
70
  joins_tracker: @joins_tracker[key]
71
71
  ).execute
@@ -0,0 +1,3 @@
1
+ class BazaModels::Validators
2
+ AutoAutoloader.autoload_sub_classes(self, __FILE__)
3
+ end
@@ -61,12 +61,18 @@ describe "BazaModels::Model" do
61
61
  it "#before_save, #after_save" do
62
62
  expect(user.before_save_called).to eq nil
63
63
  expect(user.after_save_called).to eq nil
64
+ expect(user.before_update_called).to eq nil
65
+ expect(user.after_update_called).to eq nil
64
66
  user.save!
65
67
  expect(user.before_save_called).to eq 1
66
68
  expect(user.after_save_called).to eq 1
69
+ expect(user.before_update_called).to eq nil
70
+ expect(user.after_update_called).to eq nil
67
71
  user.save!
68
72
  expect(user.before_save_called).to eq 2
69
73
  expect(user.after_save_called).to eq 2
74
+ expect(user.before_update_called).to eq 1
75
+ expect(user.after_update_called).to eq 1
70
76
  end
71
77
 
72
78
  it "#before_create, #after_create" do
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-06 00:00:00.000000000 Z
11
+ date: 2016-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: array_enumerator
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.0.10
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 0.0.10
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: baza
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +44,28 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 0.0.2
47
+ version: 0.0.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: auto_autoloader
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.1
48
62
  type: :runtime
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
- version: 0.0.2
68
+ version: 0.0.1
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -234,6 +248,7 @@ files:
234
248
  - lib/baza_models/query/not.rb
235
249
  - lib/baza_models/ransacker.rb
236
250
  - lib/baza_models/test_database_cleaner.rb
251
+ - lib/baza_models/validators.rb
237
252
  - lib/baza_models/validators/base_validator.rb
238
253
  - lib/baza_models/validators/confirmation_validator.rb
239
254
  - lib/baza_models/validators/format_validator.rb