Class: Markawesome::AttributeParser

Inherits:
Object
  • Object
show all
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

Class Method Details

.parse(params_string, attribute_schema) ⇒ Hash

Parse space-separated parameters into an attribute hash

Parameters:

  • params_string (String)

    Space-separated parameter string

  • attribute_schema (Hash)

    Schema defining attributes and allowed values Example: { variant: %w[brand success], pill: [true], attention: %w[pulse bounce] }

Returns:

  • (Hash)

    Parsed attributes with resolved values



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

Parameters:

  • token (String)

    The token to check

  • allowed_values (Array)

    Allowed values for the attribute

Returns:

  • (Boolean)

    True if token is in allowed values



37
38
39
# File 'lib/markawesome/attribute_parser.rb', line 37

def self.valid_token?(token, allowed_values)
  allowed_values.include?(token)
end