Class: Markawesome::AttributeParser
- Inherits:
-
Object
- Object
- Markawesome::AttributeParser
- Defined in:
- lib/markawesome/attribute_parser.rb
Overview
Parses space-separated attributes from a parameter string Supports multiple values per attribute with rightmost-wins semantics Useful for flexible syntax like “pill pulse brand” where order doesn’t matter
Class Method Summary collapse
-
.parse(params_string, attribute_schema) ⇒ Hash
Parse space-separated parameters into an attribute hash.
-
.valid_token?(token, allowed_values) ⇒ Boolean
Check if a token is valid for an attribute.
Class Method Details
.parse(params_string, attribute_schema) ⇒ Hash
Parse space-separated parameters into an attribute hash
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/markawesome/attribute_parser.rb', line 13 def self.parse(params_string, attribute_schema) return {} if params_string.nil? || params_string.strip.empty? parsed = {} tokens = params_string.strip.split(/\s+/) tokens.each do |token| attribute_schema.each do |attr_name, allowed_values| next unless allowed_values.include?(token) # Rightmost-wins: latest value for this attribute wins parsed[attr_name] = token break # Move to next token once we've matched it end # Tokens that don't match any attribute are silently ignored end parsed end |
.valid_token?(token, allowed_values) ⇒ Boolean
Check if a token is valid for an attribute
37 38 39 |
# File 'lib/markawesome/attribute_parser.rb', line 37 def self.valid_token?(token, allowed_values) allowed_values.include?(token) end |