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

changing tagging support

parent d849eb55
...@@ -10,8 +10,6 @@ module Noah ...@@ -10,8 +10,6 @@ module Noah
model.send :include, Ohm::Callbacks model.send :include, Ohm::Callbacks
model.send :include, Ohm::ExtraValidations model.send :include, Ohm::ExtraValidations
model.send :attribute, :tags
model.send :index, :tag
model.send :attribute, :metadata model.send :attribute, :metadata
# removing this as it's simply redundant # removing this as it's simply redundant
...@@ -34,10 +32,6 @@ module Noah ...@@ -34,10 +32,6 @@ module Noah
self.created_at == self.updated_at self.created_at == self.updated_at
end end
def tag(tags = self.tags)
tags.to_s.split(/\s*,\s*/).uniq
end
def link!(path) def link!(path)
base_pattern = "#{self.patternize_me}" base_pattern = "#{self.patternize_me}"
path.nil? ? (raise ArgumentError, "Must provide a path") : p=path path.nil? ? (raise ArgumentError, "Must provide a path") : p=path
...@@ -86,15 +80,6 @@ module Noah ...@@ -86,15 +80,6 @@ module Noah
o[:db].nil? ? "#{o[:url].split('/').last}" : "#{o[:db]}" o[:db].nil? ? "#{o[:url].split('/').last}" : "#{o[:db]}"
end 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| ["create", "update", "delete"].each do |meth|
class_eval do class_eval do
define_method("notify_via_redis_#{meth}".to_sym) do define_method("notify_via_redis_#{meth}".to_sym) do
...@@ -128,6 +113,7 @@ module Noah ...@@ -128,6 +113,7 @@ module Noah
end end
require File.join(File.dirname(__FILE__), 'models','tags') 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','hosts')
require File.join(File.dirname(__FILE__), 'models','services') require File.join(File.dirname(__FILE__), 'models','services')
require File.join(File.dirname(__FILE__), 'models','applications') require File.join(File.dirname(__FILE__), 'models','applications')
......
require File.join(File.dirname(__FILE__), 'configurations') require File.join(File.dirname(__FILE__), 'configurations')
module Noah module Noah
class Application < Model class Application < Model
include Taggable
attribute :name attribute :name
set :configurations, Configuration set :configurations, Configuration
......
require File.join(File.dirname(__FILE__), 'applications') require File.join(File.dirname(__FILE__), 'applications')
module Noah module Noah
class Configuration < Model class Configuration < Model
include Taggable
attribute :name attribute :name
attribute :format attribute :format
attribute :body attribute :body
......
module Noah module Noah
class Ephemeral < Model class Ephemeral < Model
include EphemeralValidations include EphemeralValidations
include Taggable
attribute :path attribute :path
attribute :data attribute :data
attribute :lifetime attribute :lifetime
......
...@@ -3,7 +3,7 @@ module Noah ...@@ -3,7 +3,7 @@ module Noah
class Host < Model class Host < Model
# Host model # Host model
# @return {Host} a {Host} object # @return {Host} a {Host} object
include Taggable
attribute :name attribute :name
attribute :status attribute :status
collection :services, Service collection :services, Service
......
module Noah module Noah
class Service < Model class Service < Model
include Taggable
attribute :name attribute :name
attribute :status attribute :status
reference :host, Host reference :host, Host
......
module Noah module Noah
class Tag < Model class Tag < Model
counter :total attribute :name
index :name
def name def validate
@name = id super
assert_present :name
assert_unique :name
end end
def self.[](id) def tagged(tag)
super(encode(id)) || create(:id => encode(id)) # TODO:
#logic to find all models with a given tag
#will need hooks added to taggable module
end 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 end
def all
end end
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