Class: Surveyor::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/surveyor/parser.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Instance methods



61
62
63
# File 'lib/surveyor/parser.rb', line 61

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



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
97
98
99
100
101
102
103
# File 'lib/surveyor/parser.rb', line 69

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
      report_missing_default_locale
      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.full_messages.join(", ")}"
      end
    else
      Surveyor::Parser.rake_trace("", -2)
      context[type.to_sym].clear(context)
    end
  end
end

Class Attribute Details

.logObject

Returns the value of attribute log.



8
9
10
# File 'lib/surveyor/parser.rb', line 8

def log
  @log
end

.optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/surveyor/parser.rb', line 8

def options
  @options
end

Instance Attribute Details

#contextObject

Attributes



11
12
13
# File 'lib/surveyor/parser.rb', line 11

def context
  @context
end

Class Method Details

.ensure_attrsObject



25
26
27
28
29
30
# File 'lib/surveyor/parser.rb', line 25

def self.ensure_attrs
  self.options ||= {}
  self.log ||= {}
  self.log[:indent] ||= 0
  self.log[:source] ||= ""
end

.parse(str, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/surveyor/parser.rb', line 17

def self.parse(str, options={})
  self.ensure_attrs
  self.options = options
  self.log[:source] = str
  Surveyor::Parser.rake_trace "\n"
  Surveyor::Parser.new.parse(str)
  Surveyor::Parser.rake_trace "\n"
end

.parse_file(filename, options = {}) ⇒ Object

Class methods



14
15
16
# File 'lib/surveyor/parser.rb', line 14

def self.parse_file(filename, options={})
  self.parse(File.read(filename),{:filename => filename}.merge(options))
end

.raise_error(str, skip_trace = false) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/surveyor/parser.rb', line 52

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



31
32
33
34
35
36
37
# File 'lib/surveyor/parser.rb', line 31

def self.rake_trace(str, indent_increment=0)
  self.ensure_attrs
  unless str.blank?
    puts "#{' ' * self.log[:indent]}#{str}" if self.options[:trace] == true
  end
  self.log[:indent] += indent_increment
end

.source_extract(line) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/surveyor/parser.rb', line 39

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



64
65
66
67
# File 'lib/surveyor/parser.rb', line 64

def parse(str)
  instance_eval(str)
  return context[:survey]
end