Class: Edgar::SearchForm::Operator

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

Overview

Store comparison-operator for each search field

Constant Summary

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

Instance Method Summary (collapse)

Constructor Details

- (Operator) initialize(attrs = {})

accepts only allowed operators to avoid SQL injection



63
64
65
66
67
68
69
70
71
# File 'app/models/edgar/search_form.rb', line 63

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

- (Object) method_missing(method_name, *args)



88
89
90
# File 'app/models/edgar/search_form.rb', line 88

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

Instance Method Details

- (Object) exp(attr)

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

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



76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/edgar/search_form.rb', line 76

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