Class: Kashiwamochi::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/kashiwamochi/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Query

Returns a new instance of Query.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kashiwamochi/query.rb', line 8

def initialize(attributes = {})
  @search_params = ActiveSupport::OrderedHash.new.with_indifferent_access
  @sort_params = ActiveSupport::OrderedHash.new.with_indifferent_access
  sort_key = Kashiwamochi.config.sort_key.to_s

  attributes.each do |key, value|
    if key.to_s == sort_key
      add_sort_param key, value
    else
      add_search_param key, value
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/kashiwamochi/query.rb', line 56

def method_missing(method_id, *args, &block)
  method_name = method_id.to_s
  if method_name =~ /(.+)=$/
    super
  else
    nil
  end
end

Instance Attribute Details

#search_paramsObject (readonly)

Returns the value of attribute search_params.



6
7
8
# File 'lib/kashiwamochi/query.rb', line 6

def search_params
  @search_params
end

#sort_paramsObject (readonly)

Returns the value of attribute sort_params.



6
7
8
# File 'lib/kashiwamochi/query.rb', line 6

def sort_params
  @sort_params
end

Instance Method Details

#add_search_param(key, value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kashiwamochi/query.rb', line 22

def add_search_param(key, value)
  unless @search_params.key? key
    instance_eval <<-METHOD
      def attribute_#{key}
        search = @search_params[:#{key}]
        search.value
      end
      alias original_#{key} #{key} if defined? #{key}
      alias #{key} attribute_#{key}
    METHOD
  end

  search = Search.new(key, value)
  @search_params[search.key] = search
end

#add_sort_param(key, value) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/kashiwamochi/query.rb', line 38

def add_sort_param(key, value)
  values = Array(value)
  values.each do |v|
    sort = Sort.parse(v)
    @sort_params[sort.key] = sort if sort.valid?
  end
end

#inspectObject



101
102
103
# File 'lib/kashiwamochi/query.rb', line 101

def inspect
  "<Query search: #{@search_params}, sort: #{@sort_params}>"
end

#persisted?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/kashiwamochi/query.rb', line 105

def persisted?
  false
end

#respond_to?(method_id, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/kashiwamochi/query.rb', line 46

def respond_to?(method_id, include_private = false)
  super || respond_to_missing?(method_id, include_private)
end

#respond_to_missing?(method_id, include_private) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/kashiwamochi/query.rb', line 50

def respond_to_missing?(method_id, include_private)
  method_name = method_id.to_s
  return true if method_name =~ /(.+)=$/
  super
end

#searched?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/kashiwamochi/query.rb', line 109

def searched?
  !@search_params.empty? || !@sort_params.empty?
end

#sorts_query(*keys) ⇒ Object Also known as: sorts



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kashiwamochi/query.rb', line 65

def sorts_query(*keys)
  options = keys.extract_options!

  allowed_keys = keys.flatten.map(&:to_s).uniq
  allowed_sorts = []

  unless allowed_keys.empty?
    if options[:unordered]
      allowed_sorts = @sort_params.values
      allowed_sorts.reject! { |s| !allowed_keys.include?(s.key.to_s) }
    else
      allowed_keys.each { |key| allowed_sorts << @sort_params[key] if @sort_params.key? key }
    end
  else
    allowed_sorts = @sort_params.values
  end

  allowed_sorts.empty? ? nil : allowed_sorts.map(&:to_query).join(', ')
end

#to_option(*sort_keys) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kashiwamochi/query.rb', line 86

def to_option(*sort_keys)
  sort_keys = sort_keys.flatten.map(&:to_s).uniq
  sorts = @sort_params.values
  sorts.reject! { |s| !sort_keys.include?(s.key.to_s) } unless sort_keys.empty?

  hash = Hash[*@search_params.values.map { |search| [search.key, search.value] }.flatten]
  unless sorts.empty?
    hash[Kashiwamochi.config.sort_key] = case sorts.length
                                         when 1 then sorts.first.to_query
                                         else        sorts.map(&:to_query)
                                         end
  end
  hash
end