Class: Surveyor::Parser
- Inherits:
-
Object
- Object
- Surveyor::Parser
- Defined in:
- lib/surveyor/parser.rb
Class Attribute Summary collapse
-
.log ⇒ Object
Returns the value of attribute log.
-
.options ⇒ Object
Returns the value of attribute options.
Instance Attribute Summary collapse
-
#context ⇒ Object
Attributes.
Class Method Summary collapse
- .ensure_attrs ⇒ Object
-
.parse(str, options = {}) ⇒ Object
Class methods.
- .raise_error(str, skip_trace = false) ⇒ Object
- .rake_trace(str, indent_increment = 0) ⇒ Object
- .source_extract(line) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Parser
constructor
Instance methods.
-
#method_missing(missing_method, *args, &block) ⇒ Object
This method_missing does all the heavy lifting for the DSL.
- #parse(str) ⇒ Object
Constructor Details
#initialize ⇒ Parser
Instance methods
55 56 57 |
# File 'lib/surveyor/parser.rb', line 55 def initialize self.context = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(missing_method, *args, &block) ⇒ Object
This method_missing does all the heavy lifting for the DSL
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/surveyor/parser.rb', line 63 def method_missing(missing_method, *args, &block) method_name, reference_identifier = missing_method.to_s.split("_", 2) type = full(method_name) Surveyor::Parser.raise_error( "\"#{type}\" is not a surveyor method." )if !%w(survey survey_translation survey_section question_group question dependency dependency_condition answer validation validation_condition).include?(type) Surveyor::Parser.rake_trace(reference_identifier.blank? ? "#{type} #{args.map(&:inspect).join ', '}" : "#{type}_#{reference_identifier} #{args.map(&:inspect).join ', '}", block_models.include?(type) ? 2 : 0) # check for blocks Surveyor::Parser.raise_error "A #{type.humanize.downcase} should take a block" if block_models.include?(type) && !block_given? Surveyor::Parser.raise_error "A #{type.humanize.downcase} doesn't take a block" if !block_models.include?(type) && block_given? # parse and build type.classify.constantize.new.extend("SurveyorParser#{type.classify}Methods".constantize).parse_and_build(context, args, method_name, reference_identifier) # evaluate and clear context for block models if block_models.include?(type) self.instance_eval(&block) if type == 'survey' resolve_dependency_condition_references resolve_question_correct_answers report_lost_and_duplicate_references Surveyor::Parser.rake_trace("", -2) if context[:survey].save Surveyor::Parser.rake_trace "Survey saved." else Surveyor::Parser.raise_error "Survey not saved: #{context[:survey].errors..join(", ")}" end else Surveyor::Parser.rake_trace("", -2) context[type.to_sym].clear(context) end end end |
Class Attribute Details
.log ⇒ Object
Returns the value of attribute log.
5 6 7 |
# File 'lib/surveyor/parser.rb', line 5 def log @log end |
.options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/surveyor/parser.rb', line 5 def @options end |
Instance Attribute Details
#context ⇒ Object
Attributes
8 9 10 |
# File 'lib/surveyor/parser.rb', line 8 def context @context end |
Class Method Details
.ensure_attrs ⇒ Object
19 20 21 22 23 24 |
# File 'lib/surveyor/parser.rb', line 19 def self.ensure_attrs self. ||= {} self.log ||= {} self.log[:indent] ||= 0 self.log[:source] ||= "" end |
.parse(str, options = {}) ⇒ Object
Class methods
11 12 13 14 15 16 17 18 |
# File 'lib/surveyor/parser.rb', line 11 def self.parse(str, ={}) self.ensure_attrs self. = self.log[:source] = str Surveyor::Parser.rake_trace "\n" Surveyor::Parser.new.parse(str) Surveyor::Parser.rake_trace "\n" end |
.raise_error(str, skip_trace = false) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/surveyor/parser.rb', line 46 def self.raise_error(str, skip_trace = false) self.ensure_attrs line = caller[1].split('/').last.split(':')[1].to_i raise Surveyor::ParserError, "#{str}\n" if skip_trace raise Surveyor::ParserError, "#{self.source_extract(line)}\nline \##{line}: #{str}\n" end |
.rake_trace(str, indent_increment = 0) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/surveyor/parser.rb', line 25 def self.rake_trace(str, indent_increment=0) self.ensure_attrs unless str.blank? puts "#{' ' * self.log[:indent]}#{str}" if self.[:trace] == true end self.log[:indent] += indent_increment end |
.source_extract(line) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/surveyor/parser.rb', line 33 def self.source_extract(line) source_code = self.log[:source].split("\n") radius = 3 start_on_line = [ line - radius - 1, 0 ].max end_on_line = [ line + radius - 1, source_code.length].min return unless source_code = source_code[start_on_line..end_on_line] line_counter = start_on_line source_code.sum do |line| line_counter += 1 "#{line_counter}: #{line}\n" end end |
Instance Method Details
#parse(str) ⇒ Object
58 59 60 61 |
# File 'lib/surveyor/parser.rb', line 58 def parse(str) instance_eval(str) return context[:survey] end |