Class: MongodbLogger::ServerModel::AdditionalFilter

Inherits:
Base
  • Object
show all
Defined in:
lib/mongodb_logger/server/model/additional_filter.rb

Constant Summary collapse

FORM_NAME =
"more"
FIXED_PARAMS_ON_FORM =
['type', 'key', 'condition', 'value']
VAR_TYPES =
["integer", "string", "boolean", "date"]
VAR_TYPE_CONDITIONS =
[
  ["equals", "not equals", "regexes", "<", "<=", ">=", ">"],
  ["equals", "not equals", "regexes", "<", "<=", ">=", ">"],
  ["equals", "exists"],
  ["<", "<=", ">=", ">"]
]
VAR_TYPE_VALUES =
[
  [],
  [],
  ["true", "false"],
  []
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#create_variable, #set_params_to_methods

Constructor Details

#initialize(params, filter_model) ⇒ AdditionalFilter

Returns a new instance of AdditionalFilter.



28
29
30
31
32
33
34
35
# File 'lib/mongodb_logger/server/model/additional_filter.rb', line 28

def initialize(params, filter_model)
  @filter_model = filter_model
  @params = params
  FIXED_PARAMS_ON_FORM.each do |key|
    create_variable(key, nil)
  end
  set_params_to_methods
end

Instance Attribute Details

#filter_modelObject (readonly)

Returns the value of attribute filter_model.



26
27
28
# File 'lib/mongodb_logger/server/model/additional_filter.rb', line 26

def filter_model
  @filter_model
end

#form_dataObject (readonly)

Returns the value of attribute form_data.



26
27
28
# File 'lib/mongodb_logger/server/model/additional_filter.rb', line 26

def form_data
  @form_data
end

Class Method Details

.get_type_index(type) ⇒ Object



37
38
39
# File 'lib/mongodb_logger/server/model/additional_filter.rb', line 37

def self.get_type_index(type)
  type.nil? ? 0 : VAR_TYPES.index(type)
end

Instance Method Details

#form_nameObject



53
54
55
# File 'lib/mongodb_logger/server/model/additional_filter.rb', line 53

def form_name
  "#{filter_model.form_name}[#{FORM_NAME}][]"
end

#get_type_indexObject



41
42
43
# File 'lib/mongodb_logger/server/model/additional_filter.rb', line 41

def get_type_index
  @type.nil? ? 0 : VAR_TYPES.index(@type)
end

#is_selected_values?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/mongodb_logger/server/model/additional_filter.rb', line 49

def is_selected_values?
  !VAR_TYPE_VALUES[get_type_index].blank?
end

#mongo_conditionsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mongodb_logger/server/model/additional_filter.rb', line 57

def mongo_conditions
  data = Hash.new
  return data if self.key.blank?
  m_value = case self.type
  when "integer"
    self.value.to_i
  when "boolean"
    ("true" == self.value || "1" == self.value) ? true : false
  when "date"
    val_date = Date.parse(self.value) rescue nil
    Time.utc(val_date.year, val_date.month, val_date.day) unless val_date.nil?
  else
    self.value
  end
  data = case self.condition
  when "equals"
    {"#{self.key}" => m_value }
  when "not equals"
    {"#{self.key}" => { "$ne" => m_value }}
  when "exists"
    {"#{self.key}" => { "$exists" => m_value }}
  when "regexes"
    {"#{self.key}" => { "$regex" => m_value, "$options" => 'i' }}
  when "<"
    {"#{self.key}" => { "$lt" => m_value }}
  when "<="
    {"#{self.key}" => { "$lte" => m_value }}
  when ">"
    {"#{self.key}" => { "$gt" => m_value }}
  when ">="
    {"#{self.key}" => { "$gte" => m_value }}
  else
    Hash.new
  end
  data
end

#selected_valuesObject



45
46
47
# File 'lib/mongodb_logger/server/model/additional_filter.rb', line 45

def selected_values
  VAR_TYPE_VALUES[get_type_index]
end