Class: Sakuramochi::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/sakuramochi/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



7
8
9
10
# File 'lib/sakuramochi/configuration.rb', line 7

def initialize
  @predicates = {}
  restore
end

Instance Attribute Details

#predicatesObject

Returns the value of attribute predicates.



5
6
7
# File 'lib/sakuramochi/configuration.rb', line 5

def predicates
  @predicates
end

Instance Method Details

#add(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sakuramochi/configuration.rb', line 12

def add(*args)
  options = args.extract_options!
  options.reverse_merge!(:grouping => true)

  suffixes = [nil]
  suffixes += ['any', 'all'] if options[:grouping]

  args.flatten.each do |name|
    name = name.to_s
    suffixes.each do |suffix|
      predicate_name = [name, suffix].compact.join('_')
      @predicates[predicate_name] = Predicate.new(options.merge({
        :name => predicate_name,
        :arel_predicate => [options[:arel_predicate], suffix].compact.join('_')
      }))
    end
  end 
end

#clearObject



31
32
33
# File 'lib/sakuramochi/configuration.rb', line 31

def clear
  @predicates.clear
end

#restoreObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sakuramochi/configuration.rb', line 35

def restore
  clear

  names = {
    :contains =>    [:contains],
    :starts_with => [:starts_with, :start_with],
    :ends_with =>   [:ends_with, :end_with],
    :in =>          [:in],
    :eq =>          [:eq, :equal, :equals],
    :gt =>          [:gt],
    :gte =>         [:gte, :gteq],
    :lt =>          [:lt],
    :lte =>         [:lte, :lteq],
  }
  negative = proc { |p| "not_#{p}" }

  add names[:contains],                   :arel_predicate => :matches,        :converter => proc { |v| "%#{v}%" }
  add names[:contains].map(&negative),    :arel_predicate => :does_not_match, :converter => proc { |v| "%#{v}%" }
  add names[:starts_with],                :arel_predicate => :matches,        :converter => proc { |v| "#{v}%" }
  add names[:starts_with].map(&negative), :arel_predicate => :does_not_match, :converter => proc { |v| "#{v}%" }
  add names[:ends_with],                  :arel_predicate => :matches,        :converter => proc { |v| "%#{v}" }
  add names[:ends_with].map(&negative),   :arel_predicate => :does_not_match, :converter => proc { |v| "%#{v}" }
  add names[:in],                         :arel_predicate => :in
  add names[:in].map(&negative),          :arel_predicate => :not_in
  add names[:eq],                         :arel_predicate => :eq
  add names[:eq].map(&negative), :ne,     :arel_predicate => :not_eq
  add names[:gt],                         :arel_predicate => :gt
  add names[:gte],                        :arel_predicate => :gteq
  add names[:lt],                         :arel_predicate => :lt
  add names[:lte],                        :arel_predicate => :lteq
end