Class: ActiveModel::Relation::WhereClause

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/relation/where_clause.rb

Defined Under Namespace

Classes: BlockPredicate, EqualsPredicate, NotPredicate, Predicate

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*predicates) ⇒ WhereClause

Returns a new instance of WhereClause.



80
81
82
# File 'lib/active_model/relation/where_clause.rb', line 80

def initialize(*predicates)
  @predicates = predicates.flatten(1)
end

Instance Attribute Details

#predicatesObject (readonly)

Returns the value of attribute predicates.



63
64
65
# File 'lib/active_model/relation/where_clause.rb', line 63

def predicates
  @predicates
end

Class Method Details

.build(attributes = {}, &block) ⇒ Object



73
74
75
76
77
78
# File 'lib/active_model/relation/where_clause.rb', line 73

def self.build(attributes = {}, &block)
  where_clause = new
  where_clause += from_hash(attributes) if attributes.any?
  where_clause += from_block(block) if block_given?
  where_clause
end

.from_block(block) ⇒ Object



69
70
71
# File 'lib/active_model/relation/where_clause.rb', line 69

def self.from_block(block)
  new(BlockPredicate.new(block))
end

.from_hash(attributes = {}) ⇒ Object



65
66
67
# File 'lib/active_model/relation/where_clause.rb', line 65

def self.from_hash(attributes = {})
  new(attributes.map { |attribute, value| EqualsPredicate.new(attribute, value) })
end

Instance Method Details

#+(other) ⇒ Object



84
85
86
# File 'lib/active_model/relation/where_clause.rb', line 84

def +(other)
  WhereClause.new(predicates + other.predicates)
end

#call(record) ⇒ Object



88
89
90
# File 'lib/active_model/relation/where_clause.rb', line 88

def call(record)
  predicates.all? { |predicate| predicate.call(record) }
end

#invertObject



92
93
94
# File 'lib/active_model/relation/where_clause.rb', line 92

def invert
  WhereClause.new(predicates.map(&:invert))
end

#to_procObject



96
97
98
# File 'lib/active_model/relation/where_clause.rb', line 96

def to_proc
  method(:call).to_proc
end