surveyor 0.8.0 → 0.9.0

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 (45) hide show
  1. data/README.md +22 -5
  2. data/VERSION +1 -1
  3. data/app/models/dependency.rb +16 -32
  4. data/app/models/question.rb +1 -1
  5. data/app/models/question_group.rb +1 -1
  6. data/app/models/response.rb +1 -4
  7. data/app/models/response_set.rb +4 -3
  8. data/app/models/survey.rb +0 -1
  9. data/app/models/validation.rb +20 -0
  10. data/app/models/validation_condition.rb +19 -17
  11. data/generators/surveyor/surveyor_generator.rb +9 -5
  12. data/generators/surveyor/templates/migrate/add_display_order_to_surveys.rb +9 -0
  13. data/generators/surveyor/templates/surveys/kitchen_sink_survey.rb +15 -4
  14. data/lib/surveyor.rb +16 -0
  15. data/script/surveyor/answer.rb +32 -55
  16. data/script/surveyor/base.rb +61 -0
  17. data/script/surveyor/dependency.rb +4 -41
  18. data/script/surveyor/dependency_condition.rb +13 -38
  19. data/script/surveyor/question.rb +16 -50
  20. data/script/surveyor/question_group.rb +7 -19
  21. data/script/surveyor/specs/answer_spec.rb +15 -52
  22. data/script/surveyor/specs/question_spec.rb +37 -85
  23. data/script/surveyor/specs/spec_helper.rb +6 -0
  24. data/script/surveyor/specs/survey_section_spec.rb +23 -0
  25. data/script/surveyor/specs/validation_condition_spec.rb +20 -0
  26. data/script/surveyor/specs/validation_spec.rb +20 -0
  27. data/script/surveyor/survey.rb +10 -85
  28. data/script/surveyor/survey_parser.rb +160 -38
  29. data/script/surveyor/survey_section.rb +6 -130
  30. data/script/surveyor/validation.rb +19 -0
  31. data/script/surveyor/validation_condition.rb +19 -0
  32. data/spec/lib/surveyor_spec.rb +44 -0
  33. data/spec/models/dependency_spec.rb +9 -16
  34. data/spec/models/question_group_spec.rb +3 -3
  35. data/spec/models/question_spec.rb +1 -1
  36. data/spec/models/validation_condition_spec.rb +29 -0
  37. data/spec/models/validation_spec.rb +27 -0
  38. data/spec/spec_helper.rb +0 -2
  39. data/surveyor.gemspec +12 -7
  40. metadata +12 -7
  41. data/lib/tiny_code.rb +0 -58
  42. data/script/surveyor/columnizer.rb +0 -36
  43. data/script/surveyor/specs/question_dependency_spec.rb +0 -46
  44. data/script/surveyor/specs/question_group_spec.rb +0 -9
  45. data/script/surveyor/specs/section_spec.rb +0 -58
@@ -0,0 +1,19 @@
1
+ class Validation < SurveyParser::Base
2
+
3
+ # Context, Conditional, Children
4
+ attr_accessor :id, :answer_id, :parser
5
+ attr_accessor :rule, :message
6
+ has_children :validation_conditions
7
+
8
+
9
+ def default_options
10
+ {:rule => "A"}
11
+ end
12
+ def parse_args(args)
13
+ args[0] || {}
14
+ end
15
+ def parse_opts(opts)
16
+ {} # toss the method name and reference identifier by default
17
+ end
18
+
19
+ end
@@ -0,0 +1,19 @@
1
+ class ValidationCondition < SurveyParser::Base
2
+ # Context, Conditional, Value, Reference
3
+ attr_accessor :id, :validation_id, :rule_key, :parser
4
+ attr_accessor :operator
5
+ attr_accessor :question_id, :answer_id, :datetime_value, :integer_value, :float_value, :unit, :text_value, :string_value, :response_other, :regexp
6
+ attr_accessor :question_reference, :answer_reference
7
+
8
+ def default_options
9
+ { :operator => "==" }
10
+ end
11
+ def parse_args(args)
12
+ a0, a1 = args
13
+ {:operator => a0}.merge(a1 || {})
14
+ end
15
+ def parse_opts(opts)
16
+ {:rule_key => opts[:reference_identifier]}
17
+ end
18
+
19
+ end
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../lib/surveyor')
2
+
3
+ describe Surveyor do
4
+ it "should create a normalized code from the answer text" do
5
+ # The answer object should take the title of the text and convert it to a code that is more appropirate for a database entry
6
+
7
+
8
+
9
+
10
+ # Taking a few answers from the survey for testing
11
+ strings = [ "This? is a in - t3rrible-@nswer of! (question) on",
12
+ "Private insurance/ HMO/ PPO",
13
+ "<bold>VA</bold>",
14
+ "PMS (Premenstrual syndrome)/ PMDD (Premenstrual Dysphoric Disorder)",
15
+ "Have never been employed outside the home",
16
+ "Professional",
17
+ "Not working because of temporary disability, but expect to return to a job",
18
+ "How long has it been since you last visited a doctor for a routine checkup (routine being not for a particular reason)?",
19
+ "Do you take medications as directed?",
20
+ "Do you every leak urine (or) water when you didn't want to?", #checking for () and ' removal
21
+ "Do your biological family members (not adopted) have a \"history\" of any of the following?",
22
+ "Your health:",
23
+ "In general, you would say your health is:" ]
24
+
25
+ # What the results should look like
26
+ codes = [ "this_t3rrible_nswer",
27
+ "private_insurance_hmo_ppo",
28
+ "va",
29
+ "pms_pmdd",
30
+ "never_been_employed_outside_home",
31
+ "professional",
32
+ "temporary_disability_expect_return_job",
33
+ "visited_doctor_for_routine_checkup",
34
+ "you_take_medications_as_directed",
35
+ "urine_water_you_didnt_want",
36
+ "family_members_history_any_following",
37
+ "your_health",
38
+ "you_would_say_your_health" ]
39
+
40
+ strings.each_with_index do |s, i|
41
+ Surveyor.to_normalized_string(s).should == codes[i]
42
+ end
43
+ end
44
+ end
@@ -65,24 +65,17 @@ describe Dependency, "when evaluating dependency conditions of a question in a r
65
65
  end
66
66
 
67
67
  it "knows if the dependencies are met" do
68
- @dep.met?(@response_set).should be_true
69
- @dep2.met?(@response_set).should be_false
70
- @dep3.met?(@response_set).should be_true
71
- @dep4.met?(@response_set).should be_true
68
+ @dep.is_met?(@response_set).should be_true
69
+ @dep2.is_met?(@response_set).should be_false
70
+ @dep3.is_met?(@response_set).should be_true
71
+ @dep4.is_met?(@response_set).should be_true
72
72
  end
73
73
 
74
74
  it "returns the proper keyed pairs from the dependency conditions" do
75
- @dep.keyed_conditions(@response_set).should == {:A => true}
76
- @dep2.keyed_conditions(@response_set).should == {:A => true, :B => false}
77
- @dep3.keyed_conditions(@response_set).should == {:A => true, :B => false}
78
- @dep4.keyed_conditions(@response_set).should == {:A => true, :B => false, :C => true}
75
+ @dep.conditions_hash(@response_set).should == {:A => true}
76
+ @dep2.conditions_hash(@response_set).should == {:A => true, :B => false}
77
+ @dep3.conditions_hash(@response_set).should == {:A => true, :B => false}
78
+ @dep4.conditions_hash(@response_set).should == {:A => true, :B => false, :C => true}
79
79
  end
80
-
81
- it "evaluates the rule from the keyed pairs and return a boolean value" do
82
- @dep.rule_evaluation(@dep.keyed_conditions(@response_set)).should be_true
83
- @dep2.rule_evaluation(@dep2.keyed_conditions(@response_set)).should be_false
84
- @dep3.rule_evaluation(@dep3.keyed_conditions(@response_set)).should be_true
85
- @dep4.rule_evaluation(@dep4.keyed_conditions(@response_set)).should be_true
86
- end
87
-
80
+
88
81
  end
@@ -22,14 +22,14 @@ describe QuestionGroup do
22
22
  it "should return its dependency class" do
23
23
  @dependency = Factory(:dependency)
24
24
  @question_group.dependency = @dependency
25
- @dependency.should_receive(:met?).and_return(true)
25
+ @dependency.should_receive(:is_met?).and_return(true)
26
26
  @question_group.css_class(Factory(:response_set)).should == "dependent"
27
27
 
28
- @dependency.should_receive(:met?).and_return(false)
28
+ @dependency.should_receive(:is_met?).and_return(false)
29
29
  @question_group.css_class(Factory(:response_set)).should == "dependent hidden"
30
30
 
31
31
  @question_group.custom_class = "foo bar"
32
- @dependency.should_receive(:met?).and_return(false)
32
+ @dependency.should_receive(:is_met?).and_return(false)
33
33
  @question_group.css_class(Factory(:response_set)).should == "dependent hidden foo bar"
34
34
  end
35
35
  end
@@ -67,7 +67,7 @@ describe Question, "with dependencies" do
67
67
 
68
68
  it "should check its dependency" do
69
69
  @dependency = mock_model(Dependency)
70
- @dependency.stub!(:met?).with(@rs).and_return(true)
70
+ @dependency.stub!(:is_met?).with(@rs).and_return(true)
71
71
  @question.stub!(:dependency).and_return(@dependency)
72
72
  @question.triggered?(@rs).should == true
73
73
  end
@@ -51,3 +51,32 @@ describe ValidationCondition do
51
51
  end
52
52
 
53
53
  end
54
+
55
+ describe ValidationCondition, "validating responses" do
56
+ def test_var(vhash, ahash, rhash)
57
+ v = Factory(:validation_condition, vhash)
58
+ a = Factory(:answer, ahash)
59
+ r = Factory(:response, rhash.merge(:answer => a, :question => a.question))
60
+ return v.is_valid?(r)
61
+ end
62
+
63
+ it "should validate a response by regexp" do
64
+ test_var({:operator => "=~", :regexp => /^[a-z]{1,6}$/}, {:response_class => "string"}, {:string_value => "clear"}).should be_true
65
+ test_var({:operator => "=~", :regexp => /^[a-z]{1,6}$/}, {:response_class => "string"}, {:string_value => "foobarbaz"}).should be_false
66
+ end
67
+ it "should validate a response by integer comparison" do
68
+ test_var({:operator => ">", :integer_value => 3}, {:response_class => "integer"}, {:integer_value => 4}).should be_true
69
+ test_var({:operator => "<=", :integer_value => 256}, {:response_class => "integer"}, {:integer_value => 512}).should be_false
70
+ end
71
+ it "should validate a response by (in)equality" do
72
+ test_var({:operator => "!=", :datetime_value => Date.today + 1}, {:response_class => "date"}, {:datetime_value => Date.today}).should be_true
73
+ test_var({:operator => "==", :answer_id => 2}, {:response_class => "answer"}, {:answer_id => 2}).should be_false
74
+ end
75
+ it "should represent itself as a hash" do
76
+ @v = Factory(:validation_condition, :rule_key => "A")
77
+ @v.stub!(:is_valid?).and_return(true)
78
+ @v.to_hash("foo").should == {:A => true}
79
+ @v.stub!(:is_valid?).and_return(false)
80
+ @v.to_hash("foo").should == {:A => false}
81
+ end
82
+ end
@@ -30,3 +30,30 @@ describe Validation do
30
30
  @validation.should have(1).error_on(:rule)
31
31
  end
32
32
  end
33
+ describe Validation, "reporting its status" do
34
+ def test_var(vhash, vchashes, ahash, rhash)
35
+ a = Factory(:answer, ahash)
36
+ v = Factory(:validation, {:answer => a, :rule => "A"}.merge(vhash))
37
+ vchashes.each do |vchash|
38
+ Factory(:validation_condition, {:validation => v, :rule_key => "A"}.merge(vchash))
39
+ end
40
+ rs = Factory(:response_set)
41
+ r = Factory(:response, {:answer => a, :question => a.question}.merge(rhash))
42
+ rs.responses << r
43
+ return v.is_valid?(rs)
44
+ end
45
+
46
+ it "should validate a response by integer comparison" do
47
+ test_var({:rule => "A and B"}, [{:operator => ">=", :integer_value => 0}, {:rule_key => "B", :operator => "<=", :integer_value => 120}], {:response_class => "integer"}, {:integer_value => 48}).should be_true
48
+ end
49
+ it "should validate a response by regexp" do
50
+ test_var({}, [{:operator => "=~", :regexp => /^[a-z]{1,6}$/}], {:response_class => "string"}, {:string_value => ""}).should be_false
51
+ end
52
+ it "should validate a response by (in)equality" do
53
+ # test_var({:operator => "!=", :datetime_value => Date.today + 1}, {:response_class => "date"}, {:datetime_value => Date.today}).should be_true
54
+ # test_var({:operator => "==", :answer_id => 2}, {:response_class => "answer"}, {:answer_id => 2}).should be_false
55
+ end
56
+ it "should validate a response by lookup" do
57
+
58
+ end
59
+ end
data/spec/spec_helper.rb CHANGED
@@ -7,8 +7,6 @@ require File.dirname(__FILE__) + "/factories"
7
7
 
8
8
  $LOAD_PATH.unshift(File.dirname(__FILE__))
9
9
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
- # require 'surveyor'
11
10
 
12
11
  Spec::Runner.configure do |config|
13
-
14
12
  end
data/surveyor.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{surveyor}
8
- s.version = "0.8.0"
8
+ s.version = "0.9.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brian Chamberlain", "Mark Yoon"]
12
- s.date = %q{2009-10-23}
12
+ s.date = %q{2009-11-09}
13
13
  s.email = %q{yoon@northwestern.edu}
14
14
  s.extra_rdoc_files = [
15
15
  "README.md"
@@ -109,6 +109,7 @@ Gem::Specification.new do |s|
109
109
  "generators/surveyor/templates/assets/stylesheets/ui.theme.css",
110
110
  "generators/surveyor/templates/initializers/haml.rb",
111
111
  "generators/surveyor/templates/initializers/surveyor.rb",
112
+ "generators/surveyor/templates/migrate/add_display_order_to_surveys.rb",
112
113
  "generators/surveyor/templates/migrate/create_answers.rb",
113
114
  "generators/surveyor/templates/migrate/create_dependencies.rb",
114
115
  "generators/surveyor/templates/migrate/create_dependency_conditions.rb",
@@ -131,25 +132,28 @@ Gem::Specification.new do |s|
131
132
  "lib/surveyor/acts_as_response.rb",
132
133
  "lib/surveyor/config.rb",
133
134
  "lib/tasks/surveyor_tasks.rake",
134
- "lib/tiny_code.rb",
135
135
  "lib/xml_formatter.rb",
136
136
  "script/surveyor/answer.rb",
137
- "script/surveyor/columnizer.rb",
137
+ "script/surveyor/base.rb",
138
138
  "script/surveyor/dependency.rb",
139
139
  "script/surveyor/dependency_condition.rb",
140
140
  "script/surveyor/question.rb",
141
141
  "script/surveyor/question_group.rb",
142
142
  "script/surveyor/specs/answer_spec.rb",
143
- "script/surveyor/specs/question_dependency_spec.rb",
144
- "script/surveyor/specs/question_group_spec.rb",
145
143
  "script/surveyor/specs/question_spec.rb",
146
- "script/surveyor/specs/section_spec.rb",
144
+ "script/surveyor/specs/spec_helper.rb",
145
+ "script/surveyor/specs/survey_section_spec.rb",
146
+ "script/surveyor/specs/validation_condition_spec.rb",
147
+ "script/surveyor/specs/validation_spec.rb",
147
148
  "script/surveyor/survey.rb",
148
149
  "script/surveyor/survey_parser.rb",
149
150
  "script/surveyor/survey_section.rb",
151
+ "script/surveyor/validation.rb",
152
+ "script/surveyor/validation_condition.rb",
150
153
  "script/surveyor/whr_dsl.tmproj",
151
154
  "spec/controllers/surveyor_controller_spec.rb",
152
155
  "spec/factories.rb",
156
+ "spec/lib/surveyor_spec.rb",
153
157
  "spec/models/answer_spec.rb",
154
158
  "spec/models/dependency_condition_spec.rb",
155
159
  "spec/models/dependency_spec.rb",
@@ -175,6 +179,7 @@ Gem::Specification.new do |s|
175
179
  s.test_files = [
176
180
  "spec/controllers/surveyor_controller_spec.rb",
177
181
  "spec/factories.rb",
182
+ "spec/lib/surveyor_spec.rb",
178
183
  "spec/models/answer_spec.rb",
179
184
  "spec/models/dependency_condition_spec.rb",
180
185
  "spec/models/dependency_spec.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surveyor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Chamberlain
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-10-23 00:00:00 -05:00
13
+ date: 2009-11-09 00:00:00 -06:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -126,6 +126,7 @@ files:
126
126
  - generators/surveyor/templates/assets/stylesheets/ui.theme.css
127
127
  - generators/surveyor/templates/initializers/haml.rb
128
128
  - generators/surveyor/templates/initializers/surveyor.rb
129
+ - generators/surveyor/templates/migrate/add_display_order_to_surveys.rb
129
130
  - generators/surveyor/templates/migrate/create_answers.rb
130
131
  - generators/surveyor/templates/migrate/create_dependencies.rb
131
132
  - generators/surveyor/templates/migrate/create_dependency_conditions.rb
@@ -148,25 +149,28 @@ files:
148
149
  - lib/surveyor/acts_as_response.rb
149
150
  - lib/surveyor/config.rb
150
151
  - lib/tasks/surveyor_tasks.rake
151
- - lib/tiny_code.rb
152
152
  - lib/xml_formatter.rb
153
153
  - script/surveyor/answer.rb
154
- - script/surveyor/columnizer.rb
154
+ - script/surveyor/base.rb
155
155
  - script/surveyor/dependency.rb
156
156
  - script/surveyor/dependency_condition.rb
157
157
  - script/surveyor/question.rb
158
158
  - script/surveyor/question_group.rb
159
159
  - script/surveyor/specs/answer_spec.rb
160
- - script/surveyor/specs/question_dependency_spec.rb
161
- - script/surveyor/specs/question_group_spec.rb
162
160
  - script/surveyor/specs/question_spec.rb
163
- - script/surveyor/specs/section_spec.rb
161
+ - script/surveyor/specs/spec_helper.rb
162
+ - script/surveyor/specs/survey_section_spec.rb
163
+ - script/surveyor/specs/validation_condition_spec.rb
164
+ - script/surveyor/specs/validation_spec.rb
164
165
  - script/surveyor/survey.rb
165
166
  - script/surveyor/survey_parser.rb
166
167
  - script/surveyor/survey_section.rb
168
+ - script/surveyor/validation.rb
169
+ - script/surveyor/validation_condition.rb
167
170
  - script/surveyor/whr_dsl.tmproj
168
171
  - spec/controllers/surveyor_controller_spec.rb
169
172
  - spec/factories.rb
173
+ - spec/lib/surveyor_spec.rb
170
174
  - spec/models/answer_spec.rb
171
175
  - spec/models/dependency_condition_spec.rb
172
176
  - spec/models/dependency_spec.rb
@@ -214,6 +218,7 @@ summary: A rails (gem) plugin to enable surveys in your application
214
218
  test_files:
215
219
  - spec/controllers/surveyor_controller_spec.rb
216
220
  - spec/factories.rb
221
+ - spec/lib/surveyor_spec.rb
217
222
  - spec/models/answer_spec.rb
218
223
  - spec/models/dependency_condition_spec.rb
219
224
  - spec/models/dependency_spec.rb
data/lib/tiny_code.rb DELETED
@@ -1,58 +0,0 @@
1
- module TinyCode
2
-
3
- module ClassMethods
4
-
5
- RAND_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "abcdefghijklmnopqrstuvwxyz"
6
-
7
- def make_tiny_code
8
- random_string(10)
9
- end
10
-
11
- def random_string(len)
12
- rand_max = RAND_CHARS.size
13
- ret = ""
14
- len.times{ ret << RAND_CHARS[rand(rand_max)] }
15
- ret
16
- end
17
-
18
-
19
- def self.to_normalized_string(text)
20
- words_to_omit = ["a", "to", "the", "of", "has", "have", "it", "is", "in", "on", "or", "but", "when", "be"]
21
-
22
- # strip all the html tags from the text data
23
- col_text = text.gsub(/(<[^>]*>)|\n|\t/s, ' ')
24
-
25
- # Removing capitalization
26
- col_text.downcase!
27
- # Removing potential problem characters
28
- col_text.gsub!(/\"|\'/, '')
29
- # Removing text inside parens
30
- col_text.gsub!(/\(.*?\)/,'')
31
-
32
- # Removing all other non-word characters
33
- col_text.gsub!(/\W/, ' ')
34
-
35
- column_words = []
36
- words = col_text.split(' ')
37
- words.each do |word|
38
- if !words_to_omit.include?(word)
39
- column_words << word
40
- end
41
- end
42
-
43
- #reducing the word list to limit column length
44
- if column_words.length > 5
45
- column_words.slice!(0, column_words.length - 5)
46
- end
47
-
48
- #re-assemble the string
49
- column_words.join("_")
50
- end
51
-
52
- end
53
-
54
- def self.included(klass)
55
- klass.extend(ClassMethods)
56
- end
57
-
58
- end
@@ -1,36 +0,0 @@
1
- module Columnizer
2
-
3
- def self.to_normalized_column(text)
4
- words_to_omit = ["a", "to", "the", "of", "has", "have", "it", "is", "in", "on", "or", "but", "when", "be"]
5
-
6
- # strip all the html tags from the text data
7
- col_text = text.gsub(/(<[^>]*>)|\n|\t/s, ' ')
8
-
9
- # Removing capitalization
10
- col_text.downcase!
11
- # Removing potential problem characters
12
- col_text.gsub!(/\"|\'/, '')
13
- # Removing text inside parens
14
- col_text.gsub!(/\(.*?\)/,'')
15
-
16
- # Removing all other non-word characters
17
- col_text.gsub!(/\W/, ' ')
18
-
19
- column_words = []
20
- words = col_text.split(' ')
21
- words.each do |word|
22
- if !words_to_omit.include?(word)
23
- column_words << word
24
- end
25
- end
26
-
27
- #reducing the word list to limit column length
28
- if column_words.length > 5
29
- column_words.slice!(0, column_words.length - 5)
30
- end
31
-
32
- #re-assemble the string
33
- column_words.join("_")
34
- end
35
-
36
- end