class Yapt::Filter

Attributes

key[R]
val[R]

Public Class Methods

new(a,b=nil) click to toggle source
# File lib/yapt/filter.rb, line 22
def initialize(a,b=nil)
  if b
    @key, @val = a.to_s, b.to_s
  else
    k, v = a.split(/[:=]/)
    v ? (@key, @val = k, v) : @val = k
  end
  clean_key!
  clean_val!
end
parse(args) click to toggle source
# File lib/yapt/filter.rb, line 5
def self.parse(args)
  args.inject({filter: ""}) do |query, arg|
    filter = new(arg)
    if filter.filter?
      query[:filter] += "#{filter.to_filter} "
    else
      query[filter.key] = filter.val
    end
    query
  end
end

Public Instance Methods

filter?() click to toggle source
# File lib/yapt/filter.rb, line 17
def filter?
  not %w(limit offset).include?(key)
end
keyword?() click to toggle source
# File lib/yapt/filter.rb, line 33
def keyword?
  !key
end
to_filter() click to toggle source
# File lib/yapt/filter.rb, line 37
def to_filter
  keyword? ? val : coloned
end

Private Instance Methods

clean_key!() click to toggle source
# File lib/yapt/filter.rb, line 47
def clean_key!
  # ???
end
clean_val!() click to toggle source
# File lib/yapt/filter.rb, line 51
def clean_val!
  if val
    if time_filter_fields.include?(key)
      @val = dated(Chronic.parse(val))
    end
  end
end
coloned() click to toggle source
# File lib/yapt/filter.rb, line 43
def coloned
  %{#{key}:"#{val}"}
end
dated(time) click to toggle source
# File lib/yapt/filter.rb, line 67
def dated(time)
  Date.parse(time.strftime('%Y/%m/%d'))
end
time_filter_fields() click to toggle source
# File lib/yapt/filter.rb, line 59
def time_filter_fields
  @time_filter_fields ||= %w(created modified updated accepted).collect do |event|
    ["", "on", "since", "before"].collect do |timing|
      "#{event}_#{timing}".sub(/_\Z/,'')
    end
  end.flatten
end