Class: ModelSet::Conditions
- Inherits:
-
Object
- Object
- ModelSet::Conditions
- Defined in:
- lib/model_set/conditions.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
Instance Method Summary collapse
- #&(other) ⇒ Object
- #<<(clause) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(conditions, ops) ⇒ Conditions
constructor
A new instance of Conditions.
- #new(*args) ⇒ Object
- #op(type) ⇒ Object
- #terminal? ⇒ Boolean
- #to_s ⇒ Object
- #|(other) ⇒ Object
- #~ ⇒ Object
Constructor Details
#initialize(conditions, ops) ⇒ Conditions
Returns a new instance of Conditions.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/model_set/conditions.rb', line 11 def initialize(conditions, ops) @ops = ops if conditions.kind_of?(Array) @operator = conditions.first.kind_of?(Symbol) ? conditions.shift : :and if @operator == :not # In this case, :not actually means :and :not. @conditions = ~Conditions.new([:and, conditions], @ops) else raise "invalid operator :#{operator}" unless [:and, :or].include?(@operator) # Compact the conditions if possible. @conditions = [] conditions.each do |clause| self << clause end end else @conditions = [conditions] end end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
5 6 7 |
# File 'lib/model_set/conditions.rb', line 5 def conditions @conditions end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
5 6 7 |
# File 'lib/model_set/conditions.rb', line 5 def operator @operator end |
Instance Method Details
#&(other) ⇒ Object
61 62 63 |
# File 'lib/model_set/conditions.rb', line 61 def &(other) new(:and, self, other) end |
#<<(clause) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/model_set/conditions.rb', line 35 def <<(clause) return self unless clause raise 'cannot append conditions to a terminal' if terminal? clause = new(clause, @ops) unless clause.kind_of?(Conditions) if clause.operator == operator @conditions.concat(clause.conditions) else @conditions << clause end @conditions.uniq! self end |
#eql?(other) ⇒ Boolean
91 92 93 94 |
# File 'lib/model_set/conditions.rb', line 91 def eql?(other) # for uniq self.hash == other.hash end |
#hash ⇒ Object
86 87 88 89 |
# File 'lib/model_set/conditions.rb', line 86 def hash # for uniq [operator, conditions].hash end |
#new(*args) ⇒ Object
7 8 9 |
# File 'lib/model_set/conditions.rb', line 7 def new(*args) self.class.new(*args) end |
#op(type) ⇒ Object
65 66 67 |
# File 'lib/model_set/conditions.rb', line 65 def op(type) @ops[type] end |
#terminal? ⇒ Boolean
31 32 33 |
# File 'lib/model_set/conditions.rb', line 31 def terminal? operator.nil? end |
#to_s ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/model_set/conditions.rb', line 69 def to_s return conditions.first.to_s if terminal? or conditions.empty? condition_strings = conditions.collect do |condition| condition.operator == :not ? condition.to_s : "(#{condition.to_s})" end.sort_by {|s| s.size} case operator when :not then "#{op(:not)} #{condition_strings.first}" when :and then "#{condition_strings.join(op(:and))}" when :or then "#{condition_strings.join(op(:or))}" end end |
#|(other) ⇒ Object
57 58 59 |
# File 'lib/model_set/conditions.rb', line 57 def |(other) new(:or, self, other) end |
#~ ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/model_set/conditions.rb', line 49 def ~ if operator == :not conditions.first.clone else new(:not, self) end end |