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,26 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Application::Routes do
4
+
5
+ before :each do
6
+ @rack = mock Moon::Application::Rack
7
+ Moon::Application::Rack.stub :new => @rack
8
+
9
+ @routes = described_class.new
10
+ end
11
+
12
+ describe "#define" do
13
+
14
+ it "should pass the routes to rack" do
15
+ @rack.should_receive(:routes=).with([ { :http_method => :get, :path => "/test" } ])
16
+ @routes.define do
17
+ route {
18
+ http_method :get
19
+ path "/test"
20
+ }
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Context do
4
+
5
+ before :each do
6
+ @context = described_class.new
7
+ end
8
+
9
+ [ :session, :parameters, :models, :collections ].each do |key|
10
+
11
+ describe "#{key}=" do
12
+
13
+ it "should set the #{key} hash" do
14
+ @context.send :"#{key}=", { }
15
+ @context.send(key).should == { }
16
+ end
17
+
18
+ it "should raise an #{ArgumentError} if no hash is given" do
19
+ lambda do
20
+ @context.send :"#{key}=", :invalid
21
+ end.should raise_error(ArgumentError)
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Formatter::Base do
4
+
5
+ before :each do
6
+ @model = mock Object
7
+
8
+ @formatter = described_class.new @model
9
+ end
10
+
11
+ describe "model" do
12
+
13
+ it "should return the model" do
14
+ model = @formatter.model
15
+ model.should == @model
16
+ end
17
+
18
+ end
19
+
20
+ describe "hash" do
21
+
22
+ it "should raise an #{NotImplementedError}" do
23
+ lambda do
24
+ @formatter.hash
25
+ end.should raise_error(NotImplementedError)
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Formatter::Generic do
4
+
5
+ before :each do
6
+ @model = Object.new
7
+ @model.instance_variable_set :@options, [ ]
8
+ @model.instance_variable_set :@pick, 0
9
+
10
+ @id = mock GOM::Object::Id
11
+ GOM::Object.stub :id => @id
12
+
13
+ @formatter = described_class.new @model
14
+ end
15
+
16
+ describe "hash" do
17
+
18
+ it "should fetch the object's id" do
19
+ GOM::Object.should_receive(:id).with(@model).and_return(@id)
20
+ @formatter.hash
21
+ end
22
+
23
+ it "should return a hash with all instance variables" do
24
+ hash = @formatter.hash
25
+ hash.should == {
26
+ :id => @id,
27
+ :options => [ ],
28
+ :pick => 0
29
+ }
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Formatter do
4
+
5
+ describe "self.hash_for" do
6
+
7
+ before :each do
8
+ @formatter = mock described_class::Base, :hash => :hash
9
+ @formatter_class = mock Class, :new => @formatter
10
+
11
+ @generic_formatter = mock described_class::Generic, :hash => :generic_hash
12
+ described_class::Generic.stub :new => @generic_formatter
13
+
14
+ @model = mock Object
15
+
16
+ described_class.configuration = { @model.class => @formatter_class }
17
+ end
18
+
19
+ it "should initialize the right formatter" do
20
+ @formatter_class.should_receive(:new).with(@model)
21
+ described_class.hash_for @model
22
+ end
23
+
24
+ it "should initialize the generic formatter if no formatter is configured for the model class" do
25
+ described_class::Generic.should_receive(:new).and_return(@generic_formatter)
26
+ described_class.hash_for Object.new
27
+ end
28
+
29
+ it "should return the formatters hash" do
30
+ hash = described_class.hash_for @model
31
+ hash.should == :hash
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Response::Base do
4
+
5
+ before :each do
6
+ @response = described_class.new
7
+ end
8
+
9
+ describe "status" do
10
+
11
+ it "should return 200" do
12
+ @response.status.should == 200
13
+ end
14
+
15
+ end
16
+
17
+ describe "headers" do
18
+
19
+ it "should return an empty hash" do
20
+ @response.headers.should == { }
21
+ end
22
+
23
+ end
24
+
25
+ describe "body" do
26
+
27
+ it "should return nil" do
28
+ @response.body.should be_nil
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Response::JSON::Collection do
4
+
5
+ before :each do
6
+ @object = mock Object
7
+
8
+ Moon::Formatter.stub :hash_for => { :test => "test value" }
9
+ end
10
+
11
+ describe "initialize" do
12
+
13
+ it "should set the status to 200" do
14
+ @response = described_class.new [ @object ]
15
+ @response.status.should == 200
16
+ end
17
+
18
+ it "should use the model formatter" do
19
+ Moon::Formatter.should_receive(:hash_for).with(@object).and_return({ :test => "test value" })
20
+ described_class.new [ @object ]
21
+ end
22
+
23
+ it "should return the formatters output" do
24
+ @response = described_class.new [ @object ]
25
+ @response.body.should == "[{\"test\":\"test value\"}]"
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Response::JSON::Message do
4
+
5
+ before :each do
6
+ @response = described_class.new 201, "OK"
7
+ end
8
+
9
+ describe "status" do
10
+
11
+ it "should return the given status" do
12
+ @response.status.should == 201
13
+ end
14
+
15
+ end
16
+
17
+ describe "body" do
18
+
19
+ it "should return the given message" do
20
+ @response.body.should == "{\"message\":\"OK\"}"
21
+ end
22
+
23
+ it "should return the given message and id if given" do
24
+ @response.id = "id"
25
+ @response.body.should == "{\"message\":\"OK\",\"id\":\"id\"}"
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Response::JSON::Model do
4
+
5
+ before :each do
6
+ @key = :key
7
+ @object = mock Object
8
+
9
+ Moon::Formatter.stub :hash_for => { :test => "test value" }
10
+ end
11
+
12
+ describe "initialize" do
13
+
14
+ it "should set the status to 200" do
15
+ @response = described_class.new @key, @object
16
+ @response.status.should == 200
17
+ end
18
+
19
+ it "should use the model formatter" do
20
+ Moon::Formatter.should_receive(:hash_for).with(@object).and_return({ :test => "test value" })
21
+ described_class.new @key, @object
22
+ end
23
+
24
+ it "should return the formatters output" do
25
+ @response = described_class.new @key, @object
26
+ @response.body.should == "{\"key\":{\"test\":\"test value\"}}"
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Response::JSON::ValidationErrors do
4
+
5
+ before :each do
6
+ @response = described_class.new :user => { :email => [ "Has a wrong format." ] }
7
+ end
8
+
9
+ describe "body" do
10
+
11
+ it "should return the right status code" do
12
+ @response.status.should == 200
13
+ end
14
+
15
+ it "should return the given hash encoded in json" do
16
+ @response.body.should == "{\"validation_errors\":{\"user\":{\"email\":[\"Has a wrong format.\"]}}}"
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Response::JSON do
4
+
5
+ before :each do
6
+ @response = described_class.new 201, { :message => "OK" }
7
+ end
8
+
9
+ describe "status" do
10
+
11
+ it "should return 201" do
12
+ @response.status.should == 201
13
+ end
14
+
15
+ end
16
+
17
+ describe "headers" do
18
+
19
+ it "should return a hash with the right content type" do
20
+ @response.headers.should == { "Content-Type" => "application/json" }
21
+ end
22
+
23
+ end
24
+
25
+ describe "body" do
26
+
27
+ it "should return the given hash encoded in json" do
28
+ @response.body.should == "{\"message\":\"OK\"}"
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Utility::String do
4
+
5
+ describe "camelize" do
6
+
7
+ before :each do
8
+ @underscore_string = "test/test_one"
9
+ end
10
+
11
+ it "should return the camelize version of the string" do
12
+ described_class.camelize(@underscore_string).should == "Test::TestOne"
13
+ end
14
+
15
+ end
16
+
17
+ describe "underscore" do
18
+
19
+ before :each do
20
+ @camel_cased_string = "Test::TestOne"
21
+ end
22
+
23
+ it "should return the underscore version of the string" do
24
+ described_class.underscore(@camel_cased_string).should == "test/test_one"
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Utility::Template do
4
+
5
+ before :each do
6
+ @klass = Class.new
7
+ @klass.instance_eval do
8
+ extend Moon::Utility::Template
9
+
10
+ template :test_one, :test_two
11
+ end
12
+ end
13
+
14
+ describe "#self.template" do
15
+
16
+ it "should define a :[] method for the class" do
17
+ @klass.should respond_to(:[])
18
+ end
19
+
20
+ it "should define an accessor for each attribute" do
21
+ @klass.test_one = Object
22
+ @klass.test_one.should == Object
23
+ @klass.test_two = :test
24
+ @klass.test_two.should == :test
25
+ end
26
+
27
+ end
28
+
29
+ describe "#self.[]" do
30
+
31
+ it "should return a class" do
32
+ klass = @klass[Object, :test]
33
+ klass.should be_instance_of(Class)
34
+ end
35
+
36
+ it "should return a class that is different from the original" do
37
+ klass = @klass[Object, :test]
38
+ klass.should_not == @klass
39
+ end
40
+
41
+ it "should return a class that is assigned to the right checks" do
42
+ klass = @klass[Object, :test]
43
+ klass.test_one.should == Object
44
+ klass.test_two.should == :test
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Validator::Format do
4
+
5
+ before :each do
6
+ described_class.format = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
7
+ @validator = described_class.new "test@test.com"
8
+ end
9
+
10
+ describe "#ok?" do
11
+
12
+ it "should return true" do
13
+ ok = @validator.ok?
14
+ ok.should be_true
15
+ end
16
+
17
+ it "should return false if value has an invalid format" do
18
+ @validator.value = "invalid"
19
+ ok = @validator.ok?
20
+ ok.should be_false
21
+ end
22
+
23
+ end
24
+
25
+ describe "#message" do
26
+
27
+ it "should return nil" do
28
+ message = @validator.message
29
+ message.should be_nil
30
+ end
31
+
32
+ it "should return the error message" do
33
+ @validator.value = "invalid"
34
+ message = @validator.message
35
+ message.should == "Has a wrong format."
36
+ end
37
+
38
+ end
39
+
40
+ end