Class: Yapt::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/yapt/filter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b = nil) ⇒ Filter

Returns a new instance of Filter.



22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



21
22
23
# File 'lib/yapt/filter.rb', line 21

def key
  @key
end

#valObject (readonly)

Returns the value of attribute val.



21
22
23
# File 'lib/yapt/filter.rb', line 21

def val
  @val
end

Class Method Details

.parse(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# 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

Instance Method Details

#filter?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/yapt/filter.rb', line 17

def filter?
  not %w(limit offset).include?(key)
end

#keyword?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/yapt/filter.rb', line 33

def keyword?
  !key
end

#to_filterObject



37
38
39
# File 'lib/yapt/filter.rb', line 37

def to_filter
  keyword? ? val : coloned
end