Class: Sequence::SequencePhrase
- Defined in:
- lib/lucid/sequence/sequence_phrase.rb
Constant Summary collapse
- ParameterConstant =
{ 'quotes' => '"""'}
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#phrase_params ⇒ Object
readonly
Returns the value of attribute phrase_params.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
- #data_table_required? ⇒ Boolean
- #expand(phrase, data) ⇒ Object
-
#initialize(phrase, sequence, data) ⇒ SequencePhrase
constructor
A new instance of SequencePhrase.
- #preprocess(sequence) ⇒ Object
-
#scan_parameters(phrase, mode) ⇒ Object
“Scanning parameters” means that a phrase will be scanned to find any text between chevrons or double quotes.
- #validate_params(phrase, data) ⇒ Object
-
#validate_phrase_values(params, phrase_variables) ⇒ Object
The goal of this method is to check for various inconsistencies that can occur between parameter names in the sequence phrase and those in the actual steps.
- #validate_row(row, params) ⇒ Object
Constructor Details
#initialize(phrase, sequence, data) ⇒ SequencePhrase
Returns a new instance of SequencePhrase.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 14 def initialize(phrase, sequence, data) @key = self.class.sequence_key(phrase, data, :define) #puts "*** [Sequence Step] - Key: #{@key}" @phrase_params = scan_parameters(phrase, :define) #puts "*** [Sequence Step] - Phrase Params: #{@phrase_params}" transformed_steps = preprocess(sequence) #puts "*** [Sequence Step] - Steps: \n#{transformed_steps}" @template = SequenceTemplate::Engine.new(transformed_steps) #puts "\n*** [Sequence Step] - Template: #{@template.inspect}\n" phrase_variables = template.variables #puts "\n*** [Sequence Step] - Phrase Variables: #{phrase_variables}" @values = validate_phrase_values(@phrase_params, phrase_variables) @values.concat(phrase_variables) #puts "*** [Sequence Step] - Values: #{@values}" @values.uniq! end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 7 def key @key end |
#phrase_params ⇒ Object (readonly)
Returns the value of attribute phrase_params.
10 11 12 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 10 def phrase_params @phrase_params end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
9 10 11 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 9 def template @template end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
8 9 10 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 8 def values @values end |
Class Method Details
.sequence_key(phrase, data, mode) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 37 def self.sequence_key(phrase, data, mode) new_phrase = phrase.strip # These lines replace every series of whitespace with an underscore. # For that to work, I have to make sure there are no existing # underscore characters in the first place so any pre-existing # underscores get removed first. new_phrase.gsub!(/_/, '') new_phrase.gsub!(/\s+/, '_') pattern = case mode when :define /<(?:[^\\<>]|\\.)*>/ when :invoke /"([^\\"]|\\.)*"/ end # Here 'patterned' means that for a given phrase, any bit of text # between quotes or chevrons is replaced by the letter X. patterned = new_phrase.gsub(pattern, 'X') key = patterned + (data ? '_T' : '') return key end |
Instance Method Details
#data_table_required? ⇒ Boolean
117 118 119 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 117 def data_table_required? return key =~ /_T$/ end |
#expand(phrase, data) ⇒ Object
121 122 123 124 125 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 121 def (phrase, data) params = validate_params(phrase, data) params = ParameterConstant.merge(params) return template.output(nil, params) end |
#preprocess(sequence) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 83 def preprocess(sequence) # Split text into individual lines and make sure to remove any lines # with hash style comments. lines = sequence.split(/\r\n?|\n/) processed = lines.reject { |line| line =~ /\s*#/ } return processed.join("\n") end |
#scan_parameters(phrase, mode) ⇒ Object
“Scanning parameters” means that a phrase will be scanned to find any text between chevrons or double quotes. These will be placed into an array.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 66 def scan_parameters(phrase, mode) pattern = case mode when :define /<((?:[^\\<>]|\\.)*)>/ when :invoke /"((?:[^\\"]|\\.)*)"/ end result = phrase.scan(pattern) params = result.flatten.compact # Any escaped double quotes need to be replaced by a double quote. params.map! { |item| item.sub(/\\"/, '"') } if mode == :invoke return params end |
#validate_params(phrase, data) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 127 def validate_params(phrase, data) sequence_parameters = {} quoted_values = scan_parameters(phrase, :invoke) quoted_values.each_with_index do |val, index| sequence_parameters[phrase_params[index]] = val end unless data.nil? data.each do |row| (key, value) = validate_row(row, sequence_parameters) if sequence_parameters.include? key if sequence_parameters[key].kind_of?(Array) sequence_parameters[key] << value else sequence_parameters[key] = [sequence_parameters[key], value] end else sequence_parameters[key] = value end end end return sequence_parameters end |
#validate_phrase_values(params, phrase_variables) ⇒ Object
The goal of this method is to check for various inconsistencies that can occur between parameter names in the sequence phrase and those in the actual steps.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 95 def validate_phrase_values(params, phrase_variables) # This covers the case when phrase names a parameter that never # occurs in any of the steps. params.each do |param| unless phrase_variables.include? param raise UselessPhraseParameter.new(param) end end # This covers the case when a step has a parameter that never appears # in the sequence phrase and when data table is not being used. unless data_table_required? phrase_variables.each do |variable| unless params.include?(variable) || ParameterConstant.include?(variable) raise UnreachableStepParameter.new(variable) end end end return phrase_params.dup end |
#validate_row(row, params) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/lucid/sequence/sequence_phrase.rb', line 153 def validate_row(row, params) (key, value) = row raise UnknownParameterError.new(key) unless values.include? key if (phrase_params.include? key) && (params[key] != value) raise AmbiguousParameterValue.new(key, params[key], value) end return row end |