moon 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/README.rdoc +2 -0
  2. data/Rakefile +10 -3
  3. data/lib/moon.rb +0 -2
  4. data/lib/moon/action.rb +4 -3
  5. data/lib/moon/action/inject_timestamps.rb +44 -0
  6. data/lib/moon/action/model.rb +1 -2
  7. data/lib/moon/action/model/destroy.rb +4 -26
  8. data/lib/moon/action/model/index.rb +10 -17
  9. data/lib/moon/action/model/show.rb +9 -23
  10. data/lib/moon/action/model/store.rb +20 -0
  11. data/lib/moon/action/models/finder.rb +14 -14
  12. data/lib/moon/action/models/initializer.rb +6 -5
  13. data/lib/moon/action/models/updater.rb +1 -4
  14. data/lib/moon/action/rebuild_arrays.rb +1 -4
  15. data/lib/moon/action/{reference_object.rb → reference.rb} +1 -1
  16. data/lib/moon/action/respond_blank.rb +9 -0
  17. data/lib/moon/action/transfer.rb +30 -0
  18. data/lib/moon/action/valid_models_required.rb +13 -6
  19. data/lib/moon/application.rb +12 -7
  20. data/lib/moon/application/configuration.rb +55 -0
  21. data/lib/moon/application/rack.rb +17 -7
  22. data/lib/moon/context.rb +1 -1
  23. data/lib/moon/formatter.rb +4 -13
  24. data/lib/moon/formatter/generic.rb +17 -4
  25. data/lib/moon/response/json.rb +1 -0
  26. data/lib/moon/response/json/blank.rb +9 -0
  27. data/lib/moon/response/json/collection.rb +2 -2
  28. data/lib/moon/response/json/model.rb +3 -3
  29. data/lib/moon/validator.rb +17 -45
  30. data/lib/moon/validator/format.rb +4 -23
  31. data/lib/moon/validator/length.rb +8 -33
  32. data/lib/moon/validator/presence.rb +4 -17
  33. data/spec/lib/moon/action/inject_timestamps_spec.rb +46 -0
  34. data/spec/lib/moon/action/model/destroy_spec.rb +6 -44
  35. data/spec/lib/moon/action/model/index_spec.rb +8 -25
  36. data/spec/lib/moon/action/model/show_spec.rb +33 -25
  37. data/spec/lib/moon/action/model/store_spec.rb +77 -0
  38. data/spec/lib/moon/action/models/finder_spec.rb +25 -11
  39. data/spec/lib/moon/action/models/initializer_spec.rb +7 -5
  40. data/spec/lib/moon/action/models/updater_spec.rb +5 -3
  41. data/spec/lib/moon/action/rebuild_arrays_spec.rb +3 -3
  42. data/spec/lib/moon/action/{reference_object_spec.rb → reference_spec.rb} +1 -1
  43. data/spec/lib/moon/action/respond_blank_spec.rb +24 -0
  44. data/spec/lib/moon/action/transfer_spec.rb +30 -0
  45. data/spec/lib/moon/action/valid_models_required_spec.rb +19 -17
  46. data/spec/lib/moon/application/configuration_spec.rb +75 -0
  47. data/spec/lib/moon/application/rack_spec.rb +6 -5
  48. data/spec/lib/moon/formatter/generic_spec.rb +13 -3
  49. data/spec/lib/moon/formatter_spec.rb +4 -10
  50. data/spec/lib/moon/response/json/blank_spec.rb +19 -0
  51. data/spec/lib/moon/response/json/collection_spec.rb +5 -4
  52. data/spec/lib/moon/response/json/model_spec.rb +5 -4
  53. data/spec/lib/moon/validator/format_spec.rb +8 -25
  54. data/spec/lib/moon/validator/length_spec.rb +10 -36
  55. data/spec/lib/moon/validator/presence_spec.rb +11 -28
  56. data/spec/lib/moon/validator_spec.rb +10 -59
  57. metadata +34 -28
  58. data/lib/moon/action/base.rb +0 -16
  59. data/lib/moon/action/model/create.rb +0 -45
  60. data/lib/moon/action/model/update.rb +0 -42
  61. data/lib/moon/application/routes.rb +0 -16
  62. data/lib/moon/formatter/base.rb +0 -15
  63. data/spec/lib/moon/action/base_spec.rb +0 -29
  64. data/spec/lib/moon/action/model/create_spec.rb +0 -111
  65. data/spec/lib/moon/action/model/update_spec.rb +0 -91
  66. data/spec/lib/moon/application/routes_spec.rb +0 -26
  67. data/spec/lib/moon/formatter/base_spec.rb +0 -30
@@ -10,24 +10,26 @@ describe Moon::Action::Models::Updater do
10
10
 
11
11
  @context = Moon::Context.new({ }, :decision => { :pick => 1 })
12
12
  @context.models[:decision] = @model
13
+
14
+ @action = described_class.new
13
15
  end
14
16
 
15
17
  describe "self.perform" do
16
18
 
17
19
  it "should change the model's attributes" do
18
20
  lambda do
19
- described_class.perform @context
21
+ @action.perform @context
20
22
  end.should change(@model, :pick).from(0).to(1)
21
23
  end
22
24
 
23
25
  it "should not change the model's attributes that are not specified" do
24
26
  lambda do
25
- described_class.perform @context
27
+ @action.perform @context
26
28
  end.should_not change(@model, :options)
27
29
  end
28
30
 
29
31
  it "should return nil" do
30
- response = described_class.perform @context
32
+ response = @action.perform @context
31
33
  response.should be_nil
32
34
  end
33
35
 
@@ -5,13 +5,13 @@ describe Moon::Action::RebuildArrays do
5
5
  before :each do
6
6
  @context = Moon::Context.new({ }, :test => { "0" => "test one", "1" => "test two" })
7
7
 
8
- @action = described_class.new @context
8
+ @action = described_class.new
9
9
  end
10
10
 
11
11
  describe "#perform" do
12
12
 
13
13
  it "should build a hash for each option" do
14
- @action.perform
14
+ @action.perform @context
15
15
  parameters = @context.parameters
16
16
  parameters.should == {
17
17
  :test => [ "test one", "test two" ]
@@ -19,7 +19,7 @@ describe Moon::Action::RebuildArrays do
19
19
  end
20
20
 
21
21
  it "should return nil" do
22
- response = @action.perform
22
+ response = @action.perform @context
23
23
  response.should be_nil
24
24
  end
25
25
 
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
2
 
3
- describe Moon::Action::ReferenceObject do
3
+ describe Moon::Action::Reference do
4
4
 
5
5
  before :each do
6
6
  @model_one = mock Object
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::RespondBlank do
4
+
5
+ before :each do
6
+ @context = Moon::Context.new
7
+ @action = described_class.new
8
+ end
9
+
10
+ describe "#perform" do
11
+
12
+ before :each do
13
+ @response = mock Moon::Response::JSON::Blank
14
+ Moon::Response::JSON::Blank.stub :new => @response
15
+ end
16
+
17
+ it "should initialize a #{Moon::Response::JSON::Blank}" do
18
+ Moon::Response::JSON::Blank.should_receive(:new).and_return(@response)
19
+ @action.perform @context
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::Transfer 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
10
+ @context.session[:model_one] = @model_one
11
+ @context.models[:model_two] = @model_two
12
+
13
+ @action = described_class.new :from => [ :session, :model_one ], :to => [ :model_two, :test ]
14
+ end
15
+
16
+ describe "perform" do
17
+
18
+ it "should transfer a value from the given store attribute to the given model attribute" do
19
+ @model_two.should_receive(:test=).with(@model_one)
20
+ @action.perform @context
21
+ end
22
+
23
+ it "should return nil" do
24
+ response = @action.perform @context
25
+ response.should be_nil
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -6,54 +6,56 @@ describe Moon::Action::ValidModelsRequired do
6
6
  @model_class = mock Class
7
7
  @model = mock Object, :class => @model_class
8
8
 
9
+ @checks = { }
10
+ @validators = { @model_class => @checks }
11
+ @configuration = mock Moon::Application::Configuration, :validators => @validators
12
+ @application = mock Moon::Application, :configuration => @configuration
13
+
9
14
  @context = Moon::Context.new
15
+ @context.application = @application
10
16
  @context.models[:model] = @model
11
17
 
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)
18
+ @validator = mock Moon::Validator, :messages => { }
19
+ Moon::Validator.stub :new => @validator
15
20
 
16
21
  @response = mock Moon::Response::JSON::ValidationErrors
17
22
  Moon::Response::JSON::ValidationErrors.stub :new => @response
23
+
24
+ @action = described_class.new
18
25
  end
19
26
 
20
27
  describe "perform" do
21
28
 
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
29
+ it "should initialize the validator with the right checks" do
30
+ Moon::Validator.should_receive(:new).with(@context, @checks).and_return(@validator)
31
+ @action.perform @context
30
32
  end
31
33
 
32
34
  it "should run the validator" do
33
- @validator.should_receive(:ok?).and_return(true)
34
- described_class.perform @context
35
+ @validator.should_receive(:messages).with(@model).and_return({ })
36
+ @action.perform @context
35
37
  end
36
38
 
37
39
  it "should return nil" do
38
- response = described_class.perform @context
40
+ response = @action.perform @context
39
41
  response.should be_nil
40
42
  end
41
43
 
42
44
  context "on failing validation" do
43
45
 
44
46
  before :each do
45
- @validator.stub :ok? => false
47
+ @validator.stub :messages => { :email => [ "Has a wrong format." ] }
46
48
  end
47
49
 
48
50
  it "should initialize #{Moon::Response::JSON::ValidationErrors}" do
49
51
  Moon::Response::JSON::ValidationErrors.should_receive(:new).with(
50
52
  :model => { :email => [ "Has a wrong format." ] }
51
53
  ).and_return(@response)
52
- described_class.perform @context
54
+ @action.perform @context
53
55
  end
54
56
 
55
57
  it "should return the validation errors response" do
56
- response = described_class.perform @context
58
+ response = @action.perform @context
57
59
  response.should == @response
58
60
  end
59
61
 
@@ -0,0 +1,75 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Application::Configuration do
4
+
5
+ before :each do
6
+ @action = action = Object.new
7
+ @attribute_validator = attribute_validator = Object.new
8
+ @formatter = formatter = Object.new
9
+ @configuration = described_class.new {
10
+ session {
11
+ secret "secret"
12
+ }
13
+ route {
14
+ http_method :get
15
+ path "/test"
16
+ actions action
17
+ }
18
+ validator {
19
+ model_class Object
20
+ attributes {
21
+ name attribute_validator
22
+ }
23
+ }
24
+ formatter {
25
+ model_class Object
26
+ object formatter
27
+ }
28
+ }
29
+ end
30
+
31
+ describe "#session" do
32
+
33
+ it "should return the session configuration" do
34
+ session = @configuration.session
35
+ session.should == {
36
+ :secret => "secret"
37
+ }
38
+ end
39
+
40
+ end
41
+
42
+ describe "#routes" do
43
+
44
+ it "should return an array of the defined routes" do
45
+ routes = @configuration.routes
46
+ routes.should == [
47
+ { :http_method => :get, :path => "/test", :actions => @action }
48
+ ]
49
+ end
50
+
51
+ end
52
+
53
+ describe "#validators" do
54
+
55
+ it "should return a hash with the defined validators" do
56
+ validators = @configuration.validators
57
+ validators.should == {
58
+ Object => { :name => @attribute_validator }
59
+ }
60
+ end
61
+
62
+ end
63
+
64
+ describe "#formatters" do
65
+
66
+ it "should return a hash with the defined formatters" do
67
+ formatters = @configuration.formatters
68
+ formatters.should == {
69
+ Object => @formatter
70
+ }
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -3,9 +3,9 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "sp
3
3
  describe Moon::Application::Rack do
4
4
 
5
5
  before :each do
6
- Moon::Application.stub :storage_name => :storage_name
6
+ @application = mock Moon::Application
7
7
 
8
- @context = mock Moon::Context, :storage_name= => nil
8
+ @context = mock Moon::Context, :application= => nil
9
9
  Moon::Context.stub :new => @context
10
10
 
11
11
  @response = mock Moon::Response::Base, :status => 200, :headers => { }, :body => "OK"
@@ -13,7 +13,8 @@ describe Moon::Application::Rack do
13
13
  @action_one = mock Object, :perform => nil
14
14
  @action_two = mock Class, :perform => @response
15
15
 
16
- @rack = described_class.new
16
+ @rack = described_class.new @application
17
+ @rack.session = { :secret => "secret" }
17
18
  @rack.routes = [
18
19
  {
19
20
  :http_method => :get,
@@ -33,8 +34,8 @@ describe Moon::Application::Rack do
33
34
  @browser.get "/test/5"
34
35
  end
35
36
 
36
- it "should set the storage name in the context" do
37
- @context.should_receive(:storage_name=).with(:storage_name)
37
+ it "should set the application in the context" do
38
+ @context.should_receive(:application=).with(@application)
38
39
  @browser.get "/test/5"
39
40
  end
40
41
 
@@ -3,27 +3,37 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "sp
3
3
  describe Moon::Formatter::Generic do
4
4
 
5
5
  before :each do
6
+ @model_proxy = mock Object, :id => "user_id", :is_a? => true
7
+
6
8
  @model = Object.new
9
+ @model.instance_variable_set :@user, @model_proxy
7
10
  @model.instance_variable_set :@options, [ ]
8
11
  @model.instance_variable_set :@pick, 0
9
12
 
10
13
  @id = mock GOM::Object::Id
11
14
  GOM::Object.stub :id => @id
12
15
 
13
- @formatter = described_class.new @model
16
+ @formatter = described_class.new
14
17
  end
15
18
 
16
19
  describe "hash" do
17
20
 
18
21
  it "should fetch the object's id" do
19
22
  GOM::Object.should_receive(:id).with(@model).and_return(@id)
20
- @formatter.hash
23
+ @formatter.hash @model
24
+ end
25
+
26
+ it "should not inject the id if nil" do
27
+ GOM::Object.stub :id => nil
28
+ hash = @formatter.hash @model
29
+ hash.keys.should_not include(:id)
21
30
  end
22
31
 
23
32
  it "should return a hash with all instance variables" do
24
- hash = @formatter.hash
33
+ hash = @formatter.hash @model
25
34
  hash.should == {
26
35
  :id => @id,
36
+ :user_id => "user_id",
27
37
  :options => [ ],
28
38
  :pick => 0
29
39
  }
@@ -5,29 +5,23 @@ describe Moon::Formatter do
5
5
  describe "self.hash_for" do
6
6
 
7
7
  before :each do
8
- @formatter = mock described_class::Base, :hash => :hash
9
- @formatter_class = mock Class, :new => @formatter
8
+ @formatter = mock Object, :hash => :hash
10
9
 
11
10
  @generic_formatter = mock described_class::Generic, :hash => :generic_hash
12
11
  described_class::Generic.stub :new => @generic_formatter
13
12
 
14
13
  @model = mock Object
15
14
 
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
15
+ @formatters = { @model.class => @formatter }
22
16
  end
23
17
 
24
18
  it "should initialize the generic formatter if no formatter is configured for the model class" do
25
19
  described_class::Generic.should_receive(:new).and_return(@generic_formatter)
26
- described_class.hash_for Object.new
20
+ described_class.hash_for @formatters, Object.new
27
21
  end
28
22
 
29
23
  it "should return the formatters hash" do
30
- hash = described_class.hash_for @model
24
+ hash = described_class.hash_for @formatters, @model
31
25
  hash.should == :hash
32
26
  end
33
27
 
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Response::JSON::Blank do
4
+
5
+ describe "initialize" do
6
+
7
+ it "should set the status to 200" do
8
+ @response = described_class.new
9
+ @response.status.should == 200
10
+ end
11
+
12
+ it "should set the hash to { }" do
13
+ @response = described_class.new
14
+ @response.hash.should == { }
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -4,6 +4,7 @@ describe Moon::Response::JSON::Collection do
4
4
 
5
5
  before :each do
6
6
  @object = mock Object
7
+ @collection = [ @object ]
7
8
 
8
9
  Moon::Formatter.stub :hash_for => { :test => "test value" }
9
10
  end
@@ -11,17 +12,17 @@ describe Moon::Response::JSON::Collection do
11
12
  describe "initialize" do
12
13
 
13
14
  it "should set the status to 200" do
14
- @response = described_class.new [ @object ]
15
+ @response = described_class.new @collection, :formatters
15
16
  @response.status.should == 200
16
17
  end
17
18
 
18
19
  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 ]
20
+ Moon::Formatter.should_receive(:hash_for).with(:formatters, @object).and_return({ :test => "test value" })
21
+ described_class.new @collection, :formatters
21
22
  end
22
23
 
23
24
  it "should return the formatters output" do
24
- @response = described_class.new [ @object ]
25
+ @response = described_class.new @collection, :formatters
25
26
  @response.body.should == "[{\"test\":\"test value\"}]"
26
27
  end
27
28
 
@@ -5,6 +5,7 @@ describe Moon::Response::JSON::Model do
5
5
  before :each do
6
6
  @key = :key
7
7
  @object = mock Object
8
+ @formatters = { }
8
9
 
9
10
  Moon::Formatter.stub :hash_for => { :test => "test value" }
10
11
  end
@@ -12,17 +13,17 @@ describe Moon::Response::JSON::Model do
12
13
  describe "initialize" do
13
14
 
14
15
  it "should set the status to 200" do
15
- @response = described_class.new @key, @object
16
+ @response = described_class.new @key, @object, @formatters
16
17
  @response.status.should == 200
17
18
  end
18
19
 
19
20
  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
21
+ Moon::Formatter.should_receive(:hash_for).with(@formatters, @object).and_return({ :test => "test value" })
22
+ described_class.new @key, @object, @formatters
22
23
  end
23
24
 
24
25
  it "should return the formatters output" do
25
- @response = described_class.new @key, @object
26
+ @response = described_class.new @key, @object, @formatters
26
27
  @response.body.should == "{\"key\":{\"test\":\"test value\"}}"
27
28
  end
28
29