class Apropos::Variant
A Variant
represents a single image file that should be displayed instead of the base image in one or more conditions. These conditions are parsed from the codes in the provided code fragment. If none of the available parsers can understand the Variant's codes, then the Variant
is not considered valid.
A valid Variant
can generate a CSS rule from its matching conditions, and can be compared to other Variants based on the aggregate sort values of its matching conditions.
Attributes
path[R]
Public Class Methods
new(code_fragment, path)
click to toggle source
# File lib/apropos/variant.rb, line 14 def initialize(code_fragment, path) @code_fragment = code_fragment @path = path @_invalid_codes = [] end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/apropos/variant.rb, line 63 def <=>(other) aggregate_sort_value <=> other.aggregate_sort_value end
aggregate_sort_value()
click to toggle source
# File lib/apropos/variant.rb, line 57 def aggregate_sort_value conditions.inject(0) do |total, query_or_selector| total + query_or_selector.sort_value end end
codes()
click to toggle source
# File lib/apropos/variant.rb, line 20 def codes @_codes ||= @code_fragment.split(SEPARATOR) end
conditions()
click to toggle source
# File lib/apropos/variant.rb, line 24 def conditions parse_codes && @_conditions end
conditions_by_type()
click to toggle source
# File lib/apropos/variant.rb, line 32 def conditions_by_type @_conditions_by_type ||= {}.tap do |combination| conditions.each do |condition| combination[condition.type] = if combination[condition.type] combination[condition.type].combine(condition) else condition end end end end
invalid_codes()
click to toggle source
# File lib/apropos/variant.rb, line 28 def invalid_codes parse_codes && @_invalid_codes end
rule()
click to toggle source
# File lib/apropos/variant.rb, line 48 def rule sorted_selector_types = conditions_by_type.keys.sort condition_css = sorted_selector_types.map do |rule_type| conditions_by_type[rule_type].to_css end key = sorted_selector_types.join('+') [key] + condition_css + [path] end
valid?()
click to toggle source
# File lib/apropos/variant.rb, line 44 def valid? !conditions.empty? && @_invalid_codes.empty? end
Private Instance Methods
parse_codes()
click to toggle source
# File lib/apropos/variant.rb, line 68 def parse_codes @_conditions ||= codes.map do |code| ExtensionParser.each_parser.inject(nil) do |_, parser| query_or_selector = parser.match(code) break query_or_selector if query_or_selector end.tap do |match| # Track codes not recognized by any parser @_invalid_codes << code unless match end end.compact end