roart 0.1.7 → 0.1.8

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.
data/History.txt CHANGED
@@ -1,5 +1,12 @@
1
- ==0.1.7 / 2010-30-11
1
+ ==0.1.8 / 2010-03-16
2
+ Added validate presence of.
3
+ Updated specs to test validations.
4
+ No Longer depend on mechanize version 0.9.3 and get rid of those pesky deprecation warnings.
5
+ The connection adapter classes are now suffixed with "Adapter", so Mechanize is now MechanizeAdapter. You still specify it with :adapter => 'mechanize', however.
6
+
7
+ ==0.1.7 / 2010-03-11
2
8
  Added some new error classes, add removed any extraneous raise 'text' (via jrbingham)
9
+ Removed debugging puts from the ticket file.
3
10
 
4
11
  ==0.1.6 / 2010-03-09
5
12
  Updated the search feature to pull all the ticket information at the same time, instead of only loading the id and subject.
data/Rakefile CHANGED
@@ -28,7 +28,7 @@ PROJ.rubyforge.name = 'roart'
28
28
  PROJ.exclude = %w(.git pkg coverage)
29
29
  PROJ.description = "Interface for working with Request Tracker (RT) tickets inspired by ActiveRecord."
30
30
  PROJ.rdoc.main = 'README.rdoc'
31
- depend_on 'mechanize'
31
+ depend_on 'mechanize' #this will go away when a stdlib adapter gets written
32
32
 
33
33
  PROJ.spec.opts << '--color'
34
34
 
data/lib/roart.rb CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  module Roart
3
3
 
4
4
  # :stopdoc:
5
- VERSION = '0.1.7'
5
+ VERSION = '0.1.8'
6
6
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
7
7
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
8
8
  # :startdoc:
@@ -1,5 +1,4 @@
1
1
  require 'yaml'
2
- require 'mechanize'
3
2
 
4
3
  module Roart
5
4
 
@@ -6,7 +6,7 @@ module Roart
6
6
  extend Forwardable
7
7
 
8
8
  def initialize(config)
9
- @adapter = Roart::ConnectionAdapters.const_get(config[:adapter].capitalize).new(config)
9
+ @adapter = Roart::ConnectionAdapters.const_get(config[:adapter].capitalize + "Adapter").new(config)
10
10
  @adapter.login(config) if config[:user] && config[:pass]
11
11
  end
12
12
 
@@ -1,6 +1,8 @@
1
+ require 'mechanize'
2
+
1
3
  module Roart
2
4
  module ConnectionAdapters
3
- class Mechanize
5
+ class MechanizeAdapter
4
6
 
5
7
  def initialize(config)
6
8
  @conf = config
@@ -8,7 +10,7 @@ module Roart
8
10
 
9
11
  def login(config)
10
12
  @conf.merge!(config)
11
- agent = WWW::Mechanize.new
13
+ agent = Mechanize.new
12
14
  page = agent.get(@conf[:server])
13
15
  form = page.form('login')
14
16
  form.user = @conf[:user]
@@ -70,6 +70,7 @@ module Roart
70
70
  end
71
71
 
72
72
  def validate(obj)
73
+ obj.errors.clear
73
74
  @validators.each{|validator| validator.call(obj)}
74
75
  end
75
76
 
@@ -80,14 +81,21 @@ module Roart
80
81
  ALL_RANGE_OPTIONS = [ :is, :within, :in, :minimum, :min, :maximum, :max ].freeze
81
82
  ALL_NUMERICALITY_CHECKS = { :greater_than => '>', :greater_than_or_equal_to => '>=',
82
83
  :equal_to => '==', :less_than => '<', :less_than_or_equal_to => '<=',
83
- :odd => 'odd?', :even => 'even?' }.freeze
84
+ :odd => 'odd?', :even => 'even?', :only_integer => 'is_a?' }.freeze
84
85
 
85
86
  def validator
86
87
  @validator ||= Validators.new
87
88
  end
88
89
 
89
90
  def validates_presence_of(*args)
90
-
91
+ args.each do |field|
92
+ validator_proc = lambda do |obj|
93
+ if obj.send(field.to_sym).nil? || obj.send(field.to_sym).blank?
94
+ obj.errors.add(field.to_sym, "Can't Be Blank")
95
+ end
96
+ end
97
+ self.validator.add(validator_proc)
98
+ end
91
99
  end
92
100
 
93
101
  def validates_format_of(*args)
@@ -165,35 +173,37 @@ module Roart
165
173
 
166
174
  def validates_numericality_of(*args)
167
175
  options = args.last.is_a?(Hash) ? args.pop : {}
168
- numericality_options = ALL_NUMERICALITY_CHECKS & options.keys
169
-
170
- case numericality_options
171
- when 0
172
- raise ArgumentError, "Options Unspecified. Specify an option to use."
173
- when 1
174
- #continue
175
- else
176
- raise ArgumentError, "Too many options specified"
177
- end
178
-
179
- option = numericality_options.first
180
- option_value = options[numericality_options.first]
181
- key = {:is => :wrong_length, :minimum => :too_short, :maximum => :too_long}[option]
182
- custom_message = options[:message] || options[key]
183
-
176
+ numericality_options = ALL_NUMERICALITY_CHECKS.keys & options.keys
184
177
  args.each do |field|
185
178
  numericality_options.each do |option|
186
- case option
187
- when :odd, :even
188
- unless raw_value.to_i.method(ALL_NUMERICALITY_CHECKS[option])[]
189
- record.errors.add(attr_name, option, :value => raw_value, :default => configuration[:message])
179
+ validator_proc = case option
180
+ when :only_integer
181
+ lambda do |obj|
182
+ unless obj.send(field.to_sym).send(ALL_NUMERICALITY_CHECKS[option], Integer )
183
+ obj.errors.add(field.to_sym, "Must be #{ALL_NUMERICALITY_CHECKS[option]}.")
184
+ end
185
+ end
186
+ when :even, :odd
187
+ lambda do |obj|
188
+ if obj.send(field.to_sym).send("is_a?".to_sym, Integer) == true
189
+ unless obj.send(field.to_sym).send(ALL_NUMERICALITY_CHECKS[option] )
190
+ obj.errors.add(field.to_sym, "Must be #{ALL_NUMERICALITY_CHECKS[option]}.")
191
+ end
192
+ else
193
+ obj.errors.add(field.to_sym, "Must be an #{option} Integer.")
190
194
  end
191
- else
192
- record.errors.add(attr_name, option, :default => configuration[:message], :value => raw_value, :count => configuration[option]) unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]]
195
+ end
196
+ else
197
+ raise ArgumentError, ":#{option} must be a number" unless options[option].is_a?(Numeric)
198
+ lambda do |obj|
199
+ unless obj.send(field.to_sym).send(ALL_NUMERICALITY_CHECKS[option], options[option] )
200
+ obj.errors.add(field.to_sym, "Must be #{ALL_NUMERICALITY_CHECKS[option]}.")
201
+ end
202
+ end
193
203
  end
204
+ self.validator.add(validator_proc)
194
205
  end
195
206
  end
196
-
197
207
  end
198
208
 
199
209
  end
data/roart.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{roart}
5
- s.version = "0.1.7"
5
+ s.version = "0.1.8"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["PJ Davis"]
9
- s.date = %q{2010-03-11}
9
+ s.date = %q{2010-03-17}
10
10
  s.description = %q{Interface for working with Request Tracker (RT) tickets inspired by ActiveRecord.}
11
11
  s.email = %q{pj.davis@gmail.com}
12
12
  s.extra_rdoc_files = ["History.txt", "README.rdoc", "spec/test_data/full_history.txt", "spec/test_data/search_ticket.txt", "spec/test_data/single_history.txt", "spec/test_data/ticket.txt"]
13
- s.files = ["History.txt", "README.rdoc", "Rakefile", "lib/roart.rb", "lib/roart/callbacks.rb", "lib/roart/connection.rb", "lib/roart/connection_adapter.rb", "lib/roart/connection_adapters/mechanize_adapter.rb", "lib/roart/core/array.rb", "lib/roart/core/hash.rb", "lib/roart/core/hash/indifferent_access.rb", "lib/roart/core/string.rb", "lib/roart/errors.rb", "lib/roart/history.rb", "lib/roart/roart.rb", "lib/roart/ticket.rb", "lib/roart/ticket_page.rb", "lib/roart/validations.rb", "roart.gemspec", "spec/roart/callbacks_spec.rb", "spec/roart/connection_adapter_spec.rb", "spec/roart/connection_spec.rb", "spec/roart/core/array_spec.rb", "spec/roart/core/hash_spec.rb", "spec/roart/core/string_spec.rb", "spec/roart/history_spec.rb", "spec/roart/roart_spec.rb", "spec/roart/ticket_page_spec.rb", "spec/roart/ticket_spec.rb", "spec/roart_spec.rb", "spec/spec_helper.rb", "spec/test_data/full_history.txt", "spec/test_data/search_ticket.txt", "spec/test_data/single_history.txt", "spec/test_data/ticket.txt"]
13
+ s.files = ["History.txt", "README.rdoc", "Rakefile", "lib/roart.rb", "lib/roart/callbacks.rb", "lib/roart/connection.rb", "lib/roart/connection_adapter.rb", "lib/roart/connection_adapters/mechanize_adapter.rb", "lib/roart/core/array.rb", "lib/roart/core/hash.rb", "lib/roart/core/hash/indifferent_access.rb", "lib/roart/core/string.rb", "lib/roart/errors.rb", "lib/roart/history.rb", "lib/roart/roart.rb", "lib/roart/ticket.rb", "lib/roart/ticket_page.rb", "lib/roart/validations.rb", "roart.gemspec", "spec/roart/callbacks_spec.rb", "spec/roart/connection_adapter_spec.rb", "spec/roart/connection_spec.rb", "spec/roart/core/array_spec.rb", "spec/roart/core/hash_spec.rb", "spec/roart/core/string_spec.rb", "spec/roart/history_spec.rb", "spec/roart/roart_spec.rb", "spec/roart/ticket_page_spec.rb", "spec/roart/ticket_spec.rb", "spec/roart/validation_spec.rb", "spec/roart_spec.rb", "spec/spec_helper.rb", "spec/test_data/full_history.txt", "spec/test_data/search_ticket.txt", "spec/test_data/single_history.txt", "spec/test_data/ticket.txt"]
14
14
  s.homepage = %q{http://github.com/pjdavis/roart}
15
15
  s.rdoc_options = ["--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
@@ -23,14 +23,14 @@ Gem::Specification.new do |s|
23
23
  s.specification_version = 3
24
24
 
25
25
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
- s.add_runtime_dependency(%q<mechanize>, [">= 0.9.3"])
26
+ s.add_runtime_dependency(%q<mechanize>, [">= 1.0.0"])
27
27
  s.add_development_dependency(%q<bones>, [">= 2.5.1"])
28
28
  else
29
- s.add_dependency(%q<mechanize>, [">= 0.9.3"])
29
+ s.add_dependency(%q<mechanize>, [">= 1.0.0"])
30
30
  s.add_dependency(%q<bones>, [">= 2.5.1"])
31
31
  end
32
32
  else
33
- s.add_dependency(%q<mechanize>, [">= 0.9.3"])
33
+ s.add_dependency(%q<mechanize>, [">= 1.0.0"])
34
34
  s.add_dependency(%q<bones>, [">= 2.5.1"])
35
35
  end
36
36
  end
@@ -4,7 +4,7 @@ require File.join(File.dirname(__FILE__), %w[ .. spec_helper])
4
4
  describe "ConnectionAdapter" do
5
5
 
6
6
  it 'should give us back a connection' do
7
- Roart::ConnectionAdapters::Mechanize.should_receive(:new).with(:adapter => 'mechanize').and_return(mock('mechanize'))
7
+ Roart::ConnectionAdapters::MechanizeAdapter.should_receive(:new).with(:adapter => 'mechanize').and_return(mock('mechanize'))
8
8
  Roart::ConnectionAdapter.new(:adapter => 'mechanize')
9
9
  end
10
10
 
@@ -40,7 +40,7 @@ describe "Connection" do
40
40
  it 'should give us back the whole thing' do
41
41
  mock_mech = mock('mech')
42
42
  @options = {:server => 'server', :user => 'user', :pass => 'pass', :adapter => 'mechanize'}
43
- Roart::ConnectionAdapters::Mechanize.should_receive(:new).with(@options).and_return(mock_mech)
43
+ Roart::ConnectionAdapters::MechanizeAdapter.should_receive(:new).with(@options).and_return(mock_mech)
44
44
  mock_mech.should_receive(:login).with(@options)
45
45
  mock_mech.should_receive(:get).with('uri').and_return('body')
46
46
  connection = Roart::Connection.new(@options)
@@ -0,0 +1,152 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "validations" do
4
+
5
+ describe "field length" do
6
+
7
+ it "show validate too short" do
8
+ class ShortTicket < Roart::Ticket; validates_length_of(:subject, :min => 6); end
9
+ ticket = inst_ticket(ShortTicket)
10
+ ticket.subject = "short"
11
+ ticket.valid?.should be_false
12
+ ticket.subject = "longer subject"
13
+ ticket.valid?.should be_true
14
+ end
15
+
16
+ it "should validate too long" do
17
+ class LongTicket < Roart::Ticket; validates_length_of(:subject, :max => 6); end
18
+ ticket = inst_ticket(LongTicket)
19
+ ticket.subject = "too long"
20
+ ticket.valid?.should be_false
21
+ ticket.subject = "short"
22
+ ticket.valid?.should be_true
23
+ end
24
+
25
+ it "should validate exact" do
26
+ class ExactTicket < Roart::Ticket; validates_length_of(:subject, :is => 5); end
27
+ ticket = inst_ticket(ExactTicket)
28
+ ticket.subject = "Not Five"
29
+ ticket.valid?.should be_false
30
+ ticket.subject = "Yes 5"
31
+ ticket.valid?.should be_true
32
+ end
33
+
34
+ it "should validate range" do
35
+ class RangeTicket < Roart::Ticket; validates_length_of(:subject, :within => 7..12); end
36
+ ticket = inst_ticket(RangeTicket)
37
+ ticket.subject = 'short'
38
+ ticket.valid?.should be_false
39
+ ticket.subject = 'waaaaay toooooo long'
40
+ ticket.valid?.should be_false
41
+ ticket.subject = 'just right'
42
+ ticket.valid?.should be_true
43
+ end
44
+
45
+ end
46
+
47
+ describe "format" do
48
+
49
+ it "should validate format" do
50
+ class FormatTicket < Roart::Ticket; validates_format_of(:subject, :format => /^lol$/); end
51
+ ticket = inst_ticket(FormatTicket)
52
+ ticket.subject = 'poop'
53
+ ticket.valid?.should be_false
54
+ ticket.subject = 'lol'
55
+ ticket.valid?.should be_true
56
+ end
57
+
58
+ end
59
+
60
+ describe "presence" do
61
+
62
+ it "should validate presence of" do
63
+ class PresenceTicket < Roart::Ticket; validates_presence_of(:subject); end
64
+ ticket = inst_ticket(PresenceTicket)
65
+ ticket.subject = ""
66
+ ticket.valid?.should be_false
67
+ ticket.subject = "I'M HERE!"
68
+ ticket.valid?.should be_true
69
+ end
70
+
71
+ it "should validate presence of" do
72
+ class PresenceTicket < Roart::Ticket; validates_presence_of(:subject); end
73
+ ticket = inst_ticket(PresenceTicket)
74
+ ticket.subject = nil
75
+ ticket.valid?.should be_false
76
+ ticket.subject = "I'M HERE!"
77
+ ticket.valid?.should be_true
78
+ end
79
+
80
+ end
81
+
82
+ describe "numericality" do
83
+
84
+ it "should validate greater than" do
85
+ class GTTicket < Roart::Ticket; validates_numericality_of(:subject, :greater_than => 5); end
86
+ ticket = inst_ticket(GTTicket)
87
+ ticket.subject = 4
88
+ ticket.valid?.should be_false
89
+ ticket.subject = 6
90
+ ticket.valid?.should be_true
91
+ end
92
+
93
+ it "should validate less than" do
94
+ class LTTicket < Roart::Ticket; validates_numericality_of(:subject, :less_than => 5); end
95
+ ticket = inst_ticket(LTTicket)
96
+ ticket.subject = 6
97
+ ticket.valid?.should be_false
98
+ ticket.subject = 4
99
+ ticket.valid?.should be_true
100
+ end
101
+
102
+ it "should validate integer" do
103
+ class IntTicket < Roart::Ticket; validates_numericality_of(:subject, :only_integer => true); end
104
+ ticket = inst_ticket(IntTicket)
105
+ ticket.subject = 6.3
106
+ ticket.valid?.should be_false
107
+ ticket.subject = 4
108
+ ticket.valid?.should be_true
109
+ end
110
+
111
+ it "should validate integer" do
112
+ class EqualTicket < Roart::Ticket; validates_numericality_of(:subject, :equal_to => 4); end
113
+ ticket = inst_ticket(EqualTicket)
114
+ ticket.subject = 6.3
115
+ ticket.valid?.should be_false
116
+ ticket.subject = 4
117
+ ticket.valid?.should be_true
118
+ end
119
+
120
+ it "should validate even" do
121
+ class EvenTicket < Roart::Ticket; validates_numericality_of(:subject, :even => true); end
122
+ ticket = inst_ticket(EvenTicket)
123
+ ticket.subject = 6.3
124
+ ticket.valid?.should be_false
125
+ ticket.subject = 5
126
+ ticket.valid?.should be_false
127
+ ticket.subject = 4
128
+ ticket.valid?.should be_true
129
+ end
130
+
131
+ it "should validate two at once" do
132
+ class DoubleTeam < Roart::Ticket; validates_numericality_of(:subject, :even => true, :greater_than => 5); end
133
+ ticket = inst_ticket(DoubleTeam)
134
+ ticket.subject = 6.3
135
+ ticket.valid?.should be_false
136
+ ticket.subject = 9
137
+ ticket.valid?.should be_false
138
+ ticket.subject = 4
139
+ ticket.valid?.should be_false
140
+ ticket.subject = 8
141
+ ticket.valid?.should be_true
142
+ end
143
+
144
+ end
145
+
146
+ end
147
+
148
+ #helpers
149
+
150
+ def inst_ticket(klass)
151
+ klass.send(:instantiate,{:subject => 'A New Ticket', :queue => 'My Queue', :id => 1})
152
+ end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 8
9
+ version: 0.1.8
5
10
  platform: ruby
6
11
  authors:
7
12
  - PJ Davis
@@ -9,29 +14,37 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-11 00:00:00 -06:00
17
+ date: 2010-03-17 00:00:00 -05:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: mechanize
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
23
- version: 0.9.3
24
- version:
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 0
31
+ version: 1.0.0
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: bones
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ">="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 5
44
+ - 1
33
45
  version: 2.5.1
34
- version:
46
+ type: :development
47
+ version_requirements: *id002
35
48
  description: Interface for working with Request Tracker (RT) tickets inspired by ActiveRecord.
36
49
  email: pj.davis@gmail.com
37
50
  executables: []
@@ -75,6 +88,7 @@ files:
75
88
  - spec/roart/roart_spec.rb
76
89
  - spec/roart/ticket_page_spec.rb
77
90
  - spec/roart/ticket_spec.rb
91
+ - spec/roart/validation_spec.rb
78
92
  - spec/roart_spec.rb
79
93
  - spec/spec_helper.rb
80
94
  - spec/test_data/full_history.txt
@@ -95,18 +109,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
109
  requirements:
96
110
  - - ">="
97
111
  - !ruby/object:Gem::Version
112
+ segments:
113
+ - 0
98
114
  version: "0"
99
- version:
100
115
  required_rubygems_version: !ruby/object:Gem::Requirement
101
116
  requirements:
102
117
  - - ">="
103
118
  - !ruby/object:Gem::Version
119
+ segments:
120
+ - 0
104
121
  version: "0"
105
- version:
106
122
  requirements: []
107
123
 
108
124
  rubyforge_project: roart
109
- rubygems_version: 1.3.5
125
+ rubygems_version: 1.3.6
110
126
  signing_key:
111
127
  specification_version: 3
112
128
  summary: Interface for working with Request Tracker (RT) tickets inspired by ActiveRecord