Sha256: 84aa12100198635f9480c9766fde9dc9d2b5688bc477847f6cc8be7db7d5f946

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

module RestfulQuery
  class InvalidOperator < Error; end;
  
  class Condition
    attr_reader :column, :value, :operator, :options
    
    OPERATOR_MAPPING = {
      'lt'   => '<',
      'gt'   => '>',
      'gteq' => '>=',
      'lteq' => '<=',
      'eq'   => '=',
      'like' => 'LIKE'
    }.freeze
      
    REVERSE_OPERATOR_MAPPING = {
      '<'    => 'lt', 
      '>'    => 'gt',  
      '>='   => 'gteq',
      '<='   => 'lteq',
      '='    => 'eq',
      'LIKE' => 'like'
    }.freeze
    
    def initialize(column, value, operator = '=', options = {})
      @options = {}
      @options = options if options.is_a?(Hash)
      self.column   = column
      self.value    = value
      self.operator = operator
    end
    
    def map_operator(operator_to_look_up, reverse = false)
      mapping = reverse ?  REVERSE_OPERATOR_MAPPING.dup : OPERATOR_MAPPING.dup
      return operator_to_look_up if mapping.values.include?(operator_to_look_up)
      found = mapping[operator_to_look_up.to_s]
    end
 
    def operator=(operator)
      @operator = map_operator(operator)
      raise(RestfulQuery::InvalidOperator, "#{@operator} is not a valid operator") unless @operator
    end
      
    def column=(column)
      @column = column.to_s
    end
    
    def value=(value)
      @value = parse_value(value)
    end
    
    def to_hash
      {column => {map_operator(operator, true) => value}}
    end
    
    def to_condition_array
      parsed_value = if operator == 'LIKE' 
          "%#{value}%"
        elsif options[:integer]
          value.to_i
        else
          value
        end
      ["#{column} #{operator} ?", parsed_value]
    end
    
    protected
    def parse_value(value)
      if options[:chronic]
        Chronic.parse(value.to_s)
      else 
        value
      end
    end
    
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
quirkey-restful_query-0.2.0 lib/restful_query/condition.rb
restful_query-0.2.0 lib/restful_query/condition.rb