Class: Mustermann::Expander
- Inherits:
-
Object
- Object
- Mustermann::Expander
- Defined in:
- lib/mustermann/expander.rb
Overview
Allows fine-grained control over pattern expansion.
Instance Attribute Summary collapse
-
#additional_values ⇒ Object
readonly
Returns the value of attribute additional_values.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Instance Method Summary collapse
-
#add(*patterns) ⇒ Mustermann::Expander
(also: #<<)
Add patterns to expand.
-
#cast(*types, &block) ⇒ Mustermann::Expander
Register a block as simple hash transformation that runs before expanding the pattern.
-
#expand(behavior = nil, **values) ⇒ String
Expanded string.
-
#initialize(*patterns, additional_values: :raise, **options) ⇒ Expander
constructor
A new instance of Expander.
Constructor Details
#initialize(*patterns, additional_values: :raise, **options) ⇒ Expander
Returns a new instance of Expander.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mustermann/expander.rb', line 20 def initialize(*patterns, additional_values: :raise, **) unless additional_values == :raise or additional_values == :ignore or additional_values == :append raise ArgumentError, "Illegal value %p for additional_values" % additional_values end @patterns = [] @api_expander = AST::Expander.new @additional_values = additional_values @options = @caster = Caster.new(Caster::Nil) add(*patterns) end |
Instance Attribute Details
#additional_values ⇒ Object (readonly)
Returns the value of attribute additional_values.
15 16 17 |
# File 'lib/mustermann/expander.rb', line 15 def additional_values @additional_values end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
15 16 17 |
# File 'lib/mustermann/expander.rb', line 15 def patterns @patterns end |
Instance Method Details
#add(*patterns) ⇒ Mustermann::Expander Also known as: <<
Add patterns to expand.
42 43 44 45 46 47 48 49 50 |
# File 'lib/mustermann/expander.rb', line 42 def add(*patterns) patterns.each do |pattern| pattern = Mustermann.new(pattern.to_str, **@options) if pattern.respond_to? :to_str raise NotImplementedError, "expanding not supported for #{pattern.class}" unless pattern.respond_to? :to_ast @api_expander.add(pattern.to_ast) @patterns << pattern end self end |
#cast {|key, value| ... } ⇒ Mustermann::Expander #cast(*type_matchers) {|key, value| ... } ⇒ Mustermann::Expander #cast(*cast_objects) ⇒ Mustermann::Expander
Register a block as simple hash transformation that runs before expanding the pattern.
110 111 112 113 |
# File 'lib/mustermann/expander.rb', line 110 def cast(*types, &block) caster.register(*types, &block) self end |
#expand(behavior = nil, **values) ⇒ String
Returns expanded string.
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/mustermann/expander.rb', line 140 def (behavior = nil, **values) values = caster.cast(values) case behavior || additional_values when :raise then @api_expander.(values) when :ignore then with_rest(values) { |uri, rest| uri } when :append then with_rest(values) { |uri, rest| append(uri, rest) } else raise ArgumentError, "unknown behavior %p" % behavior end end |