pickle 0.2.12 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/pickle/email.rb CHANGED
@@ -11,7 +11,7 @@ module Pickle
11
11
  index = parse_index(match[1])
12
12
  email_has_fields?(@emails[index], fields) ? @emails[index] : nil
13
13
  end
14
-
14
+
15
15
  def email_has_fields?(email, fields)
16
16
  parse_fields(fields).each do |key, val|
17
17
  return false unless (Array(email.send(key)) & Array(val)).any?
@@ -28,7 +28,7 @@ module Pickle
28
28
  request_uri = URI::parse(link).request_uri
29
29
  visit request_uri
30
30
  end
31
-
31
+
32
32
  protected
33
33
  def open_in_browser(path) # :nodoc
34
34
  require "launchy"
@@ -36,7 +36,7 @@ module Pickle
36
36
  rescue LoadError
37
37
  warn "Sorry, you need to install launchy to open emails: `gem install launchy`"
38
38
  end
39
-
39
+
40
40
  # Saves the emails out to RAILS_ROOT/tmp/ and opens it in the default
41
41
  # web browser if on OS X. (depends on webrat)
42
42
  def save_and_open_emails
@@ -76,4 +76,4 @@ module Pickle
76
76
  end
77
77
 
78
78
  end
79
- end
79
+ end
@@ -26,6 +26,7 @@ module Pickle
26
26
  fields = fields.is_a?(Hash) ? parse_hash(fields) : parse_fields(fields)
27
27
  record = pickle_config.factories[factory].create(fields)
28
28
  store_model(factory, label, record)
29
+ record
29
30
  end
30
31
 
31
32
  # if a column exists in the table which matches the singular factory name, this is used as the pickle ref
@@ -39,12 +40,17 @@ module Pickle
39
40
 
40
41
  def find_model(a_model_name, fields = nil)
41
42
  factory, name = *parse_model(a_model_name)
43
+
42
44
  raise ArgumentError, "Can't find a model with an ordinal (e.g. 1st user)" if name.is_a?(Integer)
45
+
43
46
  model_class = pickle_config.factories[factory].klass
44
- fields = fields.is_a?(Hash) ? parse_hash(fields) : parse_fields(fields)
45
- if record = model_class.find(:first, :conditions => convert_models_to_attributes(model_class, fields))
46
- store_model(factory, name, record)
47
- end
47
+ fields = fields.is_a?(Hash) ? parse_hash(fields) : parse_fields(fields)
48
+ conditions = convert_models_to_attributes(model_class, fields)
49
+ record = Pickle::Adapter.find_first_model(model_class, conditions)
50
+
51
+ store_model(factory, name, record) if record
52
+
53
+ record
48
54
  end
49
55
 
50
56
  def find_model!(a_model_name, fields = nil)
@@ -53,8 +59,11 @@ module Pickle
53
59
 
54
60
  def find_models(factory, fields = nil)
55
61
  models_by_index(factory).clear
62
+
56
63
  model_class = pickle_config.factories[factory].klass
57
- records = model_class.find(:all, :conditions => convert_models_to_attributes(model_class, parse_fields(fields)))
64
+ conditions = convert_models_to_attributes(model_class, parse_fields(fields))
65
+ records = Pickle::Adapter.find_all_models(model_class, conditions)
66
+
58
67
  records.each {|record| store_model(factory, nil, record)}
59
68
  end
60
69
 
@@ -87,7 +96,9 @@ module Pickle
87
96
 
88
97
  # return a newly selected model
89
98
  def model(name)
90
- (model = created_model(name)) && model.class.find(model.id)
99
+ model = created_model(name)
100
+ return nil unless model
101
+ Pickle::Adapter.get_model(model.class, model.id)
91
102
  end
92
103
 
93
104
  # predicate version which raises no errors
@@ -112,7 +123,9 @@ module Pickle
112
123
 
113
124
  # return all models of specified type (freshly selected from the database)
114
125
  def models(factory)
115
- created_models(factory).map{|model| model.class.find(model.id) }
126
+ created_models(factory).map do |model|
127
+ Pickle::Adapter.get_model(model.class, model.id)
128
+ end
116
129
  end
117
130
 
118
131
  def respond_to_with_pickle_parser?(method, include_private = false)
@@ -136,14 +149,16 @@ module Pickle
136
149
  def pickle_parser
137
150
  @pickle_parser or self.pickle_parser = Pickle.parser
138
151
  end
139
-
152
+
140
153
  def pickle_config
141
154
  pickle_parser.config
142
155
  end
143
156
 
144
157
  def convert_models_to_attributes(ar_class, attrs)
145
158
  attrs.each do |key, val|
146
- if val.is_a?(ActiveRecord::Base) && ar_class.column_names.include?("#{key}_id")
159
+ if ((defined?(ActiveRecord::Base) && val.is_a?(ActiveRecord::Base)) ||
160
+ (defined?(DataMapper::Model) && val.is_a?(DataMapper::Model))) &&
161
+ Pickle::Adapter.column_names(ar_class).include?("#{key}_id")
147
162
  attrs["#{key}_id"] = val.id
148
163
  attrs["#{key}_type"] = val.class.base_class.name if ar_class.column_names.include?("#{key}_type")
149
164
  attrs.delete(key)
data/lib/pickle/world.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  require 'pickle'
2
2
 
3
+ # auto require for active record and datamapper
4
+ require 'pickle/adapters/active_record' if defined?(ActiveRecord::Base)
5
+ require 'pickle/adapters/data_mapper' if defined?(DataMapper::Resource)
6
+
3
7
  # make cucumber world pickle aware
4
8
  World(Pickle::Session)
5
9
 
data/pickle.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{pickle}
8
- s.version = "0.2.12"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ian White"]
@@ -54,6 +54,8 @@ Gem::Specification.new do |s|
54
54
  "init.rb",
55
55
  "lib/pickle.rb",
56
56
  "lib/pickle/adapter.rb",
57
+ "lib/pickle/adapters/active_record.rb",
58
+ "lib/pickle/adapters/data_mapper.rb",
57
59
  "lib/pickle/config.rb",
58
60
  "lib/pickle/email.rb",
59
61
  "lib/pickle/email/parser.rb",
@@ -3,176 +3,156 @@ require File.dirname(__FILE__) + '/../spec_helper'
3
3
  require 'active_record'
4
4
  require 'factory_girl'
5
5
  require 'machinist/active_record'
6
+ require 'pickle/adapters/active_record'
6
7
 
7
8
  describe Pickle::Adapter do
8
9
  it ".factories should raise NotImplementedError" do
9
10
  lambda{ Pickle::Adapter.factories }.should raise_error(NotImplementedError)
10
11
  end
11
-
12
+
12
13
  it "#create should raise NotImplementedError" do
13
14
  lambda{ Pickle::Adapter.new.create }.should raise_error(NotImplementedError)
14
15
  end
15
-
16
- describe ".suitable_for_pickle?(klass)" do
17
- let :klass do
18
- mock('Class', :abstract_class? => false, :table_exists? => true)
19
- end
20
-
21
- before do
22
- Pickle::Adapter.stub!(:framework_class?).and_return(false)
23
- end
24
-
25
- it "a non abstract class that has a table, and is not a framework class, is suitable_for_pickle" do
26
- Pickle::Adapter.should be_suitable_for_pickle(klass)
27
- end
28
-
29
- it "an abtract_class is not suitable_for_pickle" do
30
- klass.stub!(:abstract_class?).and_return(true)
31
- Pickle::Adapter.should_not be_suitable_for_pickle(klass)
32
- end
33
-
34
- it "a class with no table is not suitable_for_pickle" do
35
- klass.stub!(:table_exists?).and_return(false)
36
- Pickle::Adapter.should_not be_suitable_for_pickle(klass)
37
- end
38
-
39
- it "an frame_work class is not suitable_for_pickle" do
40
- Pickle::Adapter.should_receive(:framework_class?).with(klass).and_return(true)
41
- Pickle::Adapter.should_not be_suitable_for_pickle(klass)
42
- end
43
- end
44
-
16
+
45
17
  describe ".model_classes" do
46
18
  before do
47
19
  Pickle::Adapter.model_classes = nil
48
20
  end
49
-
21
+
50
22
  it "should only include #suitable_for_pickle classes" do
51
23
  klass1 = Class.new(ActiveRecord::Base)
52
24
  klass2 = Class.new(ActiveRecord::Base)
53
- Pickle::Adapter.should_receive(:suitable_for_pickle?).with(klass1).and_return(true)
54
- Pickle::Adapter.should_receive(:suitable_for_pickle?).with(klass2).and_return(false)
55
- Pickle::Adapter.model_classes.should include(klass1)
56
- Pickle::Adapter.model_classes.should_not include(klass2)
25
+ klass3 = Class.new(ActiveRecord::Base)
26
+ klass4 = Class.new(ActiveRecord::Base)
27
+ klass5 = Class.new(ActiveRecord::Base)
28
+ klass6 = Class.new(ActiveRecord::Base)
29
+ [klass1, klass2,klass3,klass4, klass5, klass6].each{|k| k.stub!(:table_exists?).and_return(true)}
30
+
31
+ klass2.stub!(:name).and_return("CGI::Session::ActiveRecordStore::Session")
32
+ klass3.stub!(:name).and_return("ActiveRecord::SessionStore::Session")
33
+ klass4.stub!(:table_exists?).and_return(false)
34
+ klass5.stub!(:abstract_class?).and_return(true)
35
+ Pickle::Adapter.model_classes.should include(klass1, klass6)
36
+ Pickle::Adapter.model_classes.should_not include(klass2, klass3, klass4, klass5)
57
37
  end
58
38
  end
59
-
39
+
60
40
  describe "adapters: " do
61
41
  before do
62
42
  @klass1 = returning(Class.new(ActiveRecord::Base)) {|k| k.stub!(:name).and_return('One')}
63
43
  @klass2 = returning(Class.new(ActiveRecord::Base)) {|k| k.stub!(:name).and_return('One::Two')}
64
44
  @klass3 = returning(Class.new(ActiveRecord::Base)) {|k| k.stub!(:name).and_return('Three')}
65
45
  end
66
-
46
+
67
47
  describe 'ActiveRecord' do
68
48
  before do
69
- Pickle::Adapter::ActiveRecord.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
49
+ ActiveRecord::Base::PickleAdapter.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
70
50
  end
71
-
51
+
72
52
  it ".factories should create one for each active record class" do
73
53
  Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass1).once
74
54
  Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass2).once
75
55
  Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass3).once
76
56
  Pickle::Adapter::ActiveRecord.factories
77
57
  end
78
-
58
+
79
59
  describe ".new(Class)" do
80
60
  before do
81
61
  @factory = Pickle::Adapter::ActiveRecord.new(@klass2)
82
62
  end
83
-
63
+
84
64
  it "should have underscored (s/_) name of Class as #name" do
85
65
  @factory.name.should == 'one_two'
86
66
  end
87
-
67
+
88
68
  it "#create(attrs) should call Class.create!(attrs)" do
89
69
  @klass2.should_receive(:create!).with({:key => "val"})
90
70
  @factory.create(:key => "val")
91
71
  end
92
72
  end
93
73
  end
94
-
74
+
95
75
  describe 'FactoryGirl' do
96
76
  before do
97
77
  Pickle::Adapter::FactoryGirl.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
98
78
  @orig_factories, Factory.factories = Factory.factories, {}
99
-
79
+
100
80
  Factory.define(:one, :class => @klass1) {}
101
81
  Factory.define(:two, :class => @klass2) {}
102
82
  @factory1 = Factory.factories[:one]
103
83
  @factory2 = Factory.factories[:two]
104
84
  end
105
-
85
+
106
86
  after do
107
87
  Factory.factories = @orig_factories
108
88
  end
109
-
89
+
110
90
  it ".factories should create one for each factory" do
111
91
  Pickle::Adapter::FactoryGirl.should_receive(:new).with(@factory1).once
112
92
  Pickle::Adapter::FactoryGirl.should_receive(:new).with(@factory2).once
113
93
  Pickle::Adapter::FactoryGirl.factories
114
94
  end
115
-
95
+
116
96
  describe ".new(factory)" do
117
97
  before do
118
98
  @factory = Pickle::Adapter::FactoryGirl.new(@factory1)
119
99
  end
120
-
100
+
121
101
  it "should have name of factory_name" do
122
102
  @factory.name.should == 'one'
123
103
  end
124
-
104
+
125
105
  it "should have klass of build_class" do
126
106
  @factory.klass.should == @klass1
127
107
  end
128
-
108
+
129
109
  it "#create(attrs) should call Factory(<:key>, attrs)" do
130
110
  Factory.should_receive(:create).with("one", {:key => "val"})
131
111
  @factory.create(:key => "val")
132
112
  end
133
113
  end
134
114
  end
135
-
115
+
136
116
  describe 'Machinist' do
137
117
  before do
138
118
  Pickle::Adapter::Machinist.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
139
-
119
+
140
120
  @klass1.blueprint {}
141
121
  @klass3.blueprint {}
142
122
  @klass3.blueprint(:special) {}
143
123
  end
144
-
124
+
145
125
  it ".factories should create one for each master blueprint, and special case" do
146
126
  Pickle::Adapter::Machinist.should_receive(:new).with(@klass1, :master).once
147
127
  Pickle::Adapter::Machinist.should_receive(:new).with(@klass3, :master).once
148
128
  Pickle::Adapter::Machinist.should_receive(:new).with(@klass3, :special).once
149
129
  Pickle::Adapter::Machinist.factories
150
130
  end
151
-
131
+
152
132
  describe ".new(Class, :master)" do
153
133
  before do
154
134
  @factory = Pickle::Adapter::Machinist.new(@klass1, :master)
155
135
  end
156
-
136
+
157
137
  it "should have underscored (s/_) name of Class as #name" do
158
138
  @factory.name.should == 'one'
159
139
  end
160
-
140
+
161
141
  it "#create(attrs) should call Class.make(:master, attrs)" do
162
142
  @klass1.should_receive(:make).with(:master, {:key => "val"})
163
143
  @factory.create(:key => "val")
164
144
  end
165
145
  end
166
-
146
+
167
147
  describe ".new(Class, :special)" do
168
148
  before do
169
149
  @factory = Pickle::Adapter::Machinist.new(@klass3, :special)
170
150
  end
171
-
151
+
172
152
  it "should have 'special_<Class name>' as #name" do
173
153
  @factory.name.should == 'special_three'
174
154
  end
175
-
155
+
176
156
  it "#create(attrs) should call Class.make(:special, attrs)" do
177
157
  @klass3.should_receive(:make).with(:special, {:key => "val"})
178
158
  @factory.create(:key => "val")
@@ -180,4 +160,4 @@ describe Pickle::Adapter do
180
160
  end
181
161
  end
182
162
  end
183
- end
163
+ end
@@ -4,27 +4,27 @@ describe Pickle::Config do
4
4
  before do
5
5
  @config = Pickle::Config.new
6
6
  end
7
-
7
+
8
8
  it "#adapters should default to :machinist, :factory_girl, :active_record" do
9
9
  @config.adapters.should == [:machinist, :factory_girl, :active_record]
10
10
  end
11
-
11
+
12
12
  it "#adapter_classes should default to Adapter::Machinist, Adapter::FactoryGirl, Adapter::ActiveRecord" do
13
13
  @config.adapter_classes.should == [Pickle::Adapter::Machinist, Pickle::Adapter::FactoryGirl, Pickle::Adapter::ActiveRecord]
14
14
  end
15
-
15
+
16
16
  describe "setting adapters to [:machinist, SomeAdapter]" do
17
17
  class SomeAdapter; end
18
-
18
+
19
19
  before do
20
20
  @config.adapters = [:machinist, SomeAdapter]
21
21
  end
22
-
22
+
23
23
  it "#adapter_classes should be Adapter::Machinist, SomeAdapter" do
24
24
  @config.adapter_classes.should == [Pickle::Adapter::Machinist, SomeAdapter]
25
25
  end
26
26
  end
27
-
27
+
28
28
  describe "#factories" do
29
29
  it "should call adaptor.factories for each adaptor" do
30
30
  Pickle::Adapter::Machinist.should_receive(:factories).and_return([])
@@ -32,14 +32,14 @@ describe Pickle::Config do
32
32
  Pickle::Adapter::ActiveRecord.should_receive(:factories).and_return([])
33
33
  @config.factories
34
34
  end
35
-
35
+
36
36
  it "should aggregate factories into a hash using factory name as key" do
37
37
  Pickle::Adapter::Machinist.should_receive(:factories).and_return([@machinist = mock('machinist', :name => 'machinist')])
38
38
  Pickle::Adapter::FactoryGirl.should_receive(:factories).and_return([@factory_girl = mock('factory_girl', :name => 'factory_girl')])
39
39
  Pickle::Adapter::ActiveRecord.should_receive(:factories).and_return([@active_record = mock('active_record', :name => 'active_record')])
40
40
  @config.factories.should == {'machinist' => @machinist, 'factory_girl' => @factory_girl, 'active_record' => @active_record}
41
41
  end
42
-
42
+
43
43
  it "should give preference to adaptors first in the list" do
44
44
  Pickle::Adapter::Machinist.should_receive(:factories).and_return([@machinist_one = mock('one', :name => 'one')])
45
45
  Pickle::Adapter::FactoryGirl.should_receive(:factories).and_return([@factory_girl_one = mock('one', :name => 'one'), @factory_girl_two = mock('two', :name => 'two')])
@@ -47,51 +47,59 @@ describe Pickle::Config do
47
47
  @config.factories.should == {'one' => @machinist_one, 'two' => @factory_girl_two, 'three' => @active_record_three}
48
48
  end
49
49
  end
50
-
50
+
51
51
  it "#mappings should default to []" do
52
52
  @config.mappings.should == []
53
53
  end
54
54
 
55
55
  describe '#predicates' do
56
56
  it "should be list of all non object ? public instance methods + columns methods of Adapter.model_classes" do
57
- class1 = mock('Class1', :public_instance_methods => ['nope', 'foo?', 'bar?'], :column_names => ['one', 'two'])
58
- class2 = mock('Class2', :public_instance_methods => ['not', 'foo?', 'faz?'], :column_names => ['two', 'three'])
57
+ class1 = mock('Class1',
58
+ :public_instance_methods => ['nope', 'foo?', 'bar?'],
59
+ :column_names => ['one', 'two'],
60
+ :const_get => ::ActiveRecord::Base::PickleAdapter
61
+ )
62
+ class2 = mock('Class2',
63
+ :public_instance_methods => ['not', 'foo?', 'faz?'],
64
+ :column_names => ['two', 'three'],
65
+ :const_get => ::ActiveRecord::Base::PickleAdapter
66
+ )
59
67
  Pickle::Adapter.stub!(:model_classes).and_return([class1, class2])
60
-
68
+
61
69
  @config.predicates.to_set.should == ['foo?', 'faz?', 'bar?', 'one', 'two', 'three'].to_set
62
70
  end
63
-
71
+
64
72
  it "should be overridable" do
65
73
  @config.predicates = %w(lame?)
66
74
  @config.predicates.should == %w(lame?)
67
75
  end
68
76
  end
69
-
77
+
70
78
  describe "#map 'foo', :to => 'faz'" do
71
79
  before do
72
80
  @config.map 'foo', :to => 'faz'
73
81
  end
74
-
82
+
75
83
  it "should create OpenStruct(search: 'foo', replace: 'faz') mapping" do
76
84
  @config.mappings.first.should == OpenStruct.new(:search => 'foo', :replacement => 'faz')
77
85
  end
78
86
  end
79
-
87
+
80
88
  describe "#map 'foo', 'bar' :to => 'faz'" do
81
89
  before do
82
90
  @config.map 'foo', 'bar', :to => 'faz'
83
91
  end
84
-
92
+
85
93
  it "should create 2 mappings" do
86
94
  @config.mappings.first.should == OpenStruct.new(:search => 'foo', :replacement => 'faz')
87
95
  @config.mappings.last.should == OpenStruct.new(:search => 'bar', :replacement => 'faz')
88
96
  end
89
97
  end
90
-
98
+
91
99
  it "#configure(&block) should execiute on self" do
92
100
  @config.should_receive(:foo).with(:bar)
93
101
  @config.configure do |c|
94
102
  c.foo :bar
95
103
  end
96
104
  end
97
- end
105
+ end