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.



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

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.



95
96
97
# File 'lib/active_hash/relation.rb', line 95

def all_records
  @all_records
end

#klassObject

Returns the value of attribute klass.



95
96
97
# File 'lib/active_hash/relation.rb', line 95

def klass
  @klass
end

#query_hashObject

Returns the value of attribute query_hash.



95
96
97
# File 'lib/active_hash/relation.rb', line 95

def query_hash
  @query_hash
end

#records_dirtyObject

Returns the value of attribute records_dirty.



95
96
97
# File 'lib/active_hash/relation.rb', line 95

def records_dirty
  @records_dirty
end

Instance Method Details

#all(options = {}) ⇒ Object



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

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

#countObject



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

def count
  length
end

#find(id = nil, *args, &block) ⇒ Object



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

def find(id = nil, *args, &block)
  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") unless block_given?
      records.find(&block) # delegate to Enumerable#find if a block is given
    else
      find_by_id(id) || begin
        raise RecordNotFound.new("Couldn't find #{klass.name} with ID=#{id}")
      end
  end
end

#find_by(options) ⇒ Object



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

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

#find_by!(options) ⇒ Object



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

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

#find_by_id(id) ⇒ Object



60
61
62
63
# File 'lib/active_hash/relation.rb', line 60

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

#order(*options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/active_hash/relation.rb', line 77

def order(*options)
  check_if_method_has_arguments!(:order, options)
  relation = where({})
  return relation if options.blank?

  processed_args = preprocess_order_args(options)
  candidates = relation.dup

  order_by_args!(candidates, processed_args)

  candidates
end

#pluck(*column_names) ⇒ Object



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

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

#reloadObject



73
74
75
# File 'lib/active_hash/relation.rb', line 73

def reload
  @records = filter_all_records_by_query_hash
end

#to_aryObject



90
91
92
# File 'lib/active_hash/relation.rb', line 90

def to_ary
  records.dup
end

#where(query_hash = :chain) ⇒ Object



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

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