Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
customizing hash for configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
lusis committed Apr 12, 2011
1 parent 7a10fa7 commit f11cdec
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 29 deletions.
18 changes: 6 additions & 12 deletions lib/noah/application_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ class Noah::App

get '/applications/:appname/?' do |appname|
app = Noah::Application.find(:name => appname).first
if app.nil?
halt 404
else
app.to_json
end
(halt 404) if app.nil?
app.to_json
end

put '/applications/:appname/tag' do |appname|
Expand Down Expand Up @@ -63,13 +60,10 @@ class Noah::App

delete '/applications/:appname/?' do |appname|
app = Noah::Application.find(:name => appname).first
if app.nil?
halt 404
else
app.delete
r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}"}
r.to_json
end
(halt 404) if app.nil?
app.delete
r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}"}
r.to_json
end

get '/applications/?' do
Expand Down
10 changes: 3 additions & 7 deletions lib/noah/configuration_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ class Noah::App
end

get '/configurations/?' do
configs = []
Noah::Configuration.all.sort.each {|c| configs << c.to_hash}
if configs.empty?
halt 404
else
configs.to_json
end
configs = Noah::Configurations.all.to_hash
(halt 404) if configs.size == 0
configs.to_json
end

put '/configurations/:configname/link' do |configname|
Expand Down
2 changes: 1 addition & 1 deletion lib/noah/models/applications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def to_hash
configurations.sort.each do |cfg|
cfg_hash["#{cfg.name}"] = {:format => cfg.to_hash[:format], :body => cfg.to_hash[:body]}
end
super.merge(:name => name, :created_at => created_at, :updated_at => updated_at, :configurations => cfg_hash)
{name => {:id => id, :created_at => created_at, :updated_at => updated_at, :configurations => cfg_hash}}
end

class << self
Expand Down
5 changes: 4 additions & 1 deletion lib/noah/models/configurations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def find_or_create(opts={})

class Configurations
def self.all(options = {})
options.empty? ? Configuration.all.sort : Configuration.find(options).sort
config_hash = Hash.new
options.empty? ? configs=Configuration.all.sort : configs=Configuration.find(options).sort
configs.each {|x| config_hash["#{x.name}"] = x.to_hash.reject {|k,v| k == :name} }
config_hash
end
end
end
5 changes: 3 additions & 2 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@
a = Noah::Configuration.find_or_create(@appconf_string)
b = Noah::Configuration.find_or_create(@appconf_json)
c = Noah::Configurations.all
c.class.to_s.should == 'Hash'
c.size.should == 2
c.member?(a).should == true
c.member?(b).should == true
c.has_key?(a.name).should == true
c.has_key?(b.name).should == true
end
end

Expand Down
11 changes: 5 additions & 6 deletions spec/noahapp_application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
get '/applications/rspec_sample_app'
last_response.should be_ok
response = last_response.should return_json

response["name"].should == @a.name
response["id"].should == @a.id.to_s
response["name"].should == @a.name
response.has_key?("configurations").should == true
c = response["configurations"]
response.has_key?(@a.name).should == true
response[@a.name].class.to_s.should == 'Hash'
response[@a.name]["id"].should == @a.id.to_s
response[@a.name].has_key?("configurations").should == true
c = response[@a.name]["configurations"]
c.has_key?(@c.name).should == true
c["#{@c.name}"]["format"].should == "#{@c.format}"
c["#{@c.name}"]["body"].should == "#{@c.body}"
Expand Down

0 comments on commit f11cdec

Please sign in to comment.