Commit 4d691003 authored by John E. Vincent's avatar John E. Vincent

refactor of config/app models. linking support wip

parent 0c7922b2
......@@ -5,7 +5,7 @@ class Noah::App
if app.nil?
halt 404
else
c = Noah::Configuration.find(:name => config, :application_id => app.id).first
c = app.configurations.find(:name => config).first
c.to_json
end
end
......@@ -50,10 +50,12 @@ class Noah::App
if app.nil?
halt 404
else
configurations = []
Noah::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
# configs are no longer tied to apps. remove the cascade delete
# configurations = []
# Noah::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
app.delete
r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"}
# r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"}
r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}"}
r.to_json
end
end
......
......@@ -11,7 +11,7 @@ class Noah::App
if a.nil?
halt 404
else
c = Noah::Configuration.find(:name => element, :application_id => a.id).first
c = a.configurations.find(:name => element).first
content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym]
c.body
end
......@@ -23,7 +23,7 @@ class Noah::App
if a.nil?
halt 404
else
Noah::Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash}
a.configurations.sort.each {|c| config << c.to_hash}
config.to_json
end
end
......@@ -48,14 +48,16 @@ class Noah::App
put '/configurations/:appname/:element?' do |appname, element|
app = Noah::Application.find_or_create(:name => appname)
config = Noah::Configuration.find_or_create(:name => element, :application_id => app.id)
dependency_action = app.is_new? ? "created" : "updated"
config = Noah::Configuration.find_or_create(:name => element)
required_params = ["format", "body"]
data = JSON.parse(request.body.read)
data.keys.sort == required_params.sort ? (config.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters")
if config.valid?
config.save
app.configurations << config
app.save
action = config.is_new? ? "create" : "update"
dependency_action = app.is_new? ? "created" : "updated"
r = {"result" => "success","id" => "#{config.id}", "action" => action, "dependencies" => dependency_action, "application" => app.name, "item" => config.name}
r.to_json
else
......
......@@ -51,13 +51,12 @@ module Noah
end
end
def watch!(opts={:endpoint => nil, :pattern => nil})
def watch!(opts={:endpoint => nil})
base_pattern = "#{self.patternize_me}"
opts[:endpoint].nil? ? (raise ArgumentError, "Need an endpoint") : endpoint=opts[:endpoint]
opts[:pattern].nil? ? pattern=base_pattern : pattern=opts[:pattern]
begin
w = Watcher.new :pattern => pattern, :endpoint => endpoint
w = Watcher.new :pattern => base_pattern, :endpoint => endpoint
w.valid? ? w.save : (raise "#{w.errors}")
w.name
rescue Exception => e
......
......@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), 'configurations')
module Noah
class Application < Model
attribute :name
collection :configurations, Configuration
set :configurations, Configuration
index :name
......@@ -14,8 +14,9 @@ module Noah
def to_hash
arr = []
configurations.sort.each {|c| arr << c.to_hash}
super.merge(:name => name, :configuration => arr, :created_at => created_at, :updated_at => updated_at)
self.configurations.sort.each {|c| arr << c.to_hash} if self.configurations.size != 0
super.merge(:name => name, :configurations => arr, :created_at => created_at, :updated_at => updated_at)
#super.merge(:name => name, :created_at => created_at, :updated_at => updated_at)
end
class << self
......
require File.join(File.dirname(__FILE__), 'applications')
module Noah
class Configuration < Model
attribute :name
attribute :format
attribute :body
attribute :new_record
reference :application, Application
index :name
index :format
......@@ -16,13 +15,11 @@ module Noah
assert_present :name
assert_present :format
assert_present :body
assert_present :application_id
assert_unique [:name, :application_id]
assert_unique :name
end
def to_hash
Application[application_id].nil? ? app_name=nil : app_name=Application[application_id].name
super.merge(:name => name, :format => format, :body => body, :created_at => created_at, :updated_at => updated_at, :application => app_name)
super.merge(:name => name, :format => format, :body => body, :created_at => created_at, :updated_at => updated_at)
end
class << self
......
require File.join(File.dirname(__FILE__), 'link_member')
module Noah
class Link < Model
attribute :path
list :nodes, LinkMember
attribute :nodes
index :path
index :nodes
def nodes
arr = []
self.key[:nodes].smembers.each do |node|
arr << node
end
arr
end
def nodes=(node)
case node.class.to_s
when "Array"
node.each do |n|
self.key[:nodes].sadd(n.key)
end
else
self.key[:nodes].sadd(node.key)
end
end
def validate
super
assert_present :path
end
# Nothing to see yet.
# This will be for creating "overlays" or "link" relationships
# between arbitrary objects or modeling your data the way you want.
#
# Example:
# path = "/myservers"
# path.nodes = ["/applications/myapp","/some/ephemeral/path", "sometag"]
#
# would result in a structure like:
# path/
# applications/
# myapp
# some/ephemeral/path/
# child1
# child2
# child3
# sometag/
# tagged_item1
# tagged_item2
# tagged_item4
# tagged_item5
#
# The idea is to create a blended view across opinionated, tagged and
# ephemeral nodes.
#
# Almost a "choose your own model" thing.
def to_hash
n = Array.new
nodes.each {|node| n << node_to_class(node).to_hash} if nodes.size > 0
h = {:name => name, :nodes => n, :created_at => created_at, :updated_at => updated_at}
super.merge(h)
end
def name
@name = path
end
private
def node_to_class(node)
node.match(/^Noah::(.*):(\d+)$/)
Noah.const_get($1).send(:[], $2)
end
end
end
module Noah
class LinkMember
class <<self
def create(path)
path_to_instance(path)
end
protected
def path_to_instance(path)
p = path.split('/')
model, name = p[1], p[2]
x = instance_eval(Noah::PATH_MAPPING[model])
x.find(:name => name).first
end
end
end
end
......@@ -9,6 +9,13 @@ module Noah
def self.[](id)
super(encode(id)) || create(:id => encode(id))
end
def tagged
end
def all
end
end
end
......@@ -23,7 +23,7 @@ describe "Using the Application Model", :reset_redis => true do
end
it "create a new Noah::Application with Configurations" do
a = Noah::Application.create(@appdata1)
a.configurations << Noah::Configuration.create(@appconf_string.merge({:application => a}))
a.configurations << Noah::Configuration.create(@appconf_string)
a.valid?.should == true
a.is_new?.should == true
a.save
......
......@@ -3,13 +3,11 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Configuration Model", :reset_redis => true do
before(:each) do
Ohm.redis.flushdb
app = Noah::Application.create :name => "my_application"
@appconf_string = {:name => "mystringconf", :format => "string", :body => "some_var", :application_id => app.id}
@appconf_json = {:name => "myjsonconf", :format => "json", :body => @appconf_string.to_json, :application_id => app.id}
@appconf_string = {:name => "mystringconf", :format => "string", :body => "some_var"}
@appconf_json = {:name => "myjsonconf", :format => "json", :body => @appconf_string.to_json}
@appconf_missing_name = @appconf_string.reject {|k, v| k == :name}
@appconf_missing_format = @appconf_string.reject {|k, v| k == :format}
@appconf_missing_body = @appconf_string.reject {|k, v| k == :body}
@appconf_missing_application = @appconf_string.reject {|k, v| k == :application_id}
end
after(:each) do
Ohm.redis.flushdb
......@@ -74,15 +72,10 @@ describe "Using the Configuration Model", :reset_redis => true do
a.valid?.should == false
a.errors.should == [[:body, :not_present]]
end
it "create a new Confguration without an application" do
a = Noah::Configuration.create(@appconf_missing_application)
a.valid?.should == false
a.errors.should == [[:application_id, :not_present]]
end
it "create a duplicate Configuration" do
a = Noah::Configuration.create(@appconf_string)
b = Noah::Configuration.create(@appconf_string)
b.errors.should == [[[:name, :application_id], :not_unique]]
b.errors.should == [[:name, :not_unique]]
end
end
......
......@@ -3,9 +3,9 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Application API", :reset_redis => false do
before(:all) do
@a = Noah::Application.create(:name => 'rspec_sample_app')
@a.configurations << Noah::Configuration.create(:name => 'rspec_config', :format => 'string', :body => 'rspec is great', :application => @a)
@c = Noah::Configuration.create(:name => 'rspec_config', :format => 'string', :body => 'rspec is great')
@a.configurations << @c
@a.save
@c = @a.configurations.first
end
describe "calling" do
......@@ -38,7 +38,6 @@ describe "Using the Application API", :reset_redis => false do
response["name"].should == @c.name
response["format"].should == @c.format
response["body"].should == @c.body
response["application"].should == @a.name
end
it "invalid application should not work" do
get "/applications/should_not_exist"
......@@ -96,7 +95,6 @@ describe "Using the Application API", :reset_redis => false do
response["action"].should == "delete"
response["id"].nil?.should == false
response["name"].should == @appdata[:name]
response["configurations"].should == "0"
end
it "invalid application should not work" do
delete "/applications/should_not_work"
......
......@@ -18,7 +18,6 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_
response.first["name"].should == "redis"
response.first["format"].should == "string"
response.first["body"].should == "redis://127.0.0.1:6379/0"
response.first["application"].should == "noah"
end
it "named configuration for application should work" do
get '/configurations/noah/redis'
......@@ -86,7 +85,7 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_
describe "DELETE" do
before(:all) do
@a = Noah::Application.create(:name => 'delete_test_app')
cparms = {:name => 'a', :format => 'string', :body => 'asdf', :application_id => @a.id}
cparms = {:name => 'a', :format => 'string', :body => 'asdf'}
@a.configurations << Noah::Configuration.create(cparms)
@a.save
@c = @a.configurations.first
......
......@@ -29,9 +29,9 @@ RSpec.configure do |config|
a = Noah::Application.create(:name => 'noah')
if a.save
cr = Noah::Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a)
ch = Noah::Configuration.create(:name => 'host', :format => 'string', :body => 'localhost', :application => a)
cp = Noah::Configuration.create(:name => 'port', :format => 'string', :body => '9292', :application => a)
cr = Noah::Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0')
ch = Noah::Configuration.create(:name => 'host', :format => 'string', :body => 'localhost')
cp = Noah::Configuration.create(:name => 'port', :format => 'string', :body => '9292')
%w[cr ch cp].each do |c|
a.configurations << eval(c)
end
......@@ -53,13 +53,13 @@ EOJ
a1 = Noah::Application.create(:name => 'myrailsapp1')
if a1.save
c1 = Noah::Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1)
c1 = Noah::Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml)
a1.configurations << c1
end
a2 = Noah::Application.create(:name => 'myrestapp1')
if a2.save
c2 = Noah::Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2)
c2 = Noah::Configuration.create(:name => 'config.json', :format => 'json', :body => my_json)
a2.configurations << c2
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