Class: ActiveHash::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_hash/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, all_records, query_hash = nil) ⇒ Relation

Returns a new instance of Relation.



9
10
11
12
13
14
15
# File 'lib/active_hash/relation.rb', line 9

def initialize(klass, all_records, query_hash = nil)
  self.klass = klass
  self.all_records = all_records
  self.query_hash = query_hash
  self.records_dirty = false
  self
end

Instance Attribute Details

#all_recordsObject

Returns the value of attribute all_records.



75
76
77
# File 'lib/active_hash/relation.rb', line 75

def all_records
  @all_records
end

#klassObject

Returns the value of attribute klass.



75
76
77
# File 'lib/active_hash/relation.rb', line 75

def klass
  @klass
end

#query_hashObject

Returns the value of attribute query_hash.



75
76
77
# File 'lib/active_hash/relation.rb', line 75

def query_hash
  @query_hash
end

#records_dirtyObject

Returns the value of attribute records_dirty.



75
76
77
# File 'lib/active_hash/relation.rb', line 75

def records_dirty
  @records_dirty
end

Instance Method Details

#all(options = {}) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/active_hash/relation.rb', line 25

def all(options = {})
  if options.has_key?(:conditions)
    where(options[:conditions])
  else
    where({})
  end
end

#countObject



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

def count
  length
end

#find(id, *args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_hash/relation.rb', line 41

def find(id, *args)
  case id
    when :all
      all
    when :first
      all(*args).first
    when Array
      id.map { |i| find(i) }
    when nil
      raise RecordNotFound.new("Couldn't find #{klass.name} without an ID")
    else
      find_by_id(id) || begin
        raise RecordNotFound.new("Couldn't find #{klass.name} with ID=#{id}")
      end
  end
end

#find_by(options) ⇒ Object



33
34
35
# File 'lib/active_hash/relation.rb', line 33

def find_by(options)
  where(options).first
end

#find_by!(options) ⇒ Object



37
38
39
# File 'lib/active_hash/relation.rb', line 37

def find_by!(options)
  find_by(options) || (raise RecordNotFound.new("Couldn't find #{klass.name}"))
end

#find_by_id(id) ⇒ Object



58
59
60
61
# File 'lib/active_hash/relation.rb', line 58

def find_by_id(id)
  index = klass.send(:record_index)[id.to_s] # TODO: Make index in Base publicly readable instead of using send?
  index and records[index]
end

#pluck(*column_names) ⇒ Object



67
68
69
# File 'lib/active_hash/relation.rb', line 67

def pluck(*column_names)
  column_names.map { |column_name| all.map(&column_name.to_sym) }.inject(&:zip)
end

#reloadObject



71
72
73
# File 'lib/active_hash/relation.rb', line 71

def reload
  @records = filter_all_records_by_query_hash
end

#where(query_hash = :chain) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/active_hash/relation.rb', line 17

def where(query_hash = :chain)
  return ActiveHash::Base::WhereChain.new(self) if query_hash == :chain
  
  self.records_dirty = true unless query_hash.nil? || query_hash.keys.empty?
  self.query_hash.merge!(query_hash || {})
  self
end