Permalink
Browse files

[Server] Create a new nested group.

  • Loading branch information...
1 parent 39dcaa6 commit 68ee34681e7b0f97a103d64d4e51db7a1b907c28 @alloy alloy committed Jun 8, 2012
Showing with 46 additions and 21 deletions.
  1. +19 −9 lib/xcodeproj/server.rb
  2. +27 −12 spec/server_spec.rb
View
@@ -5,25 +5,22 @@ module Xcodeproj
class Project
module Object
class AbstractPBXObject
- def to_json
+ def to_json(generator = nil)
# TODO move to subclasses? e.g. #json_type
type = case self
when PBXGroup then 'group'
when PBXFileReference then 'file'
else
raise "Oh noes!"
end
- { 'type' => type, 'name' => name }
+ { 'id' => uuid, 'type' => type, 'name' => name }.to_json
end
end
end
class PBXObjectList
- def to_json
- inject({}) do |hash, object|
- hash[object.uuid] = object.to_json
- hash
- end.to_json
+ def to_json(generator = nil)
+ to_a.to_json
end
end
end
@@ -34,7 +31,11 @@ class << self
# TODO currently here for convenience while testing
def project
- Xcodeproj::Project.new(project_path)
+ @project ||= Xcodeproj::Project.new(project_path)
+ end
+
+ def reset!
+ @project = nil
end
end
@@ -50,10 +51,19 @@ def project
end
end
+ post '/groups/:id/groups' do
+ if group = project.objects[params[:id]]
+ new_group = group.groups.new(JSON.parse(params[:group]))
+ new_group.to_json
+ else
+ raise "Oh noes!"
+ end
+ end
+
private
def project
- @project ||= self.class.project
+ self.class.project
end
end
end
View
@@ -18,25 +18,40 @@ def app
app.project_path = fixture_path('AFNetworking iOS Example.xcodeproj')
end
+ after do
+ app.reset!
+ end
+
it "returns the items in the `main group'" do
get '/'
last_response.should.be.ok
- JSON.parse(last_response.body).should == {
- 'F8E469B71395759C00DB05C8' => { 'type' => 'group', 'name' => 'Networking Extensions' },
- 'F8E4696A1395739D00DB05C8' => { 'type' => 'group', 'name' => 'Classes' },
- 'F8E469ED1395812A00DB05C8' => { 'type' => 'group', 'name' => 'Images' },
- 'F8E469931395743A00DB05C8' => { 'type' => 'group', 'name' => 'Vendor' },
- 'F8E469631395739D00DB05C8' => { 'type' => 'group', 'name' => 'Frameworks' },
- 'F8E469611395739C00DB05C8' => { 'type' => 'group', 'name' => 'Products' },
- }
+ JSON.parse(last_response.body).should == [
+ { 'id' => 'F8E469B71395759C00DB05C8', 'type' => 'group', 'name' => 'Networking Extensions' },
+ { 'id' => 'F8E4696A1395739D00DB05C8', 'type' => 'group', 'name' => 'Classes' },
+ { 'id' => 'F8E469ED1395812A00DB05C8', 'type' => 'group', 'name' => 'Images' },
+ { 'id' => 'F8E469931395743A00DB05C8', 'type' => 'group', 'name' => 'Vendor' },
+ { 'id' => 'F8E469631395739D00DB05C8', 'type' => 'group', 'name' => 'Frameworks' },
+ { 'id' => 'F8E469611395739C00DB05C8', 'type' => 'group', 'name' => 'Products' },
+ ]
end
it "returns the items in a specific group" do
get '/groups/F8E469B71395759C00DB05C8'
last_response.should.be.ok
- JSON.parse(last_response.body).should == {
- 'F8FA948F150EF8C100ED4EAD' => { 'type' => 'file', 'name' => 'AFTwitterAPIClient.h' },
- 'F8FA9490150EF8C100ED4EAD' => { 'type' => 'file', 'name' => 'AFTwitterAPIClient.m' },
- }
+ JSON.parse(last_response.body).should == [
+ { 'id' => 'F8FA948F150EF8C100ED4EAD', 'type' => 'file', 'name' => 'AFTwitterAPIClient.h' },
+ { 'id' => 'F8FA9490150EF8C100ED4EAD', 'type' => 'file', 'name' => 'AFTwitterAPIClient.m' },
+ ]
+ end
+
+ it "creates a new nested group" do
+ post '/groups/F8E469B71395759C00DB05C8/groups', :group => { 'name' => 'A new child group' }.to_json
+ last_response.should.be.ok
+ group = JSON.parse(last_response.body)
+ group['type'].should == 'group'
+ group['name'].should == 'A new child group'
+
+ parent_group = app.project.objects['F8E469B71395759C00DB05C8']
+ parent_group.groups.where(:uuid => group['id']).name.should == 'A new child group'
end
end

0 comments on commit 68ee346

Please sign in to comment.