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
@@ -2,28 +2,15 @@
2
2
  # Value presence validator.
3
3
  class Moon::Validator::Presence
4
4
 
5
- attr_accessor :value
6
-
7
- def initialize(value)
5
+ def messages(value, context)
8
6
  @value = value
9
- @ok, @message = true, nil
10
- end
11
-
12
- def ok?
13
- check
14
- @ok
15
- end
16
-
17
- def message
18
- check
19
- @message
7
+ blank? ? [ "Must be present." ] : [ ]
20
8
  end
21
9
 
22
10
  private
23
11
 
24
- def check
25
- @ok = @value && @value != ""
26
- @message = "Must be present." unless @ok
12
+ def blank?
13
+ !@value || @value == ""
27
14
  end
28
15
 
29
16
  end
@@ -0,0 +1,46 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::InjectTimestamps do
4
+
5
+ before :each do
6
+ @model = mock Object, :created_at= => nil, :updated_at= => nil
7
+
8
+ @context = Moon::Context.new
9
+ @context.models[:test] = @model
10
+
11
+ @action = described_class.new :test
12
+ end
13
+
14
+ describe "#perform" do
15
+
16
+ before :each do
17
+ @now = Time.now
18
+ Time.stub :now => @now
19
+
20
+ GOM::Object.stub :id => nil
21
+ end
22
+
23
+ it "should inject the creation date time" do
24
+ @model.should_receive(:created_at=).with(@now)
25
+ @action.perform @context
26
+ end
27
+
28
+ it "should inject the update date time" do
29
+ @model.should_receive(:updated_at=).with(@now)
30
+ @action.perform @context
31
+ end
32
+
33
+ it "should not inject the creation date time if the model has an id" do
34
+ GOM::Object.stub :id => "id"
35
+ @model.should_not_receive(:created_at=).with(@now)
36
+ @action.perform @context
37
+ end
38
+
39
+ it "should return nil" do
40
+ response = @action.perform @context
41
+ response.should be_nil
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -8,8 +8,7 @@ describe Moon::Action::Model::Destroy do
8
8
  @context = Moon::Context.new
9
9
  @context.models[:model] = @model
10
10
 
11
- described_class.model_symbol = :model
12
- @action = described_class.new @context
11
+ @action = described_class.new :model
13
12
  end
14
13
 
15
14
  describe "perform" do
@@ -18,13 +17,13 @@ describe Moon::Action::Model::Destroy do
18
17
  GOM::Storage.stub :remove => nil
19
18
  end
20
19
 
21
- it "should rmeove the model" do
20
+ it "should remove the model" do
22
21
  GOM::Storage.should_receive(:remove).with(@model)
23
- @action.perform
22
+ @action.perform @context
24
23
  end
25
24
 
26
25
  it "should return 'ok' message response" do
27
- response = @action.perform
26
+ response = @action.perform @context
28
27
  response.should be_instance_of(Moon::Response::JSON::Message)
29
28
  response.status.should == 200
30
29
  end
@@ -32,53 +31,16 @@ describe Moon::Action::Model::Destroy do
32
31
  it "should do nothing if the model isn't initialized" do
33
32
  @context.models[:model] = nil
34
33
  GOM::Storage.should_not_receive(:remove)
35
- @action.perform
34
+ @action.perform @context
36
35
  end
37
36
 
38
37
  it "should return 'failed' message response if model isn't initialized" do
39
38
  @context.models[:model] = nil
40
- response = @action.perform
39
+ response = @action.perform @context
41
40
  response.should be_instance_of(Moon::Response::JSON::Message)
42
41
  response.status.should == 401
43
42
  end
44
43
 
45
44
  end
46
45
 
47
- describe "model" do
48
-
49
- it "should return the model" do
50
- model = @action.model
51
- model.should == @model
52
- end
53
-
54
- end
55
-
56
- describe "self.model_symbol" do
57
-
58
- it "should return the model symbol" do
59
- model_symbol = described_class.model_symbol
60
- model_symbol.should == :model
61
- end
62
-
63
- end
64
-
65
- describe "self.[]" do
66
-
67
- it "should return a class" do
68
- result = described_class[:model]
69
- result.should be_instance_of(Class)
70
- end
71
-
72
- it "should return a different class" do
73
- result = described_class[:model]
74
- result.should_not == described_class
75
- end
76
-
77
- it "should return a class connected to the given model symbol" do
78
- result = described_class[:model]
79
- result.model_symbol.should == :model
80
- end
81
-
82
- end
83
-
84
46
  end
@@ -5,47 +5,30 @@ describe Moon::Action::Model::Index do
5
5
  before :each do
6
6
  @collection = mock GOM::Object::Collection
7
7
 
8
+ @configuration = mock Moon::Application::Configuration, :formatters => :formatters
9
+ @application = mock Moon::Application, :configuration => @configuration
8
10
  @context = Moon::Context.new
9
- @context.collections[:sites] = @collection
11
+ @context.application = @application
12
+ @context.collections[:test] = @collection
10
13
 
11
14
  @response = mock Moon::Response::JSON::Collection
12
15
  Moon::Response::JSON::Collection.stub :new => @response
13
16
 
14
- described_class.collection_key = :test
15
- @action = described_class.new @context
17
+ @action = described_class.new :test
16
18
  end
17
19
 
18
20
  describe "perform" do
19
21
 
20
22
  it "should initialize a collection response" do
21
- Moon::Response::JSON::Collection.should_receive(:new).and_return(@response)
22
- @action.perform
23
+ Moon::Response::JSON::Collection.should_receive(:new).with(@collection, :formatters).and_return(@response)
24
+ @action.perform @context
23
25
  end
24
26
 
25
27
  it "should return the response" do
26
- response = @action.perform
28
+ response = @action.perform @context
27
29
  response.should == @response
28
30
  end
29
31
 
30
32
  end
31
33
 
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
34
  end
@@ -5,45 +5,53 @@ describe Moon::Action::Model::Show do
5
5
  before :each do
6
6
  @model = mock Object
7
7
 
8
+ @configuration = mock Moon::Application::Configuration, :formatters => :formatters
9
+ @application = mock Moon::Application, :configuration => @configuration
8
10
  @context = Moon::Context.new
11
+ @context.application = @application
9
12
  @context.models[:test] = @model
10
13
 
11
- @response = mock Moon::Response::JSON::Model
12
- Moon::Response::JSON::Model.stub :new => @response
14
+ @model_response = mock Moon::Response::JSON::Model
15
+ Moon::Response::JSON::Model.stub :new => @model_response
13
16
 
14
- described_class.model_key = :test
15
- @action = described_class.new @context
17
+ @message_response = mock Moon::Response::JSON::Message
18
+ Moon::Response::JSON::Message.stub :new => @message_response
19
+
20
+ @action = described_class.new :test
16
21
  end
17
22
 
18
- describe "perform" do
23
+ describe "#perform" do
19
24
 
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
25
+ context "for an existing model" do
26
+
27
+ it "should initialize #{Moon::Response::JSON::Model}" do
28
+ Moon::Response::JSON::Model.should_receive(:new).with(:test, @model, :formatters).and_return(@model_response)
29
+ @action.perform @context
30
+ end
31
+
32
+ it "should return the model response" do
33
+ response = @action.perform @context
34
+ response.should == @model_response
35
+ end
24
36
 
25
- it "should return the response" do
26
- response = @action.perform
27
- response.should == @response
28
37
  end
29
38
 
30
- end
39
+ context "for a not-existing model" do
31
40
 
32
- describe "self.[]" do
41
+ before :each do
42
+ @context.models[:test] = nil
43
+ end
33
44
 
34
- it "should return a class" do
35
- result = described_class[:test]
36
- result.should be_instance_of(Class)
37
- end
45
+ it "should initialize #{Moon::Response::JSON::Message}" do
46
+ Moon::Response::JSON::Message.should_receive(:new).with(404, "Model not found.").and_return(@message_response)
47
+ @action.perform @context
48
+ end
38
49
 
39
- it "should return a different class" do
40
- result = described_class[:test]
41
- result.should_not == described_class
42
- end
50
+ it "should return the message response" do
51
+ response = @action.perform @context
52
+ response.should == @message_response
53
+ end
43
54
 
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
55
  end
48
56
 
49
57
  end
@@ -0,0 +1,77 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe Moon::Action::Model::Store do
4
+
5
+ before :each do
6
+ @model = mock Object
7
+
8
+ @application = mock Moon::Application, :storage_name => :dump
9
+ @context = Moon::Context.new
10
+ @context.application = @application
11
+
12
+ GOM::Storage.stub :store => nil
13
+ GOM::Object.stub :storage_name => :test
14
+
15
+ @message_response = mock Moon::Response::JSON::Message
16
+ Moon::Response::JSON::Message.stub :new => @message_response
17
+
18
+ @action = described_class.new :model
19
+ end
20
+
21
+ describe "perform" do
22
+
23
+ context "with an initialized model" do
24
+
25
+ before :each do
26
+ @context.models[:model] = @model
27
+ end
28
+
29
+ it "should fetch the storage name of the model" do
30
+ GOM::Object.should_receive(:storage_name).with(@model).and_return(:test)
31
+ @action.perform @context
32
+ end
33
+
34
+ it "should store the model on the right storage" do
35
+ GOM::Storage.should_receive(:store).with(@model, :test)
36
+ @action.perform @context
37
+ end
38
+
39
+ it "should store the model on the application default storage if it hasn't stored before" do
40
+ GOM::Object.stub :storage_name => nil
41
+ GOM::Storage.should_receive(:store).with(@model, :dump)
42
+ @action.perform @context
43
+ end
44
+
45
+ it "should return nil" do
46
+ response = @action.perform @context
47
+ response.should be_nil
48
+ end
49
+
50
+ end
51
+
52
+ context "without an initialized model" do
53
+
54
+ before :each do
55
+ @context.models[:model] = nil
56
+ end
57
+
58
+ it "should do nothing if the model isn't initialized" do
59
+ GOM::Storage.should_not_receive(:store)
60
+ @action.perform @context
61
+ end
62
+
63
+ it "should initialize the 'failed' message response if the model isn't initialized" do
64
+ Moon::Response::JSON::Message.should_receive(:new).with(200, "No model initialized.").and_return(@message_response)
65
+ @action.perform @context
66
+ end
67
+
68
+ it "should return 'failed' message response if the model isn't initialized" do
69
+ response = @action.perform @context
70
+ response.should == @message_response
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -3,32 +3,46 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..
3
3
  describe Moon::Action::Models::Finder do
4
4
 
5
5
  before :each do
6
- @context = Moon::Context.new({ }, :site_id => "test:4", :test => "test value")
6
+ @context = Moon::Context.new
7
+ @context.session[:user_id] = "user_id"
8
+ @context.parameters[:site_id] = "site_id"
9
+ @context.parameters[:test] = "test value"
10
+
11
+ @user = mock Object
12
+ @site = mock Object
13
+ GOM::Storage.stub(:fetch) do |id|
14
+ { "user_id" => @user, "site_id" => @site }[id]
15
+ end
7
16
 
8
- @model = Object.new
9
- GOM::Storage.stub :fetch => @model
17
+ @action = described_class.new
10
18
  end
11
19
 
12
20
  describe "self.perform" do
13
21
 
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
22
+ it "should fetch the models using the right ids" do
23
+ GOM::Storage.should_receive(:fetch).with("user_id").ordered.and_return(@user)
24
+ GOM::Storage.should_receive(:fetch).with("site_id").ordered.and_return(@site)
25
+ @action.perform @context
26
+ end
27
+
28
+ it "should set the found parameters-models in the context" do
29
+ @action.perform @context
30
+ @context.models[:site].should == @site
17
31
  end
18
32
 
19
- it "should set the found models in the context" do
20
- described_class.perform @context
21
- @context.models[:site].should == @model
33
+ it "should set the found session-models in the context" do
34
+ @action.perform @context
35
+ @context.models[:current_user].should == @user
22
36
  end
23
37
 
24
38
  it "should return nil" do
25
- response = described_class.perform @context
39
+ response = @action.perform @context
26
40
  response.should be_nil
27
41
  end
28
42
 
29
43
  it "should not fetch non-id values" do
30
44
  GOM::Storage.should_not_receive(:fetch).with("test value")
31
- described_class.perform @context
45
+ @action.perform @context
32
46
  end
33
47
 
34
48
  end
@@ -8,33 +8,35 @@ describe Moon::Action::Models::Initializer do
8
8
  @model = mock Object, :name= => nil, :address= => nil
9
9
  @model_class = mock Class, :new => @model
10
10
  Object.stub :const_get => @model_class
11
+
12
+ @action = described_class.new
11
13
  end
12
14
 
13
15
  describe "perform" do
14
16
 
15
17
  it "should get the right model class" do
16
18
  Object.should_receive(:const_get).with("Site").and_return(@model_class)
17
- described_class.perform @context
19
+ @action.perform @context
18
20
  end
19
21
 
20
22
  it "should initialize the model" do
21
23
  @model_class.should_receive(:new).and_return(@model)
22
- described_class.perform @context
24
+ @action.perform @context
23
25
  end
24
26
 
25
27
  it "should set the model's attributes" do
26
28
  @model.should_receive(:name=).with("Test Site")
27
29
  @model.should_receive(:address=).with("Test Address")
28
- described_class.perform @context
30
+ @action.perform @context
29
31
  end
30
32
 
31
33
  it "should set the model to the context" do
32
- described_class.perform @context
34
+ @action.perform @context
33
35
  @context.models[:site].should == @model
34
36
  end
35
37
 
36
38
  it "should return nil" do
37
- response = described_class.perform @context
39
+ response = @action.perform @context
38
40
  response.should be_nil
39
41
  end
40
42