class HasMany

Public Class Methods

new(name, object) click to toggle source
Calls superclass method Relation::new
# File lib/redisant/relations.rb, line 67
def initialize name, object
  super name, object
  @reverse_name = @object.class.name.downcase
end

Public Instance Methods

<<(object, reprocitate=true) click to toggle source
# File lib/redisant/relations.rb, line 133
def << object, reprocitate=true
  add object, reprocitate
end
add(item, reprocitate=true) click to toggle source
# 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() click to toggle source
# File lib/redisant/relations.rb, line 151
def all
  @objects ||= ids.map { |id| @class.find id }
end
build(options={}) click to toggle source
# File lib/redisant/relations.rb, line 118
def build options={}
  item = @class.new options
  item.save
  add_item item
  item
end
count() click to toggle source
# File lib/redisant/relations.rb, line 90
def count
  Criteria.new(self).count
end
destroy() click to toggle source
# 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={}) click to toggle source
# File lib/redisant/relations.rb, line 98
def first attributes={}
  Criteria.new(self).first attributes
end
ids() click to toggle source

query

# File lib/redisant/relations.rb, line 86
def ids
  Criteria.new(self).ids
end
last(attributes={}) click to toggle source
# File lib/redisant/relations.rb, line 102
def last attributes={}
  Criteria.new(self).last attributes
end
order(options) click to toggle source
# File lib/redisant/relations.rb, line 110
def order options
  Criteria.new(self).order options
end
random() click to toggle source
# File lib/redisant/relations.rb, line 114
def random
  Criteria.new(self).random
end
redis_key() click to toggle source

keys

# 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) click to toggle source
# 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) click to toggle source
# File lib/redisant/relations.rb, line 146
def remove_all reprocitate=true
  $redis.del redis_key
  dirty
end
sort(options) click to toggle source
# File lib/redisant/relations.rb, line 106
def sort options
  Criteria.new(self).sort options
end
where(attributes) click to toggle source
# File lib/redisant/relations.rb, line 94
def where attributes
  Criteria.new(self).where(attributes)
end

Private Instance Methods

add_item(item, reprocitate=true) click to toggle source
# File lib/redisant/relations.rb, line 164
def add_item item, reprocitate=true
  return unless item
  raise Redisant::InvalidArgument.new("Wrong object type, expected #{@class.name}, got #{item.class}") unless item.is_a? Record
  raise Redisant::InvalidArgument.new("Wrong object type, expected #{@class.name}, got #{item.class}") unless item.class == @class
  $redis.sadd redis_key, item.id
  dirty
  #update reverse relation
  if reprocitate
    if item.respond_to? @reverse_name
      current_owner = item.send(@reverse_name)
      current_has_many = current_owner.send(@name) if current_owner
      if current_has_many && current_has_many != self
        current_has_many.remove( item, false )
      end
      item.send("#{@reverse_name}=", @object, false )
    end
  end
end
dirty() click to toggle source
# File lib/redisant/relations.rb, line 158
def dirty
  @ids = nil
  @count = nil
  @objects = nil
end
remove_item(item, reprocitate=true) click to toggle source
# File lib/redisant/relations.rb, line 183
def remove_item item, reprocitate=true
  if item.is_a? Integer
    id = item
  else
    klass = Inflector.pluralize(item.class_name)
    raise Redisant::InvalidArgument.new("Wrong object type, expected #{@name}, got #{klass}") unless klass == @name
    id = item.id
  end
  $redis.srem redis_key, id
  dirty
  #update reverse relation
  if reprocitate
    if item.respond_to? "#{@reverse_name}="
      item.send("#{@reverse_name}=", nil, false )
    end
  end
end