Class: HasMany
Instance Attribute Summary
Attributes inherited from Relation
Instance Method Summary collapse
- #<<(object, reprocitate = true) ⇒ Object
- #add(item, reprocitate = true) ⇒ Object
- #all ⇒ Object
- #build(options = {}) ⇒ Object
- #count ⇒ Object
- #destroy ⇒ Object
- #first(attributes = {}) ⇒ Object
-
#ids ⇒ Object
query.
-
#initialize(name, object) ⇒ HasMany
constructor
A new instance of HasMany.
- #last(attributes = {}) ⇒ Object
- #order(options) ⇒ Object
- #random ⇒ Object
-
#redis_key ⇒ Object
keys.
- #remove(item, reprocitate = true) ⇒ Object
- #remove_all(reprocitate = true) ⇒ Object
- #sort(options) ⇒ Object
- #where(attributes) ⇒ Object
Methods inherited from Relation
Constructor Details
#initialize(name, object) ⇒ HasMany
Returns a new instance of HasMany.
67 68 69 70 |
# File 'lib/redisant/relations.rb', line 67 def initialize name, object super name, object @reverse_name = @object.class.name.downcase end |
Instance Method Details
#<<(object, reprocitate = true) ⇒ Object
133 134 135 |
# File 'lib/redisant/relations.rb', line 133 def << object, reprocitate=true add object, reprocitate end |
#add(item, reprocitate = true) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/redisant/relations.rb', line 125 def add item, reprocitate=true if item.is_a? Array item.each { |i| add_item i } else add_item item end end |
#all ⇒ Object
151 152 153 |
# File 'lib/redisant/relations.rb', line 151 def all @objects ||= ids.map { |id| @class.find id } end |
#build(options = {}) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/redisant/relations.rb', line 118 def build ={} item = @class.new item.save add_item item item end |
#count ⇒ Object
90 91 92 |
# File 'lib/redisant/relations.rb', line 90 def count Criteria.new(self).count end |
#destroy ⇒ Object
72 73 74 75 76 77 |
# File 'lib/redisant/relations.rb', line 72 def destroy all.each do |item| item.send("#{@reverse_name}=", nil, true) end $redis.del redis_key end |
#first(attributes = {}) ⇒ Object
98 99 100 |
# File 'lib/redisant/relations.rb', line 98 def first attributes={} Criteria.new(self).first attributes end |
#ids ⇒ Object
query
86 87 88 |
# File 'lib/redisant/relations.rb', line 86 def ids Criteria.new(self).ids end |
#last(attributes = {}) ⇒ Object
102 103 104 |
# File 'lib/redisant/relations.rb', line 102 def last attributes={} Criteria.new(self).last attributes end |
#order(options) ⇒ Object
110 111 112 |
# File 'lib/redisant/relations.rb', line 110 def order Criteria.new(self).order end |
#random ⇒ Object
114 115 116 |
# File 'lib/redisant/relations.rb', line 114 def random Criteria.new(self).random end |
#redis_key ⇒ Object
keys
80 81 82 83 |
# File 'lib/redisant/relations.rb', line 80 def redis_key raise Redisant::InvalidArgument.new('Cannot make key without id') unless @object && @object.id "#{@object.class_name}:#{@object.id}:has_many:#{@name}" end |
#remove(item, reprocitate = true) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/redisant/relations.rb', line 137 def remove item, reprocitate=true return unless item if item.is_a? Array item.each {|i| remove_item i } else remove_item item end end |
#remove_all(reprocitate = true) ⇒ Object
146 147 148 149 |
# File 'lib/redisant/relations.rb', line 146 def remove_all reprocitate=true $redis.del redis_key dirty end |
#sort(options) ⇒ Object
106 107 108 |
# File 'lib/redisant/relations.rb', line 106 def sort Criteria.new(self).sort end |
#where(attributes) ⇒ Object
94 95 96 |
# File 'lib/redisant/relations.rb', line 94 def where attributes Criteria.new(self).where(attributes) end |