Class: Edgarj::SearchForm::Operator

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Conversion, ActiveModel::Naming
Defined in:
app/models/edgarj/search_form.rb

Overview

Store comparison-operator for each search field

Constant Summary collapse

ALLOWED =
HashWithIndifferentAccess.new.tap do |a|
  %w(= <> > >= < <=).each do |op|
    a[op] = true
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Operator

accepts only allowed operators to avoid SQL injection



68
69
70
71
72
73
74
75
76
# File 'app/models/edgarj/search_form.rb', line 68

def initialize(attrs = {})
  @attrs = HashWithIndifferentAccess.new

  for k, v in (attrs || {}) do
    if ALLOWED[v]
      @attrs[k] = v
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



93
94
95
# File 'app/models/edgarj/search_form.rb', line 93

def method_missing(method_name, *args)
  @attrs[method_name.to_sym]
end

Instance Method Details

#exp(attr) ⇒ Object

generate a part of expression like ‘=?’, ‘ in(?)’, etc.

When operator contains place-holder ‘?’, it is not appended.



81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/edgarj/search_form.rb', line 81

def exp(attr)
  if @attrs[attr]
    if @attrs[attr].index('?')
      @attrs[attr]
    else
      @attrs[attr] + '?'
    end
  else
    '=?'
  end
end