Class: Deli::Adapters::Mongoid::String

Inherits:
Param
  • Object
show all
Includes:
Param::String
Defined in:
lib/deli/adapters/mongoid.rb

Instance Attribute Summary

Attributes inherited from Param

#controller, #default, #exact, #key, #model_name, #namespace

Instance Method Summary collapse

Methods included from Param::String

#parse

Methods inherited from Param

#render_value

Methods inherited from Param

#initialize, #inspect, #parse, #parse_value

Constructor Details

This class inherits a constructor from Deli::Param

Instance Method Details

#render(value) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/deli/adapters/mongoid.rb', line 95

def render(value)
  result = parse(value).map do |children|
    regex          = []
    includes       = []
    excludes       = []
    hash           = {}
    
    children.each do |item|
      start_operator = item[:operators][1] == "^" ? "^" : ""
      end_operator   = item[:operators][1] == "$" ? "$" : ""
      
      case item[:operators][0]
      #when "="
      #  includes << item[:value]
      #when "!="
      #  excludes << item[:value]
      when "!~", "!="
        regex << Regexp.new("(?!#{start_operator}#{item[:value]}#{end_operator})", "i")
      when "=~", "="
        regex << Regexp.new("#{start_operator}#{item[:value]}#{end_operator}", "i")
      end
    end
    
    #hash["$in"]    = includes if includes.present?
    #hash["$nin"]   = excludes if excludes.present?
    hash["$regex"] = Regexp.union(*regex) if regex.present?
    hash
  end
  
  result.length > 1 ? {"$or" => result} : result[0]
end