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
- #==(other) ⇒ Object
-
#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.
- #eql?(other) ⇒ Boolean
-
#expand(behavior = nil, values = {}) ⇒ String
Expanded string.
- #expandable?(values) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(*patterns, additional_values: :raise, **options, &block) ⇒ Expander
constructor
A new instance of Expander.
Constructor Details
#initialize(*patterns, additional_values: :raise, **options, &block) ⇒ Expander
Returns a new instance of Expander.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mustermann/expander.rb', line 24 def initialize(*patterns, additional_values: :raise, **, &block) raise ArgumentError, "Illegal value %p for additional_values" % additional_values unless ADDITIONAL_VALUES.include? additional_values @patterns = [] @api_expander = AST::Expander.new @additional_values = additional_values @options = @caster = Caster.new add(*patterns, &block) end |
Instance Attribute Details
#additional_values ⇒ Object (readonly)
Returns the value of attribute additional_values.
19 20 21 |
# File 'lib/mustermann/expander.rb', line 19 def additional_values @additional_values end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
19 20 21 |
# File 'lib/mustermann/expander.rb', line 19 def patterns @patterns end |
Instance Method Details
#==(other) ⇒ Object
163 164 165 166 |
# File 'lib/mustermann/expander.rb', line 163 def ==(other) return false unless other.class == self.class other.patterns == patterns and other.additional_values == additional_values end |
#add(*patterns) ⇒ Mustermann::Expander Also known as: <<
Add patterns to expand.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mustermann/expander.rb', line 44 def add(*patterns) patterns.each do |pattern| if pattern.is_a? Expander add(*pattern.patterns) next end pattern = Mustermann.new(pattern, **@options) if block_given? @api_expander.add(yield(pattern)) else raise NotImplementedError, "expanding not supported for #{pattern.class}" unless pattern.respond_to? :to_ast @api_expander.add(pattern.to_ast) end @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.
120 121 122 123 |
# File 'lib/mustermann/expander.rb', line 120 def cast(*types, &block) caster.register(*types, &block) self end |
#eql?(other) ⇒ Boolean
169 170 171 172 |
# File 'lib/mustermann/expander.rb', line 169 def eql?(other) return false unless other.class == self.class other.patterns.eql? patterns and other.additional_values.eql? additional_values end |
#expand(behavior = nil, values = {}) ⇒ String
Returns expanded string.
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/mustermann/expander.rb', line 150 def (behavior = nil, values = {}) behavior, values = nil, behavior if behavior.is_a? Hash values = map_values(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 |
#expandable?(values) ⇒ Boolean
179 180 181 182 183 |
# File 'lib/mustermann/expander.rb', line 179 def (values) return false unless values , _ = split_values(map_values(values)) @api_expander. end |
#hash ⇒ Object
175 176 177 |
# File 'lib/mustermann/expander.rb', line 175 def hash patterns.hash + additional_values.hash end |