Commit 5ea50e75 authored by John E. Vincent's avatar John E. Vincent

changing tagging support

parent d849eb55
......@@ -10,8 +10,6 @@ module Noah
model.send :include, Ohm::Callbacks
model.send :include, Ohm::ExtraValidations
model.send :attribute, :tags
model.send :index, :tag
model.send :attribute, :metadata
# removing this as it's simply redundant
......@@ -34,10 +32,6 @@ module Noah
self.created_at == self.updated_at
end
def tag(tags = self.tags)
tags.to_s.split(/\s*,\s*/).uniq
end
def link!(path)
base_pattern = "#{self.patternize_me}"
path.nil? ? (raise ArgumentError, "Must provide a path") : p=path
......@@ -86,15 +80,6 @@ module Noah
o[:db].nil? ? "#{o[:url].split('/').last}" : "#{o[:db]}"
end
def before_update
return if new?
tag(read_remote(:tags)).map(&Tag).each {|t| t.decr :total}
end
def after_save
tag.map(&Tag).each {|t| t.incr :total }
end
["create", "update", "delete"].each do |meth|
class_eval do
define_method("notify_via_redis_#{meth}".to_sym) do
......@@ -128,6 +113,7 @@ module Noah
end
require File.join(File.dirname(__FILE__), 'models','tags')
require File.join(File.dirname(__FILE__), 'taggable')
require File.join(File.dirname(__FILE__), 'models','hosts')
require File.join(File.dirname(__FILE__), 'models','services')
require File.join(File.dirname(__FILE__), 'models','applications')
......
require File.join(File.dirname(__FILE__), 'configurations')
module Noah
class Application < Model
include Taggable
attribute :name
set :configurations, Configuration
......
require File.join(File.dirname(__FILE__), 'applications')
module Noah
class Configuration < Model
include Taggable
attribute :name
attribute :format
attribute :body
......
module Noah
class Ephemeral < Model
include EphemeralValidations
include Taggable
attribute :path
attribute :data
attribute :lifetime
......
......@@ -3,7 +3,7 @@ module Noah
class Host < Model
# Host model
# @return {Host} a {Host} object
include Taggable
attribute :name
attribute :status
collection :services, Service
......
module Noah
class Service < Model
include Taggable
attribute :name
attribute :status
reference :host, Host
......
module Noah
class Tag < Model
counter :total
attribute :name
index :name
def name
@name = id
def validate
super
assert_present :name
assert_unique :name
end
def self.[](id)
super(encode(id)) || create(:id => encode(id))
def tagged(tag)
# TODO:
#logic to find all models with a given tag
#will need hooks added to taggable module
end
def tagged
class <<self
def find_or_create(opts={})
begin
find(opts).first.nil? ? obj=new(opts) : obj=find(opts).first
if obj.valid?
obj.save
end
obj
rescue Exception => e
e.message
end
end
def all
end
end
......
module Noah::Taggable
def self.included(model)
model.send :set, :tags, ::Noah::Tag
model.send :index, :tags
end
def tag!(tag_name)
tags << ::Noah::Tag.find_or_create(:name => tag_name)
end
def untag!(tag_name)
end
def to_hash
tag_arr = []
self.tags.sort.each {|t| tag_arr << t.name} if self.tags.size != 0
super.merge(:tags => tag_arr)
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment