Class: RQuery::AttributeCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/rquery/attribute_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ AttributeCollection

Returns a new instance of AttributeCollection.



5
6
7
8
# File 'lib/rquery/attribute_collection.rb', line 5

def initialize(fields)
  @fields = fields.map{ |x| x.to_s }
  @clauses = OperationCollector.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

if the field was added upon initialization its a valid call otherwise report to the user it is invalid. Reports errors at the ruby level instead of the data store level with something like “column doesn’t exist”

example

>> user = RQuery::FieldCollection.new([:age, :name])

> #<RQuery::FieldCollection:0x1a2c21c @fields=[:age, :name]>

>> user.age

> #<RQuery::Field:0x1a2b240 @name=:age>

>> user.name

> #<RQuery::Field:0x1a2a390 @name=:name>

>> user.weight ArgumentError: The field ‘weight’ doesn’t exist for this object from /Users/johnbender/Projects/rquery/lib/rquery/where_clause.rb:20:in ‘method_missing’ from (irb):5



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rquery/attribute_collection.rb', line 26

def method_missing(symbol, *args)
  attr_str = symbol.to_s
  eq_str = attr_str.gsub(/=/, '')

  if @fields.include?(attr_str) #if the method is part of the attributes 
    add_clause(attr_str)
  elsif @fields.include?(eq_str) #if the method sans '=' is part of the attributes
    add_clause(eq_str)
    eq(*args)
  else
    raise AttributeNotFoundError, "The field '#{symbol.to_s}' doesn't exist for this object" 
  end

  #explicit return of OperationCollector, same instance returned by methods of Operation collector
  #but included here for clarity
  @clauses
end

Instance Attribute Details

#clausesObject (readonly)

Returns the value of attribute clauses.



3
4
5
# File 'lib/rquery/attribute_collection.rb', line 3

def clauses
  @clauses
end