Class: ActiveHash::Relation
- Inherits:
-
Object
- Object
- ActiveHash::Relation
- Includes:
- Enumerable
- Defined in:
- lib/active_hash/relation.rb
Instance Attribute Summary collapse
-
#all_records ⇒ Object
readonly
Returns the value of attribute all_records.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#query_hash ⇒ Object
readonly
Returns the value of attribute query_hash.
-
#records_dirty ⇒ Object
readonly
Returns the value of attribute records_dirty.
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #count ⇒ Object
- #find(id = nil, *args, &block) ⇒ Object
- #find_by(options) ⇒ Object
- #find_by!(options) ⇒ Object
- #find_by_id(id) ⇒ Object
-
#initialize(klass, all_records, query_hash = nil) ⇒ Relation
constructor
A new instance of Relation.
- #order(*options) ⇒ Object
- #pluck(*column_names) ⇒ Object
- #reload ⇒ Object
- #to_ary ⇒ Object
- #where(query_hash = :chain) ⇒ Object
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_records ⇒ Object
Returns the value of attribute all_records.
95 96 97 |
# File 'lib/active_hash/relation.rb', line 95 def all_records @all_records end |
#klass ⇒ Object
Returns the value of attribute klass.
95 96 97 |
# File 'lib/active_hash/relation.rb', line 95 def klass @klass end |
#query_hash ⇒ Object
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_dirty ⇒ Object
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( = {}) if .has_key?(:conditions) where([:conditions]) else where({}) end end |
#count ⇒ Object
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() where().first end |
#find_by!(options) ⇒ Object
38 39 40 |
# File 'lib/active_hash/relation.rb', line 38 def find_by!() find_by() || (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(*) check_if_method_has_arguments!(:order, ) relation = where({}) return relation if .blank? processed_args = preprocess_order_args() 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 |
#reload ⇒ Object
73 74 75 |
# File 'lib/active_hash/relation.rb', line 73 def reload @records = filter_all_records_by_query_hash end |
#to_ary ⇒ Object
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 |