active_mocker 1.1.11 → 1.1.20

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: 461b70e002570b08fb40fd4f3bbfeeedaac59672
4
- data.tar.gz: c1a570d233029b13813e61ba849b388edc417183
3
+ metadata.gz: 8e9d27f7fc8dbb5079f81659036d612ffd2260cb
4
+ data.tar.gz: ec92f2ff25fe4ca80de7bf1adb989d9ad846f881
5
5
  SHA512:
6
- metadata.gz: 80063db1732dec862301ce306b77a63b8097cb27f1b23d07d9c11ad25532b31c24f17ba5054410ec50b6fbd0677684bc91337a206b2045bf56cf981183efd171
7
- data.tar.gz: d336a09d187f76d1eec2fc3220cd277e5da941649bd378ad14e253b2be826b95f1ff5a317c0fce1edf1a17bd5b6c8ae80e8adff41b9232a14cd42745e56599b1
6
+ metadata.gz: 5a7c9caa9d2a8d161fb8b0f7012e801ef69fbb67c27e857ed49aa7a0cae7527bf3bac97749635c1ccac113c4b18d42f4b6e68ae7752add479a48a2951f6a1eae
7
+ data.tar.gz: a6a003002e18bc71c20afdda29c95be10d7aae86c5955b5b693734da93b9f52d38be81fd3afed55c24beeafc6879e2bd0ebd59f2753b4e4ef59ecbf609d250ad
data/README.md CHANGED
@@ -147,9 +147,9 @@ Or install it yourself as:
147
147
  => NameError: undefined method `bar' for class `PersonMock'
148
148
 
149
149
  ### ActiveRecord supported methods
150
- **class methods**
151
- * new
152
- * create
150
+ **Class methods**
151
+
152
+ * create/new
153
153
  * column_names
154
154
  * find
155
155
  * find_by
@@ -162,13 +162,22 @@ Or install it yourself as:
162
162
  * count
163
163
  * first/last
164
164
 
165
- **instance methods**
166
- * attributes
165
+ **Instance methods**
166
+
167
+ * attributes
167
168
  * update
168
169
  * save
169
170
  * write_attribute - (private)
170
171
  * read_attribute - (private)
171
172
 
173
+ **Collection Associations**
174
+
175
+
176
+ * last/first
177
+ * sum(attribute)
178
+ * <<
179
+ * Enumerable methods
180
+
172
181
  ### Known Limitations
173
182
 
174
183
  * **::mock** model names and table names must follow the default ActiveRecord naming pattern.
@@ -27,12 +27,12 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "bundler", "~> 1.5"
28
28
  spec.add_development_dependency "rake", "~>10.1"
29
29
  spec.add_development_dependency "rspec", "~>2.14"
30
- spec.add_development_dependency "i18n", "~>0.6"
30
+ # spec.add_development_dependency "i18n", "~>0.6"
31
31
  spec.add_development_dependency "sqlite3", "~>1.3"
32
32
 
33
- # if ENV['DEBUG'] == '1'
33
+ if ENV['DEBUG'] == '1'
34
34
  spec.add_development_dependency "debase", "~>0.0"
35
35
  spec.add_development_dependency "ruby-debug-ide", "~>0.4"
36
- # end
36
+ end
37
37
 
38
38
  end
data/lib/active_mocker.rb CHANGED
@@ -14,6 +14,7 @@ require 'active_mocker/active_record'
14
14
  require 'active_mocker/model_reader'
15
15
  require 'active_mocker/schema_reader'
16
16
  require 'active_mocker/active_record/schema'
17
+ require 'active_mocker/collection_association'
17
18
  require 'active_mocker/base'
18
19
  require 'active_mocker/active_record'
19
20
  require 'active_mocker/model_reader'
@@ -2,6 +2,14 @@ require 'ostruct'
2
2
 
3
3
  module Relationships
4
4
 
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ attr_reader :has_and_belongs_to_many, :belongs_to, :has_one, :has_many
11
+ end
12
+
5
13
  def relationships
6
14
  OpenStruct.new({has_many: @has_many ||= [],
7
15
  has_one: @has_one ||= [],
@@ -9,6 +17,14 @@ module Relationships
9
17
  has_and_belongs_to_many: @has_and_belongs_to_many ||= []})
10
18
  end
11
19
 
20
+ def single_relationships
21
+ belongs_to + has_one
22
+ end
23
+
24
+ def collections
25
+ has_and_belongs_to_many + has_many
26
+ end
27
+
12
28
  private
13
29
 
14
30
  def has_many(*args)
@@ -29,8 +29,7 @@ module ActiveMocker
29
29
  end
30
30
 
31
31
  def model_definition
32
- return @model_definition unless @model_definition.nil?
33
- @model_definition = ModelReader.new({model_dir: model_dir, file_reader: model_file_reader}).parse(model_file_name)
32
+ @model_definition ||= ModelReader.new({model_dir: model_dir, file_reader: model_file_reader}).parse(model_file_name)
34
33
  end
35
34
 
36
35
  def model_file_name
@@ -38,78 +37,72 @@ module ActiveMocker
38
37
  end
39
38
 
40
39
  def table_definition
41
- return @table_definition unless @table_definition.nil?
42
- table_name = model_name.tableize
43
- table = SchemaReader.new({schema_file: schema_file, file_reader: schema_file_reader, clear_cache: clear_cache}).search(table_name)
44
- raise "#{table_name} table not found." if table.nil?
45
- @table_definition = table
40
+ @table_definition ||= SchemaReader.new({schema_file: schema_file, file_reader: schema_file_reader, clear_cache: clear_cache}).search(model_name.tableize)
46
41
  end
47
42
 
48
43
  def active_hash_mock_class
49
44
  fill_templates
50
- klass = create_klass
51
- fields = table_definition.column_names
52
- klass.class_eval do
53
- klass.fields(*fields)
54
- end
55
-
56
- add_relationships_methods
57
- add_column_names_method
58
45
  add_method_mock_of
46
+ if schema_attributes
47
+ klass = create_klass
48
+ fields = table_definition.column_names
49
+ klass.class_eval do
50
+ klass.fields(*fields)
51
+ end
52
+ add_column_names_method
53
+ end
59
54
  if model_attributes
60
55
  add_class_methods
61
56
  add_instance_methods
57
+ add_single_relationships
58
+ add_collections_relationships
62
59
  end
63
60
  end
64
61
 
65
- def create_initializer
66
- klass = create_klass
67
- klass.class_eval <<-'eos', __FILE__, __LINE__+1
68
- def initialize(options={})
69
- options.each {|method, value| write_attribute(method, value) }
70
- end
71
- eos
72
- end
73
-
74
62
  def fill_templates
75
63
  klass = create_klass
76
64
  klass.send(:association_names=, model_definition.relationships)
77
65
  klass.send(:attribute_names=, table_definition.column_names)
78
66
  end
79
67
 
80
- def add_relationships
68
+ def add_method_mock_of
69
+ klass = create_klass
70
+ m_name = model_name
71
+ klass.instance_variable_set(:@model_class, model_definition.klass)
72
+ klass.instance_eval do
73
+ define_method(:mock_of) {m_name}
74
+ end
75
+ end
76
+
77
+ def add_single_relationships
78
+ klass = create_klass
79
+ model_definition.single_relationships.each do |m|
80
+ klass.send(:association_template)[m] = nil
81
+ create_association(m)
82
+ end
83
+ end
84
+
85
+ def add_collections_relationships
81
86
  klass = create_klass
82
- model_definition.relationships.each do |m|
83
- klass.send(:schema_attributes_template)[m] = nil
87
+ model_definition.collections.each do |m|
88
+ klass.send(:association_template)[m] = CollectionAssociation.new
84
89
  begin
85
90
  klass.class_eval <<-eos, __FILE__, __LINE__+1
86
- def #{m}
87
- read_attribute(#{m.inspect})
91
+ def #{m}
92
+ read_association(#{m.inspect})
88
93
  end
89
94
 
90
95
  def #{m}=(value)
91
- write_attribute(#{m.inspect}, value)
96
+ write_association(#{m.inspect}, CollectionAssociation.new(value))
92
97
  end
93
98
  eos
94
99
  rescue SyntaxError
95
- Logger_.debug "ActiveMocker :: Can't create accessor methods for #{m}.\n #{caller}"
100
+ Logger_.warn "ActiveMocker :: Can't create accessor methods for #{m}.\n #{caller}"
96
101
  end
97
102
  end
98
103
  end
99
104
 
100
- def add_method_mock_of
101
- klass = create_klass
102
- m_name = model_name
103
- klass.instance_variable_set(:@model_class, model_definition.klass)
104
- klass.instance_eval do
105
- define_method(:mock_of) {m_name}
106
- end
107
- end
108
-
109
- def add_relationships_methods
110
- klass = create_klass
111
- model_definition.relationships.each do |m|
112
- klass.send(:schema_attributes_template)[m] = nil
105
+ def create_association(m)
113
106
  begin
114
107
  klass.class_eval <<-eos, __FILE__, __LINE__+1
115
108
  def #{m}
@@ -121,9 +114,8 @@ module ActiveMocker
121
114
  end
122
115
  eos
123
116
  rescue SyntaxError
124
- Logger_.debug "ActiveMocker :: Can't create accessor methods for #{m}.\n #{caller}"
117
+ Logger_.warn "ActiveMocker :: Can't create accessor methods for #{m}.\n #{caller}"
125
118
  end
126
- end
127
119
  end
128
120
 
129
121
 
@@ -131,11 +123,14 @@ module ActiveMocker
131
123
  klass = create_klass
132
124
  model_definition.instance_methods_with_arguments.each do |method|
133
125
  m = method.keys.first
126
+ if m == :attributes
127
+ Logger_.warn "ActiveMocker Depends on the #attributes method. It will not be redefined for the mock."
128
+ next
129
+ end
134
130
  params = Reparameterize.call(method.values.first)
135
131
  params_pass = Reparameterize.call(method.values.first, true)
136
132
 
137
133
  klass.send(:model_methods_template)[m] = eval_lambda(params, %Q[raise "##{m} is not Implemented for Class: #{klass.name}"])
138
-
139
134
  klass.class_eval <<-eos, __FILE__, __LINE__+1
140
135
  def #{m}(#{params})
141
136
  block = model_instance_methods[#{m.inspect}].to_proc
@@ -0,0 +1,42 @@
1
+ module ActiveMocker
2
+
3
+ class CollectionAssociation
4
+
5
+ include Enumerable
6
+
7
+ attr_accessor :collection
8
+
9
+ def initialize(collection=[])
10
+ @collection = *collection ||=[]
11
+ end
12
+
13
+ def each(&block)
14
+ collection.each do |item|
15
+ block.call(item)
16
+ end
17
+ end
18
+
19
+ def last
20
+ collection.last
21
+ end
22
+
23
+ def <<(*records)
24
+ collection.concat(records.flatten)
25
+ end
26
+
27
+ def sum(attribute=nil)
28
+ values = collection.map{ |obj| obj.send(attribute) }
29
+ values.inject{ |sum, n| sum + n }
30
+ end
31
+
32
+ def ==(other_ary)
33
+ collection == other_ary
34
+ end
35
+
36
+ def method_missing(meth, *args, &block)
37
+ collection.send(meth, *args, &block)
38
+ end
39
+
40
+ end
41
+ end
42
+
@@ -24,13 +24,12 @@ module ActiveMocker
24
24
  while failure
25
25
  begin
26
26
  m = Module.new
27
- m.module_eval(read_file)
27
+ m.module_eval(read_file, file_path)
28
28
  rescue NameError => e
29
29
  raise e
30
30
  result = e.to_s.scan /::(\w*)$/ # gets the Constant name from error
31
31
  const_name = result.flatten.first
32
32
  Logger_.debug "ActiveMocker :: Can't can't find Constant #{const_name} from class #{model_name}..\n #{caller}"
33
- # Object.const_set(const_name,const_name)
34
33
  next
35
34
  end
36
35
  failure = false
@@ -40,7 +39,11 @@ module ActiveMocker
40
39
  end
41
40
 
42
41
  def read_file
43
- file_reader.read("#{model_dir}/#{model_name}.rb")
42
+ file_reader.read(file_path)
43
+ end
44
+
45
+ def file_path
46
+ "#{model_dir}/#{model_name}.rb"
44
47
  end
45
48
 
46
49
  def class_methods
@@ -71,6 +74,14 @@ module ActiveMocker
71
74
  relationships_types.to_h.values.flatten
72
75
  end
73
76
 
77
+ def collections
78
+ klass.collections.flatten.compact
79
+ end
80
+
81
+ def single_relationships
82
+ klass.single_relationships.flatten.compact
83
+ end
84
+
74
85
  end
75
86
 
76
87
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveMocker
2
- VERSION = "1.1.11"
2
+ VERSION = "1.1.20"
3
3
  end
@@ -5,6 +5,7 @@ require 'logger'
5
5
  require 'active_mocker/logger'
6
6
  require 'string_reader'
7
7
  require 'active_mocker/public_methods'
8
+ require 'active_mocker/collection_association'
8
9
  require 'active_mocker/table'
9
10
  require 'active_mocker/config'
10
11
  require 'active_mocker/reparameterize'
@@ -124,6 +125,7 @@ describe ActiveMocker::Base do
124
125
  StringReader.new <<-eos
125
126
  class Person < ActiveRecord::Base
126
127
  belongs_to :account
128
+ has_many :files
127
129
  end
128
130
  eos
129
131
  }
@@ -133,6 +135,16 @@ describe ActiveMocker::Base do
133
135
  expect(result.account).to eq 'Account'
134
136
  end
135
137
 
138
+ it 'add has_many relationship' do
139
+
140
+ expect(mock_class.new.files.class).to eq ActiveMocker::CollectionAssociation
141
+ expect(mock_class.new.files.count).to eq 0
142
+ mock_inst = mock_class.new
143
+ mock_inst.files << 1
144
+ expect(mock_inst.files.count).to eq 1
145
+
146
+ end
147
+
136
148
  end
137
149
 
138
150
  describe 'conflict of instance mocks and class mocks' do
@@ -435,4 +447,24 @@ describe ActiveMocker::Base do
435
447
 
436
448
  end
437
449
 
450
+ describe 'cannot redefine attributes method' do
451
+
452
+ let(:model_file){
453
+ StringReader.new <<-eos
454
+ class Person < ActiveRecord::Base
455
+ def attributes; end
456
+
457
+ end
458
+ eos
459
+ }
460
+
461
+ it 'still works' do
462
+
463
+ mock_class.new.attributes
464
+
465
+ end
466
+
467
+
468
+ end
469
+
438
470
  end
@@ -0,0 +1,81 @@
1
+ require 'rspec'
2
+ $:.unshift File.expand_path('../../', __FILE__)
3
+ require 'active_mocker/collection_association'
4
+ require 'ostruct'
5
+
6
+ describe ActiveMocker::CollectionAssociation do
7
+
8
+ subject{described_class.new}
9
+
10
+ describe '#sum' do
11
+
12
+
13
+ it 'sum values by attribute name' do
14
+
15
+ subject << [OpenStruct.new(value: 1), OpenStruct.new(value: 1)]
16
+
17
+ expect(subject.sum(:value)).to eq 2
18
+
19
+ end
20
+
21
+ end
22
+
23
+ describe '#<<' do
24
+
25
+ it 'will add a single item to the array' do
26
+
27
+ subject << "item"
28
+
29
+ expect(subject.count).to eq 1
30
+ expect(subject.first).to eq 'item'
31
+
32
+ end
33
+
34
+ it 'will add a many item to the array' do
35
+
36
+ subject << ['item1', 'item2', 'item3']
37
+
38
+ expect(subject.count).to eq 3
39
+ expect(subject).to eq ['item1', 'item2', 'item3']
40
+
41
+ end
42
+
43
+ end
44
+
45
+ describe 'new' do
46
+
47
+ it 'take optional item and adds to collection' do
48
+
49
+ subject = described_class.new(1)
50
+
51
+ expect(subject.first).to eq 1
52
+
53
+ end
54
+
55
+ it 'can take an array' do
56
+
57
+ subject = described_class.new([1])
58
+
59
+ expect(subject.first).to eq 1
60
+
61
+ subject = described_class.new([1,2])
62
+
63
+ expect(subject.last).to eq 2
64
+
65
+ end
66
+
67
+ end
68
+
69
+ describe 'empty?' do
70
+
71
+ it 'works' do
72
+
73
+ expect(subject.empty?).to eq true
74
+
75
+ end
76
+
77
+
78
+ end
79
+
80
+
81
+ end
@@ -78,7 +78,7 @@ describe ActiveMocker::ModelReader do
78
78
 
79
79
  it '#has_and_belongs_to_many' do
80
80
 
81
- expect(subject.relationships_types.has_and_belongs_to_many).to eq([[:disclosure]])
81
+ expect(subject.relationships_types.has_and_belongs_to_many).to eq([[:disclosures]])
82
82
 
83
83
  end
84
84
 
@@ -88,7 +88,27 @@ describe ActiveMocker::ModelReader do
88
88
 
89
89
  it 'returns an array of relations' do
90
90
 
91
- expect(subject.relationships).to eq [:users, :account, :company, :disclosure]
91
+ expect(subject.relationships).to eq [:users, :account, :company, :disclosures]
92
+
93
+ end
94
+
95
+ end
96
+
97
+ describe '#collections' do
98
+
99
+ it 'returns an array of relations' do
100
+
101
+ expect(subject.collections).to eq [:disclosures, :users]
102
+
103
+ end
104
+
105
+ end
106
+
107
+ describe '#single_relationships' do
108
+
109
+ it 'returns an array of relations' do
110
+
111
+ expect(subject.single_relationships).to eq [:company, :account]
92
112
 
93
113
  end
94
114
 
@@ -125,5 +145,6 @@ describe ActiveMocker::ModelReader do
125
145
 
126
146
  end
127
147
 
148
+
128
149
  end
129
150
 
data/spec/lib/model.rb CHANGED
@@ -7,7 +7,7 @@ class Model < ActiveRecord::Base
7
7
  belongs_to :company, class_name: 'PlanServiceCategory'
8
8
  has_many :users
9
9
  has_one :account
10
- has_and_belongs_to_many :disclosure
10
+ has_and_belongs_to_many :disclosures
11
11
  devise :database_authenticatable, :registerable,
12
12
  :recoverable, :rememberable, :trackable, :validatable
13
13
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_mocker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.11
4
+ version: 1.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Zeisler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-21 00:00:00.000000000 Z
11
+ date: 2014-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -94,20 +94,6 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '2.14'
97
- - !ruby/object:Gem::Dependency
98
- name: i18n
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: '0.6'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '0.6'
111
97
  - !ruby/object:Gem::Dependency
112
98
  name: sqlite3
113
99
  requirement: !ruby/object:Gem::Requirement
@@ -122,34 +108,6 @@ dependencies:
122
108
  - - "~>"
123
109
  - !ruby/object:Gem::Version
124
110
  version: '1.3'
125
- - !ruby/object:Gem::Dependency
126
- name: debase
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - "~>"
130
- - !ruby/object:Gem::Version
131
- version: '0.0'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - "~>"
137
- - !ruby/object:Gem::Version
138
- version: '0.0'
139
- - !ruby/object:Gem::Dependency
140
- name: ruby-debug-ide
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - "~>"
144
- - !ruby/object:Gem::Version
145
- version: '0.4'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - "~>"
151
- - !ruby/object:Gem::Version
152
- version: '0.4'
153
111
  description: Create mocks from active record models without loading rails or running
154
112
  a database. The Mock's methods have the same arguments as the AR model and if they
155
113
  change you get a error in your test.
@@ -181,6 +139,7 @@ files:
181
139
  - lib/active_mocker/active_record/unknown_class_method.rb
182
140
  - lib/active_mocker/active_record/unknown_module.rb
183
141
  - lib/active_mocker/base.rb
142
+ - lib/active_mocker/collection_association.rb
184
143
  - lib/active_mocker/config.rb
185
144
  - lib/active_mocker/const_sets.rb
186
145
  - lib/active_mocker/field.rb
@@ -199,6 +158,7 @@ files:
199
158
  - lib/string_reader.rb
200
159
  - spec/lib/active_mocker/active_record/schema_spec.rb
201
160
  - spec/lib/active_mocker/base_spec.rb
161
+ - spec/lib/active_mocker/collection_association_spec.rb
202
162
  - spec/lib/active_mocker/model_reader_spec.rb
203
163
  - spec/lib/active_mocker/schema_reader_spec.rb
204
164
  - spec/lib/compare_mocker_and_record_spec.rb
@@ -233,6 +193,7 @@ summary: Create mocks from active record models without loading rails or running
233
193
  test_files:
234
194
  - spec/lib/active_mocker/active_record/schema_spec.rb
235
195
  - spec/lib/active_mocker/base_spec.rb
196
+ - spec/lib/active_mocker/collection_association_spec.rb
236
197
  - spec/lib/active_mocker/model_reader_spec.rb
237
198
  - spec/lib/active_mocker/schema_reader_spec.rb
238
199
  - spec/lib/compare_mocker_and_record_spec.rb