Sha256: c3c7f2690d76bafe174f454b7b38caf3a3addac2b8c3adae7293c7723640f85e

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module WhereLower
  module Core
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def where_lower(fields)
        fields.is_a?(Hash) or raise AugumentError, 'fields is not a Hash'

        scope = self
        table = self.arel_table

        fields.each do |field_name, value|
          case value
          when Range
            value = Range.new(value.begin.to_s.downcase, value.end.to_s.downcase, value.exclude_end?)
            scope = scope.where(
              table[field_name].lower.in(value)
            )
          when Array # Assume the content to be string, or can be converted to string
            value = value.to_a.map {|x| x.to_s.downcase}
            scope = scope.where(
              table[field_name].lower.in(value)
            )
          when String
            scope = scope.where(
              table[field_name].lower.eq(table.lower(value))
            )
          else # other single value classes
            scope = scope.where(
              table[field_name].eq(value)
            )
          end
        end

        scope
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
where_lower-0.1.0 lib/where_lower/core.rb