Class: HasMany

Inherits:
Relation show all
Defined in:
lib/redisant/relations.rb

Instance Attribute Summary

Attributes inherited from Relation

#name, #object

Instance Method Summary collapse

Methods inherited from Relation

#object_class

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

#allObject



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 options={}
  item = @class.new options
  item.save
  add_item item
  item
end

#countObject



90
91
92
# File 'lib/redisant/relations.rb', line 90

def count
  Criteria.new(self).count
end

#destroyObject



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

#idsObject

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 options
  Criteria.new(self).order options
end

#randomObject



114
115
116
# File 'lib/redisant/relations.rb', line 114

def random
  Criteria.new(self).random
end

#redis_keyObject

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 options
  Criteria.new(self).sort options
end

#where(attributes) ⇒ Object



94
95
96
# File 'lib/redisant/relations.rb', line 94

def where attributes
  Criteria.new(self).where(attributes)
end