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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



108
109
110
111
112
# File 'lib/active_hash/relation.rb', line 108

def method_missing(method_name, *args)
  return super unless self.klass.scopes.key?(method_name)

  instance_exec(*args, &self.klass.scopes[method_name])
end

Instance Attribute Details

#all_recordsObject

Returns the value of attribute all_records.



114
115
116
# File 'lib/active_hash/relation.rb', line 114

def all_records
  @all_records
end

#klassObject

Returns the value of attribute klass.



114
115
116
# File 'lib/active_hash/relation.rb', line 114

def klass
  @klass
end

#query_hashObject

Returns the value of attribute query_hash.



114
115
116
# File 'lib/active_hash/relation.rb', line 114

def query_hash
  @query_hash
end

#records_dirtyObject

Returns the value of attribute records_dirty.



114
115
116
# File 'lib/active_hash/relation.rb', line 114

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



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

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", klass.name, "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}", klass.name, "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}", klass.name))
end

#find_by_id(id) ⇒ Object



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

def find_by_id(id)
  return where(id: id).first if query_hash.present?

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

#idsObject



79
80
81
# File 'lib/active_hash/relation.rb', line 79

def ids
  pluck(:id)
end

#order(*options) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_hash/relation.rb', line 91

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

#pick(*column_names) ⇒ Object



83
84
85
# File 'lib/active_hash/relation.rb', line 83

def pick(*column_names)
  pluck(*column_names).first
end

#pluck(*column_names) ⇒ Object



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

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

#reloadObject



87
88
89
# File 'lib/active_hash/relation.rb', line 87

def reload
  @records = filter_all_records_by_query_hash
end

#sizeObject



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

def size
  length
end

#to_aryObject



104
105
106
# File 'lib/active_hash/relation.rb', line 104

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