moon 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +11 -0
  3. data/Rakefile +48 -0
  4. data/lib/moon.rb +15 -0
  5. data/lib/moon/action.rb +12 -0
  6. data/lib/moon/action/base.rb +16 -0
  7. data/lib/moon/action/model.rb +10 -0
  8. data/lib/moon/action/model/create.rb +45 -0
  9. data/lib/moon/action/model/destroy.rb +42 -0
  10. data/lib/moon/action/model/index.rb +31 -0
  11. data/lib/moon/action/model/show.rb +32 -0
  12. data/lib/moon/action/model/update.rb +42 -0
  13. data/lib/moon/action/models.rb +8 -0
  14. data/lib/moon/action/models/finder.rb +43 -0
  15. data/lib/moon/action/models/initializer.rb +51 -0
  16. data/lib/moon/action/models/updater.rb +36 -0
  17. data/lib/moon/action/rebuild_arrays.rb +40 -0
  18. data/lib/moon/action/reference_object.rb +35 -0
  19. data/lib/moon/action/valid_models_required.rb +21 -0
  20. data/lib/moon/application.rb +33 -0
  21. data/lib/moon/application/rack.rb +104 -0
  22. data/lib/moon/application/routes.rb +16 -0
  23. data/lib/moon/context.rb +25 -0
  24. data/lib/moon/formatter.rb +22 -0
  25. data/lib/moon/formatter/base.rb +15 -0
  26. data/lib/moon/formatter/generic.rb +29 -0
  27. data/lib/moon/response.rb +7 -0
  28. data/lib/moon/response/base.rb +18 -0
  29. data/lib/moon/response/json.rb +26 -0
  30. data/lib/moon/response/json/collection.rb +12 -0
  31. data/lib/moon/response/json/message.rb +18 -0
  32. data/lib/moon/response/json/model.rb +16 -0
  33. data/lib/moon/response/json/validation_errors.rb +9 -0
  34. data/lib/moon/utility.rb +7 -0
  35. data/lib/moon/utility/string.rb +17 -0
  36. data/lib/moon/utility/template.rb +23 -0
  37. data/lib/moon/validator.rb +71 -0
  38. data/lib/moon/validator/format.rb +32 -0
  39. data/lib/moon/validator/length.rb +42 -0
  40. data/lib/moon/validator/presence.rb +29 -0
  41. data/spec/lib/moon/action/base_spec.rb +29 -0
  42. data/spec/lib/moon/action/model/create_spec.rb +111 -0
  43. data/spec/lib/moon/action/model/destroy_spec.rb +84 -0
  44. data/spec/lib/moon/action/model/index_spec.rb +51 -0
  45. data/spec/lib/moon/action/model/show_spec.rb +51 -0
  46. data/spec/lib/moon/action/model/update_spec.rb +91 -0
  47. data/spec/lib/moon/action/models/finder_spec.rb +36 -0
  48. data/spec/lib/moon/action/models/initializer_spec.rb +43 -0
  49. data/spec/lib/moon/action/models/updater_spec.rb +36 -0
  50. data/spec/lib/moon/action/rebuild_arrays_spec.rb +28 -0
  51. data/spec/lib/moon/action/reference_object_spec.rb +32 -0
  52. data/spec/lib/moon/action/valid_models_required_spec.rb +64 -0
  53. data/spec/lib/moon/application/rack_spec.rb +73 -0
  54. data/spec/lib/moon/application/routes_spec.rb +26 -0
  55. data/spec/lib/moon/context_spec.rb +28 -0
  56. data/spec/lib/moon/formatter/base_spec.rb +30 -0
  57. data/spec/lib/moon/formatter/generic_spec.rb +34 -0
  58. data/spec/lib/moon/formatter_spec.rb +36 -0
  59. data/spec/lib/moon/response/base_spec.rb +33 -0
  60. data/spec/lib/moon/response/json/collection_spec.rb +30 -0
  61. data/spec/lib/moon/response/json/message_spec.rb +30 -0
  62. data/spec/lib/moon/response/json/model_spec.rb +31 -0
  63. data/spec/lib/moon/response/json/validation_errors_spec.rb +21 -0
  64. data/spec/lib/moon/response/json_spec.rb +33 -0
  65. data/spec/lib/moon/utility/string_spec.rb +29 -0
  66. data/spec/lib/moon/utility/template_spec.rb +49 -0
  67. data/spec/lib/moon/validator/format_spec.rb +40 -0
  68. data/spec/lib/moon/validator/length_spec.rb +60 -0
  69. data/spec/lib/moon/validator/presence_spec.rb +45 -0
  70. data/spec/lib/moon/validator_spec.rb +83 -0
  71. data/spec/spec_helper.rb +4 -0
  72. metadata +209 -0
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::Model::Index do
4
+
5
+ before :each do
6
+ @collection = mock GOM::Object::Collection
7
+
8
+ @context = Moon::Context.new
9
+ @context.collections[:sites] = @collection
10
+
11
+ @response = mock Moon::Response::JSON::Collection
12
+ Moon::Response::JSON::Collection.stub :new => @response
13
+
14
+ described_class.collection_key = :test
15
+ @action = described_class.new @context
16
+ end
17
+
18
+ describe "perform" do
19
+
20
+ it "should initialize a collection response" do
21
+ Moon::Response::JSON::Collection.should_receive(:new).and_return(@response)
22
+ @action.perform
23
+ end
24
+
25
+ it "should return the response" do
26
+ response = @action.perform
27
+ response.should == @response
28
+ end
29
+
30
+ end
31
+
32
+ describe "self.[]" do
33
+
34
+ it "should return a class" do
35
+ result = described_class[:test]
36
+ result.should be_instance_of(Class)
37
+ end
38
+
39
+ it "should return a different class" do
40
+ result = described_class[:test]
41
+ result.should_not == described_class
42
+ end
43
+
44
+ it "should return a class connected to the given collection key" do
45
+ result = described_class[:test]
46
+ result.collection_key.should == :test
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::Model::Show do
4
+
5
+ before :each do
6
+ @model = mock Object
7
+
8
+ @context = Moon::Context.new
9
+ @context.models[:test] = @model
10
+
11
+ @response = mock Moon::Response::JSON::Model
12
+ Moon::Response::JSON::Model.stub :new => @response
13
+
14
+ described_class.model_key = :test
15
+ @action = described_class.new @context
16
+ end
17
+
18
+ describe "perform" do
19
+
20
+ it "should initialize a model response" do
21
+ Moon::Response::JSON::Model.should_receive(:new).with(:test, @model).and_return(@response)
22
+ @action.perform
23
+ end
24
+
25
+ it "should return the response" do
26
+ response = @action.perform
27
+ response.should == @response
28
+ end
29
+
30
+ end
31
+
32
+ describe "self.[]" do
33
+
34
+ it "should return a class" do
35
+ result = described_class[:test]
36
+ result.should be_instance_of(Class)
37
+ end
38
+
39
+ it "should return a different class" do
40
+ result = described_class[:test]
41
+ result.should_not == described_class
42
+ end
43
+
44
+ it "should return a class connected to the given model key" do
45
+ result = described_class[:test]
46
+ result.model_key.should == :test
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,91 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::Model::Update do
4
+
5
+ before :each do
6
+ @model = mock Object
7
+
8
+ @context = Moon::Context.new
9
+ @context.storage_name = :dump
10
+ @context.models[:model] = @model
11
+
12
+ described_class.model_symbol = :model
13
+ @action = described_class.new @context
14
+ end
15
+
16
+ describe "perform" do
17
+
18
+ before :each do
19
+ GOM::Storage.stub :store => nil
20
+ GOM::Object.stub :storage_name => :test
21
+ end
22
+
23
+ it "should fetch the storage name of the model" do
24
+ GOM::Object.should_receive(:storage_name).with(@model).and_return(:test)
25
+ @action.perform
26
+ end
27
+
28
+ it "should store the model on the right storage" do
29
+ GOM::Storage.should_receive(:store).with(@model, :test)
30
+ @action.perform
31
+ end
32
+
33
+ it "should return 'ok' message response" do
34
+ response = @action.perform
35
+ response.should be_instance_of(Moon::Response::JSON::Message)
36
+ response.status.should == 200
37
+ end
38
+
39
+ it "should do nothing if the model isn't initialized" do
40
+ @context.models[:model] = nil
41
+ GOM::Storage.should_not_receive(:store)
42
+ @action.perform
43
+ end
44
+
45
+ it "should return 'failed' message response if model isn't initialized" do
46
+ @context.models[:model] = nil
47
+ response = @action.perform
48
+ response.should be_instance_of(Moon::Response::JSON::Message)
49
+ response.status.should == 401
50
+ end
51
+
52
+ end
53
+
54
+ describe "model" do
55
+
56
+ it "should return the model" do
57
+ model = @action.model
58
+ model.should == @model
59
+ end
60
+
61
+ end
62
+
63
+ describe "self.model_symbol" do
64
+
65
+ it "should return the model symbol" do
66
+ model_symbol = described_class.model_symbol
67
+ model_symbol.should == :model
68
+ end
69
+
70
+ end
71
+
72
+ describe "self.[]" do
73
+
74
+ it "should return a class" do
75
+ result = described_class[:model]
76
+ result.should be_instance_of(Class)
77
+ end
78
+
79
+ it "should return a different class" do
80
+ result = described_class[:model]
81
+ result.should_not == described_class
82
+ end
83
+
84
+ it "should return a class connected to the given model symbol" do
85
+ result = described_class[:model]
86
+ result.model_symbol.should == :model
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::Models::Finder do
4
+
5
+ before :each do
6
+ @context = Moon::Context.new({ }, :site_id => "test:4", :test => "test value")
7
+
8
+ @model = Object.new
9
+ GOM::Storage.stub :fetch => @model
10
+ end
11
+
12
+ describe "self.perform" do
13
+
14
+ it "should fetch the model using the right id" do
15
+ GOM::Storage.should_receive(:fetch).with("test:4").and_return(@model)
16
+ described_class.perform @context
17
+ end
18
+
19
+ it "should set the found models in the context" do
20
+ described_class.perform @context
21
+ @context.models[:site].should == @model
22
+ end
23
+
24
+ it "should return nil" do
25
+ response = described_class.perform @context
26
+ response.should be_nil
27
+ end
28
+
29
+ it "should not fetch non-id values" do
30
+ GOM::Storage.should_not_receive(:fetch).with("test value")
31
+ described_class.perform @context
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::Models::Initializer do
4
+
5
+ before :each do
6
+ @context = Moon::Context.new({ }, :site => { :name => "Test Site", :address => "Test Address" })
7
+
8
+ @model = mock Object, :name= => nil, :address= => nil
9
+ @model_class = mock Class, :new => @model
10
+ Object.stub :const_get => @model_class
11
+ end
12
+
13
+ describe "perform" do
14
+
15
+ it "should get the right model class" do
16
+ Object.should_receive(:const_get).with("Site").and_return(@model_class)
17
+ described_class.perform @context
18
+ end
19
+
20
+ it "should initialize the model" do
21
+ @model_class.should_receive(:new).and_return(@model)
22
+ described_class.perform @context
23
+ end
24
+
25
+ it "should set the model's attributes" do
26
+ @model.should_receive(:name=).with("Test Site")
27
+ @model.should_receive(:address=).with("Test Address")
28
+ described_class.perform @context
29
+ end
30
+
31
+ it "should set the model to the context" do
32
+ described_class.perform @context
33
+ @context.models[:site].should == @model
34
+ end
35
+
36
+ it "should return nil" do
37
+ response = described_class.perform @context
38
+ response.should be_nil
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::Models::Updater do
4
+
5
+ before :each do
6
+ @model = Object.new
7
+ @model.class.send :attr_accessor, :options
8
+ @model.class.send :attr_accessor, :pick
9
+ @model.pick = 0
10
+
11
+ @context = Moon::Context.new({ }, :decision => { :pick => 1 })
12
+ @context.models[:decision] = @model
13
+ end
14
+
15
+ describe "self.perform" do
16
+
17
+ it "should change the model's attributes" do
18
+ lambda do
19
+ described_class.perform @context
20
+ end.should change(@model, :pick).from(0).to(1)
21
+ end
22
+
23
+ it "should not change the model's attributes that are not specified" do
24
+ lambda do
25
+ described_class.perform @context
26
+ end.should_not change(@model, :options)
27
+ end
28
+
29
+ it "should return nil" do
30
+ response = described_class.perform @context
31
+ response.should be_nil
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::RebuildArrays do
4
+
5
+ before :each do
6
+ @context = Moon::Context.new({ }, :test => { "0" => "test one", "1" => "test two" })
7
+
8
+ @action = described_class.new @context
9
+ end
10
+
11
+ describe "#perform" do
12
+
13
+ it "should build a hash for each option" do
14
+ @action.perform
15
+ parameters = @context.parameters
16
+ parameters.should == {
17
+ :test => [ "test one", "test two" ]
18
+ }
19
+ end
20
+
21
+ it "should return nil" do
22
+ response = @action.perform
23
+ response.should be_nil
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::ReferenceObject do
4
+
5
+ before :each do
6
+ @model_one = mock Object
7
+ @model_two = mock Object, :test => nil, :test= => nil
8
+
9
+ @context = Moon::Context.new :model_one => @model_one
10
+ @context.models[:model_two] = @model_two
11
+
12
+ @action = described_class.new :from => [ :session, :model_one ], :to => [ :model_two, :test ]
13
+ end
14
+
15
+ describe "perform" do
16
+
17
+ it "should create a reference from the given store attribute to the given model attribute" do
18
+ @model_two.should_receive(:test=).and_return do |proxy|
19
+ proxy.should be_instance_of(GOM::Object::Proxy)
20
+ proxy.object.should == @model_one
21
+ end
22
+ @action.perform @context
23
+ end
24
+
25
+ it "should return nil" do
26
+ response = @action.perform @context
27
+ response.should be_nil
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::ValidModelsRequired do
4
+
5
+ before :each do
6
+ @model_class = mock Class
7
+ @model = mock Object, :class => @model_class
8
+
9
+ @context = Moon::Context.new
10
+ @context.models[:model] = @model
11
+
12
+ @validator = mock Moon::Validator, :ok? => true, :messages => { :email => [ "Has a wrong format." ] }
13
+ @validator_class = mock Moon::Validator, :new => @validator
14
+ Moon::Validator.stub(:[]).and_return(@validator_class)
15
+
16
+ @response = mock Moon::Response::JSON::ValidationErrors
17
+ Moon::Response::JSON::ValidationErrors.stub :new => @response
18
+ end
19
+
20
+ describe "perform" do
21
+
22
+ it "should get the validator class for the right model class" do
23
+ Moon::Validator.should_receive(:[]).with(@model_class).and_return(@validator_class)
24
+ described_class.perform @context
25
+ end
26
+
27
+ it "should initialize the validator with the right model" do
28
+ @validator_class.should_receive(:new).with(@model).and_return(@validator)
29
+ described_class.perform @context
30
+ end
31
+
32
+ it "should run the validator" do
33
+ @validator.should_receive(:ok?).and_return(true)
34
+ described_class.perform @context
35
+ end
36
+
37
+ it "should return nil" do
38
+ response = described_class.perform @context
39
+ response.should be_nil
40
+ end
41
+
42
+ context "on failing validation" do
43
+
44
+ before :each do
45
+ @validator.stub :ok? => false
46
+ end
47
+
48
+ it "should initialize #{Moon::Response::JSON::ValidationErrors}" do
49
+ Moon::Response::JSON::ValidationErrors.should_receive(:new).with(
50
+ :model => { :email => [ "Has a wrong format." ] }
51
+ ).and_return(@response)
52
+ described_class.perform @context
53
+ end
54
+
55
+ it "should return the validation errors response" do
56
+ response = described_class.perform @context
57
+ response.should == @response
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,73 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Application::Rack do
4
+
5
+ before :each do
6
+ Moon::Application.stub :storage_name => :storage_name
7
+
8
+ @context = mock Moon::Context, :storage_name= => nil
9
+ Moon::Context.stub :new => @context
10
+
11
+ @response = mock Moon::Response::Base, :status => 200, :headers => { }, :body => "OK"
12
+
13
+ @action_one = mock Object, :perform => nil
14
+ @action_two = mock Class, :perform => @response
15
+
16
+ @rack = described_class.new
17
+ @rack.routes = [
18
+ {
19
+ :http_method => :get,
20
+ :path => "/test/:id",
21
+ :actions => [ @action_one, @action_two ]
22
+ }
23
+ ]
24
+ @browser = Rack::Test::Session.new Rack::MockSession.new(@rack)
25
+ end
26
+
27
+ it "should have the environment set to :test" do
28
+ @rack.environment.should == :test
29
+ end
30
+
31
+ it "should initialize the context with the right parameters" do
32
+ Moon::Context.should_receive(:new).with({ }, { "id" => "5" }).and_return(@context)
33
+ @browser.get "/test/5"
34
+ end
35
+
36
+ it "should set the storage name in the context" do
37
+ @context.should_receive(:storage_name=).with(:storage_name)
38
+ @browser.get "/test/5"
39
+ end
40
+
41
+ it "should perform all actions with the context" do
42
+ @action_one.should_receive(:perform).with(@context).ordered
43
+ @action_two.should_receive(:perform).with(@context).ordered
44
+ @browser.get "/test/5"
45
+ end
46
+
47
+ it "should not perform action two if action one has a response" do
48
+ @action_one.stub :perform => @response
49
+ @action_two.should_not_receive(:perform)
50
+ @browser.get "/test/5"
51
+ end
52
+
53
+ it "should response the action's response" do
54
+ response = @browser.get "/test/5"
55
+ response.status.should == 200
56
+ response.body.should == "OK"
57
+ end
58
+
59
+ it "should raise an #{Moon::Application::InvalidActionError} if the action doesn't respond to :perform" do
60
+ @action_one.stub(:respond_to?).with(:perform).and_return(false)
61
+ lambda do
62
+ @browser.get "/test/5"
63
+ end.should raise_error(Moon::Application::InvalidActionError)
64
+ end
65
+
66
+ it "should raise an #{Moon::Application::InvalidResponseError} if the response doesn't respond to :status, :headers and :body" do
67
+ @response.stub(:respond_to?).with(:status).and_return(false)
68
+ lambda do
69
+ @browser.get "/test/5"
70
+ end.should raise_error(Moon::Application::InvalidResponseError)
71
+ end
72
+
73
+ end