Class: Noah::Tag

Inherits:
Model
  • Object
show all
Defined in:
lib/noah/models/tags.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

inherited

Class Method Details

.find_or_create(opts = {}) ⇒ Object


64
65
66
67
68
69
70
71
# File 'lib/noah/models/tags.rb', line 64

def find_or_create(opts={})
  begin
    find(opts).first.nil? ? obj=create(opts) : obj=find(opts).first
    obj
  rescue Exception => e
    e.message
  end
end

Instance Method Details

#deleteObject


9
10
11
12
13
14
15
16
# File 'lib/noah/models/tags.rb', line 9

def delete
  self.members.each_value do |member_array|
    member_array.each do |mem|
      delete_member(mem)
    end
  end
  super
end

#delete_member(node) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/noah/models/tags.rb', line 41

def delete_member(node)
  nc = class_to_lower(node.class.to_s)+"s"
  # Need to refactor. Essentially there are conditions we need to check for before trying to delete a member
  # Are there any members at all?
  # Are there any members of a given class?
  # Not pretty but it works for now
  if self.members.size == 0 || self.members.has_key?(nc.to_sym) == false
    # no match so save and be done with it
    self.save
  elsif self.members[nc.to_sym].include?(node)
    Ohm.redis.srem("#{self.key}:members", "#{node.key}")
    node.untag!(self.name) if node.tags.include?(self)
    self.save
  end
end

#membersObject


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/noah/models/tags.rb', line 29

def members
  hsh = Hash.new
  self.key[:members].smembers.each do |member|
    n = node_to_class(member)
    cls = class_to_lower(n.class.to_s)
    hash_key = "#{cls}s".to_sym
    hsh[hash_key] = Array.new unless hsh.has_key?(hash_key)
    hsh[hash_key] << n
  end
  hsh
end

#members=(member) ⇒ Object


24
25
26
27
# File 'lib/noah/models/tags.rb', line 24

def members=(member)
  self.key[:members].sadd(member.key)
  member.tag! self.name unless member.tags.member?(self)
end

#to_hashObject


57
58
59
60
61
# File 'lib/noah/models/tags.rb', line 57

def to_hash
  h = {:name => name, :created_at => created_at, :updated_at => updated_at}
  h.merge!(members_to_hash)
  super.merge(h)
end

#validateObject


18
19
20
21
22
# File 'lib/noah/models/tags.rb', line 18

def validate
  super
  assert_present :name
  assert_unique :name
end