ninja-model 0.4.2 → 0.5.1

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 (54) hide show
  1. data/.gitignore +19 -0
  2. data/Rakefile +0 -7
  3. data/autotest/discover.rb +1 -0
  4. data/lib/ninja_model.rb +22 -26
  5. data/lib/ninja_model/adapters.rb +33 -43
  6. data/lib/ninja_model/adapters/abstract_adapter.rb +2 -10
  7. data/lib/ninja_model/adapters/adapter_manager.rb +17 -10
  8. data/lib/ninja_model/adapters/adapter_pool.rb +15 -17
  9. data/lib/ninja_model/adapters/adapter_specification.rb +3 -3
  10. data/lib/ninja_model/associations.rb +25 -106
  11. data/lib/ninja_model/associations/association_proxy.rb +119 -1
  12. data/lib/ninja_model/associations/belongs_to_association.rb +5 -1
  13. data/lib/ninja_model/attribute.rb +130 -0
  14. data/lib/ninja_model/{attributes.rb → attribute_methods.rb} +21 -42
  15. data/lib/ninja_model/base.rb +23 -20
  16. data/lib/ninja_model/callbacks.rb +2 -11
  17. data/lib/ninja_model/identity.rb +6 -11
  18. data/lib/ninja_model/persistence.rb +15 -30
  19. data/lib/ninja_model/predicate.rb +4 -4
  20. data/lib/ninja_model/rails_ext/active_record.rb +187 -0
  21. data/lib/ninja_model/railtie.rb +14 -8
  22. data/lib/ninja_model/reflection.rb +7 -14
  23. data/lib/ninja_model/relation.rb +5 -3
  24. data/lib/ninja_model/relation/finder_methods.rb +4 -8
  25. data/lib/ninja_model/relation/spawn_methods.rb +1 -1
  26. data/lib/ninja_model/validation.rb +6 -23
  27. data/lib/ninja_model/version.rb +1 -1
  28. data/ninja-model.gemspec +28 -0
  29. data/spec/ninja_model/adapters/abstract_adapter_spec.rb +45 -0
  30. data/spec/ninja_model/adapters/adapter_manager_spec.rb +69 -0
  31. data/spec/ninja_model/adapters/adapter_pool_spec.rb +210 -48
  32. data/spec/ninja_model/adapters_spec.rb +77 -0
  33. data/spec/ninja_model/attribute_methods_spec.rb +95 -0
  34. data/spec/ninja_model/attribute_spec.rb +129 -0
  35. data/spec/ninja_model/base_spec.rb +4 -52
  36. data/spec/ninja_model/identity_spec.rb +16 -32
  37. data/spec/ninja_model/persistence_spec.rb +130 -4
  38. data/spec/ninja_model/predicate_spec.rb +40 -6
  39. data/spec/ninja_model/query_methods_spec.rb +76 -74
  40. data/spec/ninja_model/reflection_spec.rb +63 -0
  41. data/spec/ninja_model/relation_spec.rb +213 -20
  42. data/spec/ninja_model/symbol_spec.rb +19 -0
  43. data/spec/ninja_model/validation_spec.rb +18 -0
  44. data/spec/spec_helper.rb +9 -0
  45. data/spec/support/matchers/convert.rb +30 -0
  46. metadata +85 -63
  47. data/lib/ninja_model/associations/active_record_proxy.rb +0 -53
  48. data/lib/ninja_model/associations/ninja_model_proxy.rb +0 -46
  49. data/lib/ninja_model/configuration.rb +0 -20
  50. data/lib/ninja_model/errors.rb +0 -5
  51. data/lib/ninja_model/log_subscriber.rb +0 -18
  52. data/lib/ninja_model/scoping.rb +0 -50
  53. data/spec/ninja_model/attributes_spec.rb +0 -85
  54. data/spec/ninja_model/scoping_spec.rb +0 -40
@@ -1,18 +0,0 @@
1
- module NinjaModel
2
- class LogSubscriber < ActiveSupport::LogSubscriber
3
- def xml(event)
4
- return unless logger.debug?
5
-
6
- name = '%s (%.1fms)' % [event.payload[:name], event.duration]
7
- xml = event.payload[:xml]
8
-
9
- debug " #{name} #{xml}"
10
- end
11
-
12
- def logger
13
- NinjaModel::Base.logger
14
- end
15
- end
16
- end
17
-
18
- ActiveRecord::LogSubscriber.attach_to :ninja_model
@@ -1,50 +0,0 @@
1
- require 'active_support'
2
- require 'active_support/core_ext/array'
3
- require 'active_support/core_ext/kernel/singleton_class'
4
-
5
- module NinjaModel
6
- module Scoping
7
- extend ActiveSupport::Concern
8
-
9
- module ClassMethods
10
- def scoped(options = nil)
11
- if options
12
- scoped.apply_finder_options(options)
13
- else
14
- current_scoped_methods ? relation.merge(current_scoped_methods) : relation.clone
15
- end
16
- end
17
-
18
- def scopes
19
- read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
20
- end
21
-
22
- def scope(name, scope_options = {}, &block)
23
- name = name.to_sym
24
- valid_scope_name?(name)
25
- extension = Module.new(&block) if block_given?
26
-
27
- scopes[name] = lambda do |*args|
28
- options = scope_options.is_a?(Proc) ? scope_options.call(*args) : scope_options
29
- relation = if options.is_a?(Hash)
30
- scoped.apply_finder_options(options)
31
- elsif options
32
- scoped.merge(options)
33
- else
34
- scoped
35
- end
36
-
37
- extension ? relation.extending(extension) : relation
38
- end
39
-
40
- singleton_class.send(:redefine_method, name, &scopes[name])
41
- end
42
-
43
- def valid_scope_name?(name)
44
- if !scopes[name] && respond_to?(name, true)
45
- logger.warn "Creating scope :#{name}. Overwriting existing method #{self.name}.#{name}."
46
- end
47
- end
48
- end
49
- end
50
- end
@@ -1,85 +0,0 @@
1
- require 'spec_helper'
2
- require 'ninja_model/attributes'
3
-
4
- describe NinjaModel::Attributes do
5
- before(:each) do
6
- @klass = Class.new do
7
- def initialize
8
- @attributes = {}.with_indifferent_access
9
- end
10
- end
11
- @klass.send :include, NinjaModel::Attributes
12
- @klass.send :attr_reader, :attributes
13
- end
14
-
15
- describe 'adding an attribute' do
16
- before(:each) do
17
- @klass.attribute :var, :string
18
- @obj = @klass.new
19
- end
20
-
21
- it 'should add a new attribute' do
22
- @klass.model_attributes[:var].type.should eql(:string)
23
- end
24
-
25
- it 'should add a read method' do
26
- lambda { @obj.send :var }.should_not raise_error(NoMethodError)
27
- end
28
-
29
- it 'should add a write method' do
30
- lambda { @obj.send :var= }.should_not raise_error(NoMethodError)
31
- end
32
- end
33
-
34
- describe 'with an instance' do
35
- describe 'writing an attribute value' do
36
- before(:each) do
37
- @klass.attribute :valid, :string
38
- @obj = @klass.new
39
- end
40
-
41
- it 'should raise an error when writing an invalid attribute' do
42
- lambda { @obj.write_attribute(:invalid, 'test') }.should raise_error(NoMethodError)
43
- end
44
-
45
- it 'should properly update a valid attribute' do
46
- @obj.write_attribute(:valid, 'test')
47
- @obj.attributes['valid'].should eql('test')
48
- end
49
- end
50
-
51
- describe 'assigning from a hash' do
52
- before(:each) do
53
- @klass.attribute :valid, :string
54
- @obj = @klass.new
55
- end
56
-
57
- it 'should update instance attributes' do
58
- attrs = {:valid => 'valid value'}
59
- @obj.send :attributes=, attrs
60
- @obj.valid.should eql('valid value')
61
- end
62
- end
63
-
64
- describe 'checking existence of an attribute' do
65
- before(:each) do
66
- @klass.attribute :valid, :string
67
- @obj = @klass.new
68
- end
69
- it { (@obj.send :attribute_method?, :valid).should be_true }
70
- it { (@obj.send :attribute_method?, 'valid').should be_true }
71
- it { (@obj.send :attribute_method?, :invalid).should be_false }
72
- end
73
-
74
- describe 'assigining default attributes from model' do
75
- before(:each) do
76
- @klass.attribute :valid, :string, {:default => 'foobar'}
77
- @klass.send :class_inheritable_accessor, :primary_key
78
- @obj = @klass.new
79
- @attrs = @obj.attributes_from_model_attributes
80
- end
81
- it { @attrs.should have_key('valid') }
82
- it { @attrs[:valid].should eql('foobar') }
83
- end
84
- end
85
- end
@@ -1,40 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe NinjaModel::Scoping do
4
- before(:each) do
5
- @klass = Class.new
6
- @klass.send :include, NinjaModel::Scoping
7
- @relation = mock('Relation') do
8
- stubs(:apply_finder_options).returns(self)
9
- end
10
- @klass.stubs(:relation).returns(@relation)
11
- @klass.stubs(:current_scoped_methods).returns(nil)
12
- end
13
-
14
- describe 'scoped' do
15
- it 'should return a relation when no options are sent' do
16
- @klass.scoped.should be_instance_of(@relation.class)
17
- end
18
-
19
- it 'should accept scoping options' do
20
- @klass.scoped(:conditions => {:key => 'value'}).should be_instance_of(@relation.class)
21
-
22
- end
23
- end
24
-
25
- describe 'scopes' do
26
- it 'should return an empty hash when no scopes have been set' do
27
- @klass.scopes.should eql({})
28
- end
29
-
30
- it 'should return existing scopes' do
31
- scopes = mock('scopes')
32
- @klass.stubs(:read_inheritable_attribute).returns(scopes)
33
- @klass.scopes.should eql(scopes)
34
- end
35
- end
36
-
37
- describe 'defining a scope' do
38
-
39
- end
40
- end