class Glug::Condition

—– Condition

represents a Mapbox GL filter of the form [operator, key, value] (etc.)
can be merged with other conditions via the & and | operators

Attributes

operator[RW]
values[RW]

Public Class Methods

new() click to toggle source
# File lib/glug.rb, line 25
def initialize
        @values=[]
end

Public Instance Methods

&(cond) click to toggle source
# File lib/glug.rb, line 38
def &(cond); merge(:all,cond) end
encode() click to toggle source

Encode into an array for Mapbox GL JSON (recursive)

# File lib/glug.rb, line 54
def encode
        [@operator.to_s, *@values.map { |v| v.is_a?(Condition) ? v.encode : v } ]
end
from_key(operator, key, list) click to toggle source
# File lib/glug.rb, line 28
def from_key(operator, key, list)
        @operator = operator
        @values = [key].concat(list)
        self
end
from_list(operator, list) click to toggle source
# File lib/glug.rb, line 33
def from_list(operator, list)
        @operator = operator
        @values = list
        self
end
merge(op,cond) click to toggle source
# File lib/glug.rb, line 40
                def merge(op,cond)
                        if cond.nil?
                                self
                        elsif @operator==op
                                Condition.new.from_list(op, @values + [cond])
#                               @values << cond; self
                        elsif cond.operator==op
                                Condition.new.from_list(op, [self] + cond.values)
#                               cond.values << self; cond
                        else
                                Condition.new.from_list(op, [self, cond])
                        end
                end
to_s() click to toggle source
# File lib/glug.rb, line 57
def to_s; "<Condition #{@operator} #{@values}>" end
|(cond) click to toggle source
# File lib/glug.rb, line 39
def |(cond); merge(:any,cond) end