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.
13 14 15 16 17 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 13 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
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 33 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)..] end |
#parse_interpolated_macro ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 45 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..] else parse_contextual_interpolated_macro end end |
#parse_macro_array ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 19 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
57 58 59 60 61 62 63 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 57 def parse_macro_literal new_idx = @s.index('%') new_idx ||= @s.length @macros << MacroLiteral.new(@s[0, new_idx]) @s = @s[new_idx..] new_idx end |
#starting_macro? ⇒ Boolean
29 30 31 |
# File 'lib/coppertone/macro_string/macro_parser.rb', line 29 def starting_macro? @s && @s.length >= 1 && (@s[0] == '%') end |