Class: Coppertone::MacroString::MacroParser
- Inherits:
-
Object
- Object
- Coppertone::MacroString::MacroParser
- Defined in:
- lib/coppertone/macro_string/macro_parser.rb
Overview
A internal class that parses the macro string template into an object that can later be evaluated (or ‘expanded’) in the context of a particular SPF check.
Constant Summary collapse
- SIMPLE_MACRO_LETTERS =
%w[% _ -].freeze
Instance Attribute Summary collapse
-
#macros ⇒ Object
readonly
Returns the value of attribute macros.
Instance Method Summary collapse
-
#initialize(s) ⇒ MacroParser
constructor
A new instance of MacroParser.
- #parse_contextual_interpolated_macro ⇒ Object
- #parse_interpolated_macro ⇒ Object
- #parse_macro_array ⇒ Object
- #parse_macro_literal ⇒ Object
- #starting_macro? ⇒ Boolean
Constructor Details
#initialize(s) ⇒ MacroParser
Returns a new instance of MacroParser.
12 13 14 15 16 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 12 def initialize(s) @s = s.dup @macros = [] parse_macro_array end |
Instance Attribute Details
#macros ⇒ Object (readonly)
Returns the value of attribute macros.
11 12 13 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 11 def macros @macros end |
Instance Method Details
#parse_contextual_interpolated_macro ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 32 def parse_contextual_interpolated_macro raise MacroStringParsingError unless @s[1] == '{' closing_index = @s.index('}') raise MacroStringParsingError unless closing_index interpolated_body = @s[2, closing_index - 2] @macros << MacroExpand.new(interpolated_body) @s = @s[(closing_index + 1)..-1] end |
#parse_interpolated_macro ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 42 def parse_interpolated_macro raise MacroStringParsingError if @s.length == 1 macro_code = @s[0, 2] if MacroStaticExpand.exists_for?(macro_code) @macros << MacroStaticExpand.macro_for(macro_code) @s = @s[2..-1] else parse_contextual_interpolated_macro end end |
#parse_macro_array ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 18 def parse_macro_array while @s && !@s.empty? if starting_macro? parse_interpolated_macro else parse_macro_literal end end end |
#parse_macro_literal ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 53 def parse_macro_literal new_idx = @s.index('%') new_idx ||= @s.length @macros << MacroLiteral.new(@s[0, new_idx]) @s = @s[new_idx..-1] new_idx end |
#starting_macro? ⇒ Boolean
28 29 30 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 28 def starting_macro? @s && @s.length >= 1 && (@s[0] == '%') end |