ccls-surveyor 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +285 -0
  3. data/Rakefile +54 -0
  4. data/VERSION +1 -0
  5. data/app/controllers/surveyor_controller.rb +142 -0
  6. data/app/helpers/survey_form_builder.rb +37 -0
  7. data/app/helpers/surveyor_helper.rb +67 -0
  8. data/app/models/answer.rb +23 -0
  9. data/app/models/dependency.rb +50 -0
  10. data/app/models/dependency_condition.rb +47 -0
  11. data/app/models/question.rb +54 -0
  12. data/app/models/question_group.rb +33 -0
  13. data/app/models/response.rb +94 -0
  14. data/app/models/response_set.rb +316 -0
  15. data/app/models/survey.rb +172 -0
  16. data/app/models/survey_section.rb +18 -0
  17. data/app/models/validation.rb +38 -0
  18. data/app/models/validation_condition.rb +47 -0
  19. data/app/views/layouts/surveyor_default.html.erb +13 -0
  20. data/app/views/partials/_answer.html.haml +58 -0
  21. data/app/views/partials/_question.html.haml +80 -0
  22. data/app/views/partials/_question_group.html.haml +39 -0
  23. data/app/views/surveyor/edit.html.haml +38 -0
  24. data/app/views/surveyor/new.html.haml +16 -0
  25. data/app/views/surveyor/show.html.haml +12 -0
  26. data/ccls-surveyor.gemspec +212 -0
  27. data/config/routes.rb +11 -0
  28. data/features/step_definitions/surveyor_steps.rb +7 -0
  29. data/features/step_definitions/web_steps.rb +273 -0
  30. data/features/support/env.rb +57 -0
  31. data/features/support/paths.rb +25 -0
  32. data/features/surveyor.feature +11 -0
  33. data/generators/extend_surveyor/extend_surveyor_generator.rb +22 -0
  34. data/generators/extend_surveyor/templates/EXTENDING_SURVEYOR +17 -0
  35. data/generators/extend_surveyor/templates/extensions/survey_extensions.rb +24 -0
  36. data/generators/extend_surveyor/templates/extensions/surveyor_controller_extensions.rb +28 -0
  37. data/generators/extend_surveyor/templates/extensions/surveyor_custom.html.erb +13 -0
  38. data/generators/extend_surveyor/templates/extensions/surveyor_helper_extensions.rb +17 -0
  39. data/generators/surveyor/surveyor_generator.rb +67 -0
  40. data/generators/surveyor/templates/README +10 -0
  41. data/generators/surveyor/templates/assets/images/222222_11x11_icon_arrows_leftright.gif +0 -0
  42. data/generators/surveyor/templates/assets/images/222222_11x11_icon_arrows_updown.gif +0 -0
  43. data/generators/surveyor/templates/assets/images/222222_11x11_icon_close.gif +0 -0
  44. data/generators/surveyor/templates/assets/images/222222_11x11_icon_doc.gif +0 -0
  45. data/generators/surveyor/templates/assets/images/222222_11x11_icon_folder_closed.gif +0 -0
  46. data/generators/surveyor/templates/assets/images/222222_11x11_icon_folder_open.gif +0 -0
  47. data/generators/surveyor/templates/assets/images/222222_11x11_icon_minus.gif +0 -0
  48. data/generators/surveyor/templates/assets/images/222222_11x11_icon_plus.gif +0 -0
  49. data/generators/surveyor/templates/assets/images/222222_11x11_icon_resize_se.gif +0 -0
  50. data/generators/surveyor/templates/assets/images/222222_35x9_colorpicker_indicator.gif.gif +0 -0
  51. data/generators/surveyor/templates/assets/images/222222_7x7_arrow_down.gif +0 -0
  52. data/generators/surveyor/templates/assets/images/222222_7x7_arrow_left.gif +0 -0
  53. data/generators/surveyor/templates/assets/images/222222_7x7_arrow_right.gif +0 -0
  54. data/generators/surveyor/templates/assets/images/222222_7x7_arrow_up.gif +0 -0
  55. data/generators/surveyor/templates/assets/images/454545_11x11_icon_arrows_leftright.gif +0 -0
  56. data/generators/surveyor/templates/assets/images/454545_11x11_icon_arrows_updown.gif +0 -0
  57. data/generators/surveyor/templates/assets/images/454545_11x11_icon_close.gif +0 -0
  58. data/generators/surveyor/templates/assets/images/454545_11x11_icon_doc.gif +0 -0
  59. data/generators/surveyor/templates/assets/images/454545_11x11_icon_folder_closed.gif +0 -0
  60. data/generators/surveyor/templates/assets/images/454545_11x11_icon_folder_open.gif +0 -0
  61. data/generators/surveyor/templates/assets/images/454545_11x11_icon_minus.gif +0 -0
  62. data/generators/surveyor/templates/assets/images/454545_11x11_icon_plus.gif +0 -0
  63. data/generators/surveyor/templates/assets/images/454545_7x7_arrow_down.gif +0 -0
  64. data/generators/surveyor/templates/assets/images/454545_7x7_arrow_left.gif +0 -0
  65. data/generators/surveyor/templates/assets/images/454545_7x7_arrow_right.gif +0 -0
  66. data/generators/surveyor/templates/assets/images/454545_7x7_arrow_up.gif +0 -0
  67. data/generators/surveyor/templates/assets/images/888888_11x11_icon_arrows_leftright.gif +0 -0
  68. data/generators/surveyor/templates/assets/images/888888_11x11_icon_arrows_updown.gif +0 -0
  69. data/generators/surveyor/templates/assets/images/888888_11x11_icon_close.gif +0 -0
  70. data/generators/surveyor/templates/assets/images/888888_11x11_icon_doc.gif +0 -0
  71. data/generators/surveyor/templates/assets/images/888888_11x11_icon_folder_closed.gif +0 -0
  72. data/generators/surveyor/templates/assets/images/888888_11x11_icon_folder_open.gif +0 -0
  73. data/generators/surveyor/templates/assets/images/888888_11x11_icon_minus.gif +0 -0
  74. data/generators/surveyor/templates/assets/images/888888_11x11_icon_plus.gif +0 -0
  75. data/generators/surveyor/templates/assets/images/888888_7x7_arrow_down.gif +0 -0
  76. data/generators/surveyor/templates/assets/images/888888_7x7_arrow_left.gif +0 -0
  77. data/generators/surveyor/templates/assets/images/888888_7x7_arrow_right.gif +0 -0
  78. data/generators/surveyor/templates/assets/images/888888_7x7_arrow_up.gif +0 -0
  79. data/generators/surveyor/templates/assets/images/dadada_40x100_textures_02_glass_75.png +0 -0
  80. data/generators/surveyor/templates/assets/images/e6e6e6_40x100_textures_02_glass_75.png +0 -0
  81. data/generators/surveyor/templates/assets/images/ffffff_40x100_textures_01_flat_0.png +0 -0
  82. data/generators/surveyor/templates/assets/images/ffffff_40x100_textures_02_glass_65.png +0 -0
  83. data/generators/surveyor/templates/assets/javascripts/accessibleUISlider.jQuery.js +201 -0
  84. data/generators/surveyor/templates/assets/javascripts/jquery-1.2.6.js +3549 -0
  85. data/generators/surveyor/templates/assets/javascripts/jquery-ui-personalized-1.5.3.js +7616 -0
  86. data/generators/surveyor/templates/assets/javascripts/jquery.form.js +637 -0
  87. data/generators/surveyor/templates/assets/javascripts/surveyor.js +47 -0
  88. data/generators/surveyor/templates/assets/stylesheets/jquery-ui-slider-additions.css +71 -0
  89. data/generators/surveyor/templates/assets/stylesheets/reset.css +50 -0
  90. data/generators/surveyor/templates/assets/stylesheets/sass/surveyor.sass +243 -0
  91. data/generators/surveyor/templates/assets/stylesheets/surveyor.css +235 -0
  92. data/generators/surveyor/templates/assets/stylesheets/ui.theme.css +851 -0
  93. data/generators/surveyor/templates/initializers/haml.rb +8 -0
  94. data/generators/surveyor/templates/initializers/surveyor.rb +10 -0
  95. data/generators/surveyor/templates/migrate/add_correct_answer_id_to_questions.rb +9 -0
  96. data/generators/surveyor/templates/migrate/add_display_order_to_surveys.rb +9 -0
  97. data/generators/surveyor/templates/migrate/add_index_to_response_sets.rb +9 -0
  98. data/generators/surveyor/templates/migrate/add_index_to_surveys.rb +9 -0
  99. data/generators/surveyor/templates/migrate/add_manual_numbering.rb +11 -0
  100. data/generators/surveyor/templates/migrate/add_unique_indicies.rb +17 -0
  101. data/generators/surveyor/templates/migrate/create_answers.rb +37 -0
  102. data/generators/surveyor/templates/migrate/create_dependencies.rb +22 -0
  103. data/generators/surveyor/templates/migrate/create_dependency_conditions.rb +29 -0
  104. data/generators/surveyor/templates/migrate/create_question_groups.rb +27 -0
  105. data/generators/surveyor/templates/migrate/create_questions.rb +36 -0
  106. data/generators/surveyor/templates/migrate/create_response_sets.rb +22 -0
  107. data/generators/surveyor/templates/migrate/create_responses.rb +33 -0
  108. data/generators/surveyor/templates/migrate/create_survey_sections.rb +29 -0
  109. data/generators/surveyor/templates/migrate/create_surveys.rb +31 -0
  110. data/generators/surveyor/templates/migrate/create_validation_conditions.rb +32 -0
  111. data/generators/surveyor/templates/migrate/create_validations.rb +20 -0
  112. data/generators/surveyor/templates/surveys/kitchen_sink_survey.rb +218 -0
  113. data/generators/surveyor/templates/tasks/surveyor.rb +4 -0
  114. data/generators/test_surveyor/templates/TESTING_SURVEYOR +0 -0
  115. data/generators/test_surveyor/templates/environments/cucumber.rb +21 -0
  116. data/generators/test_surveyor/test_surveyor_generator.rb +15 -0
  117. data/lib/ccls-surveyor.rb +1 -0
  118. data/lib/fixtures_extensions.rb +6 -0
  119. data/lib/surveyor.rb +44 -0
  120. data/lib/surveyor/acts_as_response.rb +33 -0
  121. data/lib/surveyor/config.rb +45 -0
  122. data/lib/tasks/surveyor_tasks.rake +33 -0
  123. data/lib/xml_formatter.rb +12 -0
  124. data/rails/init.rb +1 -0
  125. data/script/surveyor/answer.rb +54 -0
  126. data/script/surveyor/base.rb +77 -0
  127. data/script/surveyor/dependency.rb +13 -0
  128. data/script/surveyor/dependency_condition.rb +40 -0
  129. data/script/surveyor/parser.rb +207 -0
  130. data/script/surveyor/question.rb +37 -0
  131. data/script/surveyor/question_group.rb +26 -0
  132. data/script/surveyor/specs/answer_spec.rb +29 -0
  133. data/script/surveyor/specs/question_spec.rb +63 -0
  134. data/script/surveyor/specs/spec_helper.rb +7 -0
  135. data/script/surveyor/specs/survey_section_spec.rb +23 -0
  136. data/script/surveyor/specs/validation_condition_spec.rb +20 -0
  137. data/script/surveyor/specs/validation_spec.rb +20 -0
  138. data/script/surveyor/survey.rb +35 -0
  139. data/script/surveyor/survey_section.rb +21 -0
  140. data/script/surveyor/validation.rb +21 -0
  141. data/script/surveyor/validation_condition.rb +21 -0
  142. data/script/surveyor/whr_dsl.tmproj +244 -0
  143. data/spec/controllers/surveyor_controller_spec.rb +193 -0
  144. data/spec/factories.rb +145 -0
  145. data/spec/lib/surveyor_spec.rb +44 -0
  146. data/spec/models/answer_spec.rb +29 -0
  147. data/spec/models/dependency_condition_spec.rb +321 -0
  148. data/spec/models/dependency_spec.rb +81 -0
  149. data/spec/models/question_group_spec.rb +35 -0
  150. data/spec/models/question_spec.rb +75 -0
  151. data/spec/models/response_set_spec.rb +245 -0
  152. data/spec/models/response_spec.rb +76 -0
  153. data/spec/models/survey_section_spec.rb +32 -0
  154. data/spec/models/survey_spec.rb +71 -0
  155. data/spec/models/validation_condition_spec.rb +105 -0
  156. data/spec/models/validation_spec.rb +59 -0
  157. data/spec/rcov.opts +2 -0
  158. data/spec/spec.opts +4 -0
  159. data/spec/spec_helper.rb +12 -0
  160. metadata +254 -0
@@ -0,0 +1,4 @@
1
+ $VERBOSE = nil
2
+ if surveyor_gem = Gem.searcher.find('surveyor')
3
+ Dir["#{surveyor_gem.full_gem_path}/lib/tasks/*.rake"].each { |ext| load ext }
4
+ end
@@ -0,0 +1,21 @@
1
+ config.cache_classes = true # This must be true for Cucumber to operate correctly!
2
+
3
+ # Log error messages when you accidentally call methods on nil.
4
+ config.whiny_nils = true
5
+
6
+ # Show full error reports and disable caching
7
+ config.action_controller.consider_all_requests_local = true
8
+ config.action_controller.perform_caching = false
9
+
10
+ # Disable request forgery protection in test environment
11
+ config.action_controller.allow_forgery_protection = false
12
+
13
+ # Tell Action Mailer not to deliver emails to the real world.
14
+ # The :test delivery method accumulates sent emails in the
15
+ # ActionMailer::Base.deliveries array.
16
+ config.action_mailer.delivery_method = :test
17
+
18
+ config.gem "cucumber", :lib => false, :version => ">=0.3.11" unless File.directory?(File.join(Rails.root, 'vendor/plugins/cucumber'))
19
+ config.gem "webrat", :lib => false, :version => ">=0.4.4" unless File.directory?(File.join(Rails.root, 'vendor/plugins/webrat'))
20
+ config.gem "rspec", :lib => false, :version => ">=1.2.6" unless File.directory?(File.join(Rails.root, 'vendor/plugins/rspec'))
21
+ config.gem "rspec-rails", :lib => 'spec/rails', :version => ">=1.2.6" unless File.directory?(File.join(Rails.root, 'vendor/plugins/rspec-rails'))
@@ -0,0 +1,15 @@
1
+ class TestSurveyorGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+
5
+ m.directory "surveys"
6
+
7
+ # Copy README to your app
8
+ m.file "TESTING_SURVEYOR", "surveys/TESTING_SURVEYOR"
9
+
10
+ m.file "environments/cucumber.rb", "config/environments/cucumber.rb"
11
+ m.readme "TESTING_SURVEYOR"
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ require 'surveyor'
@@ -0,0 +1,6 @@
1
+ class Fixtures
2
+ def delete_existing_fixtures
3
+ # @connection.delete "DELETE FROM #{@connection.quote_table_name(table_name)}", 'Fixture Delete'
4
+ puts "Appending, skipping delete..."
5
+ end
6
+ end
data/lib/surveyor.rb ADDED
@@ -0,0 +1,44 @@
1
+ require File.dirname(__FILE__) + '/surveyor/acts_as_response'
2
+ module Surveyor
3
+ RAND_CHARS = [('a'..'z'), ('A'..'Z'), (0..9)].map{|r| r.to_a}.flatten.to_s
4
+
5
+ def self.make_tiny_code(len = 10)
6
+ if RUBY_VERSION < "1.8.7"
7
+ (1..len).to_a.map{|i| RAND_CHARS[rand(RAND_CHARS.size), 1] }.to_s
8
+ else
9
+ len.times.map{|i| RAND_CHARS[rand(RAND_CHARS.size), 1] }.to_s
10
+ end
11
+ end
12
+
13
+ def self.to_normalized_string(text)
14
+ words_to_omit = %w(a be but has have in is it of on or the to when)
15
+ col_text = text.gsub(/(<[^>]*>)|\n|\t/s, ' ') # Remove html tags
16
+ col_text.downcase! # Remove capitalization
17
+ col_text.gsub!(/\"|\'/, '') # Remove potential problem characters
18
+ col_text.gsub!(/\(.*?\)/,'') # Remove text inside parens
19
+ col_text.gsub!(/\W/, ' ') # Remove all other non-word characters
20
+ cols = (col_text.split(' ') - words_to_omit)
21
+ (cols.size > 5 ? cols[-5..-1] : cols).join("_")
22
+ end
23
+ end
24
+
25
+ # From http://guides.rubyonrails.org/plugins.html#controllers
26
+ # Fix for:
27
+ # ArgumentError in SurveyorController#edit
28
+ # A copy of ApplicationController has been removed from the module tree but is still active!
29
+ # Equivalent of using "unloadable" in SurveyorController (unloadable has been deprecated)
30
+
31
+ %w{models controllers}.each do |dir|
32
+ path = File.expand_path(File.join(File.dirname(__FILE__), '../app', dir))
33
+ # $LOAD_PATH << path # already here
34
+ # ActiveSupport::Dependencies.load_paths << path # already here too
35
+ # ActiveSupport::Dependencies.load_once_paths.delete(path)
36
+ # ActiveSupport::Dependencies.autoload_once_paths.delete(path)
37
+ # [$LOAD_PATH, ActiveSupport::Dependencies.load_paths, ActiveSupport::Dependencies.load_once_paths].each{|x| Rails.logger.info x}
38
+
39
+
40
+ ActiveSupport::Dependencies.autoload_paths << path
41
+ ActiveSupport::Dependencies.autoload_once_paths << path
42
+ end
43
+
44
+ require 'surveyor/config'
@@ -0,0 +1,33 @@
1
+ require 'active_record'
2
+
3
+ module Surveyor
4
+ module Response
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def acts_as_response
11
+ include Surveyor::Response::InstanceMethods
12
+ end
13
+ end
14
+
15
+ module InstanceMethods
16
+ # Returns the response as a particular response_class type
17
+ def as(type_symbol)
18
+ return case type_symbol.to_sym
19
+ when :string, :text, :integer, :float, :datetime
20
+ self.send("#{type_symbol}_value".to_sym)
21
+ when :date
22
+ self.datetime_value.nil? ? nil : self.datetime_value.to_date
23
+ when :time
24
+ self.datetime_value.nil? ? nil : self.datetime_value.to_time
25
+ else # :answer_id
26
+ self.answer_id
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ ActiveRecord::Base.send(:include, Surveyor::Response)
@@ -0,0 +1,45 @@
1
+ module Surveyor
2
+ #
3
+ # The Surveyor::Config object emulates a hash with simple bracket methods
4
+ # which allow you to get and set values in the configuration table:
5
+ #
6
+ # Surveyor::Config['setting.name'] = 'value'
7
+ # Surveyor::Config['setting.name'] #=> "value"
8
+ #
9
+ # Currently, there is not a way to edit configuration through the admin
10
+ # system so it must be done manually. The console script is probably the
11
+ # easiest way to this:
12
+ #
13
+ # % script/console production
14
+ # Loading production environment.
15
+ # >> Surveyor::Config['setting.name'] = 'value'
16
+ # => "value"
17
+ # >>
18
+ #
19
+ # Surveyor currently uses the following settings:
20
+ #
21
+ # defaults.title :: the title of the survey system
22
+ # defaults.layout :: the layout used by the survey system
23
+
24
+ class Config
25
+ @@config_hash = {}
26
+
27
+ class << self
28
+ def [](key)
29
+ @@config_hash[key]
30
+ end
31
+
32
+ def []=(key, value)
33
+ @@config_hash[key] = value
34
+ end
35
+
36
+ def to_hash
37
+ @@config_hash
38
+ end
39
+
40
+ def run
41
+ yield self if block_given?
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,33 @@
1
+ desc "generate and load survey (specify FILE=surveys/your_survey.rb)"
2
+ task :surveyor => :"surveyor:default"
3
+
4
+ namespace :surveyor do
5
+
6
+ task :default => [:generate_fixtures, :load_fixtures]
7
+
8
+ desc "generate survey fixtures from survey file"
9
+ task :generate_fixtures => :environment do
10
+ require File.join(File.dirname(__FILE__), "../../script/surveyor/parser")
11
+ raise "USAGE: file name required e.g. 'FILE=surveys/kitchen_sink_survey.rb'" if ENV["FILE"].blank?
12
+ fixture_dir = File.join(RAILS_ROOT, "surveys", "fixtures")
13
+ mkdir fixture_dir unless File.exists?(fixture_dir)
14
+ SurveyParser::Parser.parse(File.join(RAILS_ROOT, ENV["FILE"]))
15
+ end
16
+
17
+ desc "load survey fixtures"
18
+ task :load_fixtures => :environment do
19
+ require 'active_record/fixtures'
20
+ require 'fixtures_extensions' unless ENV["APPEND"].blank?
21
+ ActiveRecord::Base.establish_connection(Rails.env)
22
+
23
+ fixture_dir = File.join(RAILS_ROOT, "surveys", "fixtures")
24
+ fixtures = Dir.glob("#{fixture_dir}/*.yml")
25
+ raise "No fixtures found." if fixtures.empty?
26
+
27
+ fixtures.each do |file_name|
28
+ puts "Loading #{file_name}..."
29
+ Fixtures.create_fixtures(fixture_dir, File.basename(file_name, '.*'))
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,12 @@
1
+ module XmlFormatter
2
+ def to_xml(options = {})
3
+ options[:indent] ||= 2
4
+ xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
5
+ xml.instruct! unless options[:skip_instruct]
6
+ xml.tag!(self.class.name.downcase.to_sym, self.attributes) do
7
+ self.class.reflect_on_all_associations.to_a.each do |assoc|
8
+ xml.tag!(assoc.name)
9
+ end
10
+ end
11
+ end
12
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ # Init hook code here
@@ -0,0 +1,54 @@
1
+ module SurveyParser
2
+ class Answer < SurveyParser::Base
3
+ # Context, Content, Reference, Display
4
+ attr_accessor :id, :parser, :question_id
5
+ attr_accessor :text, :short_text, :help_text, :weight, :response_class
6
+ attr_accessor :reference_identifier, :data_export_identifier, :common_namespace, :common_identitier
7
+ attr_accessor :display_order, :is_exclusive, :hide_label, :display_length, :custom_class, :custom_renderer
8
+ attr_accessor :validation
9
+
10
+ def default_options
11
+ { :is_exclusive => false,
12
+ :hide_label => false,
13
+ :response_class => "answer"
14
+ }
15
+ end
16
+
17
+ def parse_args(args)
18
+ case args[0]
19
+ when Hash # Hash
20
+ text_args(args[0][:text]).merge(args[0])
21
+ when String # (String, Hash) or (String, Symbol, Hash)
22
+ text_args(args[0]).merge(hash_from args[1]).merge(args[2] || {})
23
+ when Symbol # (Symbol, Hash) or (Symbol, Symbol, Hash)
24
+ symbol_args(args[0]).merge(hash_from args[1]).merge(args[2] || {})
25
+ else
26
+ text_args(nil)
27
+ end
28
+ end
29
+
30
+ def text_args(text = "Answer")
31
+ {:text => text.to_s, :short_text => text, :data_export_identifier => Surveyor.to_normalized_string(text)}
32
+ end
33
+ def hash_from(arg)
34
+ arg.is_a?(Symbol) ? {:response_class => arg.to_s} : arg.is_a?(Hash) ? arg : {}
35
+ end
36
+ def symbol_args(arg)
37
+ case arg
38
+ when :other
39
+ text_args("Other")
40
+ when :other_and_string
41
+ text_args("Other").merge({:response_class => "string"})
42
+ when :none, :omit # is_exclusive erases and disables other checkboxes and input elements
43
+ text_args(arg.to_s.humanize).merge({:is_exclusive => true})
44
+ when :integer, :date, :time, :datetime, :text, :datetime, :string
45
+ text_args(arg.to_s.humanize).merge({:response_class => arg.to_s, :hide_label => true})
46
+ end
47
+ end
48
+ def to_file
49
+ super
50
+ if self.validation then self.validation.to_file end
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,77 @@
1
+ module SurveyParser
2
+ class Base
3
+
4
+ # Class level instance variable, because class variable are shared with subclasses
5
+ class << self
6
+ attr_accessor :children
7
+ end
8
+
9
+ @children = []
10
+
11
+ # Class methods
12
+ def self.inherited(subclass)
13
+ # set the class level instance variable default on subclasses
14
+ subclass.instance_variable_set(:@children, self.instance_variable_get(:@children))
15
+ end
16
+
17
+ def self.has_children(*args)
18
+ args.each{|model| attr_accessor model}
19
+ self.instance_variable_set(:@children, args)
20
+ end
21
+
22
+ # Instance methods
23
+ def initialize(obj, args, opts)
24
+ # inherit the parser from parent (obj)
25
+ self.parser = (obj.nil? ? nil : obj.class == SurveyParser::Parser ? obj : obj.parser)
26
+ # get a new id from the parser
27
+ self.id = parser.nil? ? nil : parser.send("new_#{self.class.name.demodulize.underscore}_id")
28
+ # set [parent]_id to obj.id, if we have that attribute
29
+ self.send("#{obj.class.name.demodulize.underscore}_id=", obj.nil? ? nil : obj.id) if self.respond_to?("#{obj.class.name.demodulize.underscore}_id=")
30
+ # initialize descendant models
31
+ self.class.children.each{|model| self.send("#{model}=", [])}
32
+ # combine default options, parsed opts, parsed args, and initialize instance variables
33
+ self.default_options.merge(parse_opts(opts)).merge(parse_args(args)).each{|k,v| self.send("#{k.to_s}=", v)}
34
+ # print to the log
35
+ print "#{self.class.name.demodulize.gsub(/[a-z]/, "")[-1,1]}#{self.id} "
36
+ end
37
+ def default_options
38
+ {}
39
+ end
40
+ def parse_opts(opts)
41
+ opts.reject{|k,v| k == :method_name} # toss the method name by default
42
+ end
43
+ def parse_args(args)
44
+ args[0] || {}
45
+ end
46
+
47
+ # Filter out attributes that shouldn't be in fixtures, including children, parser, placeholders
48
+ def yml_attrs
49
+ instance_variables.sort - self.class.children.map{|model| "@#{model.to_s}"} - %w(@id @parser @dependency @validation @question_reference @answer_reference)
50
+ end
51
+ def to_yml
52
+ out = [ %(#{self.parser.salt}_#{self.class.name.demodulize.underscore}_#{@id}:) ]
53
+ yml_attrs.each{|a| out << associate_and_format(a)}
54
+ (out << nil ).join("\r\n")
55
+ end
56
+ def associate_and_format(a)
57
+ if a =~ /_id$/ # a foreign key, e.g. survey_id
58
+ " #{a[1..-4]}: " + (instance_variable_get(a).nil? ? "" : "#{self.parser.salt}_#{a[1..-4]}_#{instance_variable_get(a)}")
59
+ else # quote strings
60
+ #
61
+ # >> Regexp.new( /[0-9a-zA-z\. #]/).to_s
62
+ # => "(?-mix:[0-9a-zA-z\\. #])"
63
+ #
64
+ # The regex in kitchen sink included a space which caused the yml
65
+ # to have a space which got chopped off in the fixture load leaving
66
+ # an open ended regex which ruby did not like at all. Therefore,
67
+ # wrap Regexp in quotes as well?
68
+ #
69
+ " #{a[1..-1]}: #{(instance_variable_get(a).is_a?(String) || instance_variable_get(a).is_a?(Regexp)) ? "\"#{instance_variable_get(a)}\"" : instance_variable_get(a) }"
70
+ end
71
+ end
72
+ def to_file
73
+ File.open(self.parser.send("#{self.class.name.demodulize.underscore.pluralize}_yml"), File::CREAT|File::APPEND|File::WRONLY) {|f| f << to_yml}
74
+ self.class.children.each{|model| self.send(model).compact.map(&:to_file)}
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,13 @@
1
+ module SurveyParser
2
+ class Dependency < SurveyParser::Base
3
+ # Context, Conditional, Children
4
+ attr_accessor :id, :question_id, :question_group_id, :parser
5
+ attr_accessor :rule
6
+ has_children :dependency_conditions
7
+
8
+ def parse_opts(opts)
9
+ {} # toss the method name and reference identifier by default
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,40 @@
1
+ module SurveyParser
2
+ class DependencyCondition < SurveyParser::Base
3
+
4
+ # Context, Conditional, Value
5
+ attr_accessor :id, :dependency_id, :rule_key, :parser
6
+ attr_accessor :question_id, :operator
7
+ attr_accessor :answer_id, :datetime_value, :integer_value, :float_value, :unit, :text_value, :string_value, :response_other
8
+ attr_accessor :question_reference, :answer_reference
9
+
10
+ def default_options
11
+ { :operator => "==" }
12
+ end
13
+ def parse_args(args)
14
+ a0, a1, a2 = args
15
+ {:question_reference => a0.to_s.gsub("q_", ""), :operator => a1}.merge(a2.is_a?(Hash) ? a2 : {:answer_reference => a2.to_s.gsub("a_", "")})
16
+ end
17
+ def parse_opts(opts)
18
+ {:rule_key => opts[:reference_identifier]}
19
+ end
20
+
21
+ def reconcile_dependencies
22
+ # Looking up references to questions and answers for linking the dependency objects
23
+ print "Lookup Q ref #{@question_reference}:"
24
+ if (ref_question = parser.current_survey.find_question_by_reference(@question_reference))
25
+ print " found Q#{ref_question.id} "
26
+ @question_id = ref_question.id
27
+ print "Lookup A ref #{@answer_reference}"
28
+ if (ref_answer = ref_question.find_answer_by_reference(@answer_reference))
29
+ print " found A#{ref_answer.id} "
30
+ @answer_id = ref_answer.id
31
+ else
32
+ raise "Could not find referenced answer #{@answer_reference}"
33
+ end
34
+ else
35
+ raise "Could not find referenced question #{@question_reference}"
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,207 @@
1
+ require 'active_support' # for pluralize, humanize in ActiveSupport::CoreExtensions::String::Inflections
2
+ module SurveyParser
3
+ class Parser
4
+ @@models = %w(survey survey_section question_group question answer dependency dependency_condition validation validation_condition)
5
+
6
+ # Require base and all models
7
+ (%w(base) + @@models).each{|m| require File.dirname(__FILE__) + "/#{m}"}
8
+
9
+ # Attributes
10
+ attr_accessor :salt, :surveys, :grid_answers
11
+ @@models.each{|m| attr_accessor "#{m.pluralize}_yml".to_sym } # for fixtures
12
+ (@@models - %w(dependency_condition validation_condition)).each {|m| attr_accessor "current_#{m}".to_sym} # for current_model caches
13
+
14
+ # Class methods
15
+ def self.parse(file_name)
16
+ self.define_counter_methods(@@models)
17
+ puts "\n--- Parsing '#{file_name}' ---"
18
+ parser = SurveyParser::Parser.new
19
+ parser.instance_eval(File.read(file_name))
20
+ parser.to_files
21
+ puts "--- End of parsing ---\n\n"
22
+ end
23
+
24
+ # new_survey_id, new_survey_section_id, etc.
25
+ def self.define_counter_methods(names)
26
+ names.each do |name|
27
+ define_method("new_#{name}_id") do
28
+ instance_variable_set("@last_#{name}_id", instance_variable_get("@last_#{name}_id") + 1)
29
+ end
30
+ end
31
+ end
32
+
33
+ # Instance methods
34
+ def initialize
35
+ self.salt = Time.now.strftime("%Y%m%d%H%M%S")
36
+ self.surveys = []
37
+ self.grid_answers = []
38
+ initialize_counters(@@models)
39
+ initialize_fixtures(@@models.map(&:pluralize), File.join(RAILS_ROOT, "surveys", "fixtures"))
40
+ end
41
+
42
+ # @last_survey_id, @last_survey_section_id, etc.
43
+ def initialize_counters(names)
44
+ names.each{|name| instance_variable_set("@last_#{name}_id", 0)}
45
+ end
46
+
47
+ # @surveys_yml, @survey_sections_yml, etc.
48
+ def initialize_fixtures(names, path)
49
+ names.each {|name| file = instance_variable_set("@#{name}_yml", "#{path}/#{name}.yml"); File.truncate(file, 0) if File.exist?(file) }
50
+ end
51
+
52
+ # This method_missing does all the heavy lifting for the DSL
53
+ def method_missing(missing_method, *args, &block)
54
+ method_name, reference_identifier = missing_method.to_s.split("_", 2)
55
+ opts = {:method_name => method_name, :reference_identifier => reference_identifier}
56
+ case method_name
57
+ when "survey"
58
+ self.current_survey = Survey.new(self, args, opts)
59
+ evaluate_the "survey", &block
60
+
61
+ when "section"
62
+ self.current_survey_section = SurveySection.new(self.current_survey, args, opts.merge({:display_order => current_survey.survey_sections.size + 1}))
63
+ evaluate_the "survey_section", &block
64
+
65
+ when "group", "g", "grid", "repeater"
66
+ self.current_question_group = QuestionGroup.new(self.current_survey_section, args, opts)
67
+ evaluate_the "question_group", &block
68
+
69
+ when "question", "q", "label", "image"
70
+ drop_the &block
71
+ self.current_question = Question.new(self.current_survey_section, args, opts.merge(:question_group_id => current_question_group ? current_question_group.id : nil))
72
+ add_grid_answers if in_a_grid?
73
+
74
+ when "dependency", "d"
75
+ drop_the &block
76
+ self.current_dependency = Dependency.new(self.current_question_group || current_question, args, opts)
77
+
78
+ when "condition", "c"
79
+ drop_the &block
80
+ raise "Error: No current dependency or validation for this condition" if self.current_dependency.nil? && self.current_validation.nil?
81
+ if self.current_dependency.nil?
82
+ self.current_validation.validation_conditions << ValidationCondition.new(self.current_validation, args, opts)
83
+ else
84
+ self.current_dependency.dependency_conditions << DependencyCondition.new(self.current_dependency, args, opts)
85
+ end
86
+
87
+ when "answer", "a"
88
+ drop_the &block
89
+ if in_a_grid?
90
+ self.grid_answers << Answer.new(nil, args, opts.merge(:display_order => grid_answers.size + 1))
91
+ else
92
+ raise "Error: No current question" if self.current_question.nil?
93
+ self.current_answer = Answer.new(self.current_question, args, opts.merge(:display_order => current_question.answers.size + 1))
94
+ end
95
+
96
+ when "validation", "v"
97
+ drop_the &block
98
+ self.current_validation = Validation.new(self.current_answer, args, opts)
99
+
100
+
101
+ # explicitly define a dependency condition
102
+ # (not really necessary as is default)
103
+ when "dependencycondition", "dcondition", "dc"
104
+ drop_the &block
105
+ raise "Error: No current dependency for this condition" if self.current_dependency.nil?
106
+ self.current_dependency.dependency_conditions << DependencyCondition.new(self.current_dependency, args, opts)
107
+
108
+ # explicitly define a validation condition
109
+ # (is necessary if want dependency AND validation on
110
+ # same question as dependency existance would try to
111
+ # make the condition a dependency condition.)
112
+ when "validationcondition", "vcondition", "vc"
113
+ drop_the &block
114
+ raise "Error: No current validation for this condition" if self.current_validation.nil?
115
+ self.current_validation.validation_conditions << ValidationCondition.new(self.current_validation, args, opts)
116
+
117
+
118
+
119
+ else
120
+ raise " ERROR: '#{missing_method}' not valid method"
121
+
122
+ end
123
+ end
124
+
125
+ def drop_the(&block)
126
+ raise "Error, I'm dropping the block like it's hot" if block_given?
127
+ end
128
+
129
+ def evaluate_the(model, &block)
130
+ raise "Error: A #{model.humanize} cannot be empty" unless block_given?
131
+ self.instance_eval(&block)
132
+ self.send("clear_current", model)
133
+ end
134
+
135
+ def clear_current(model)
136
+ # puts "clear_current #{model}"
137
+ case model
138
+ when "survey"
139
+ self.current_survey.reconcile_dependencies unless current_survey.nil?
140
+ when "question_group"
141
+ self.grid_answers = []
142
+ clear_current("question")
143
+ when "question"
144
+ @current_dependency = nil
145
+ when "answer"
146
+ @current_validation = nil
147
+ end
148
+ instance_variable_set("@current_#{model}", nil)
149
+ "SurveyParser::#{model.classify}".constantize.send(:children).each{|m| clear_current(m.to_s.singularize)}
150
+ end
151
+
152
+ def current_survey=(s)
153
+ clear_current "survey"
154
+ self.surveys << s
155
+ @current_survey = s
156
+ end
157
+ def current_survey_section=(s)
158
+ clear_current "survey_section"
159
+ self.current_survey.survey_sections << s
160
+ @current_survey_section = s
161
+ end
162
+ def current_question_group=(g)
163
+ clear_current "question_group"
164
+ self.current_survey_section.question_groups << g
165
+ @current_question_group = g
166
+ end
167
+ def current_question=(q)
168
+ clear_current "question"
169
+ self.current_survey_section.questions << q
170
+ @current_question = q
171
+ end
172
+ def current_dependency=(d)
173
+ raise "Error: No question or question group" unless (dependent = self.current_question_group || self.current_question)
174
+ dependent.dependency = d
175
+ @current_dependency = d
176
+ end
177
+ def current_answer=(a)
178
+ raise "Error: No current question" if self.current_question.nil?
179
+ self.current_question.answers << a
180
+ @current_answer = a
181
+ end
182
+ def current_validation=(v)
183
+ clear_current "validation"
184
+ self.current_answer.validation = v
185
+ @current_validation = v
186
+ end
187
+
188
+ def in_a_grid?
189
+ self.current_question_group and self.current_question_group.display_type == "grid"
190
+ end
191
+
192
+ def add_grid_answers
193
+ self.grid_answers.each do |grid_answer|
194
+ my_answer = grid_answer.dup
195
+ my_answer.id = new_answer_id
196
+ my_answer.question_id = self.current_question.id
197
+ my_answer.parser = self
198
+ self.current_answer = my_answer
199
+ end
200
+ end
201
+
202
+ def to_files
203
+ self.surveys.compact.map(&:to_file)
204
+ end
205
+
206
+ end
207
+ end