moon 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -0
- data/Rakefile +10 -3
- data/lib/moon.rb +0 -2
- data/lib/moon/action.rb +4 -3
- data/lib/moon/action/inject_timestamps.rb +44 -0
- data/lib/moon/action/model.rb +1 -2
- data/lib/moon/action/model/destroy.rb +4 -26
- data/lib/moon/action/model/index.rb +10 -17
- data/lib/moon/action/model/show.rb +9 -23
- data/lib/moon/action/model/store.rb +20 -0
- data/lib/moon/action/models/finder.rb +14 -14
- data/lib/moon/action/models/initializer.rb +6 -5
- data/lib/moon/action/models/updater.rb +1 -4
- data/lib/moon/action/rebuild_arrays.rb +1 -4
- data/lib/moon/action/{reference_object.rb → reference.rb} +1 -1
- data/lib/moon/action/respond_blank.rb +9 -0
- data/lib/moon/action/transfer.rb +30 -0
- data/lib/moon/action/valid_models_required.rb +13 -6
- data/lib/moon/application.rb +12 -7
- data/lib/moon/application/configuration.rb +55 -0
- data/lib/moon/application/rack.rb +17 -7
- data/lib/moon/context.rb +1 -1
- data/lib/moon/formatter.rb +4 -13
- data/lib/moon/formatter/generic.rb +17 -4
- data/lib/moon/response/json.rb +1 -0
- data/lib/moon/response/json/blank.rb +9 -0
- data/lib/moon/response/json/collection.rb +2 -2
- data/lib/moon/response/json/model.rb +3 -3
- data/lib/moon/validator.rb +17 -45
- data/lib/moon/validator/format.rb +4 -23
- data/lib/moon/validator/length.rb +8 -33
- data/lib/moon/validator/presence.rb +4 -17
- data/spec/lib/moon/action/inject_timestamps_spec.rb +46 -0
- data/spec/lib/moon/action/model/destroy_spec.rb +6 -44
- data/spec/lib/moon/action/model/index_spec.rb +8 -25
- data/spec/lib/moon/action/model/show_spec.rb +33 -25
- data/spec/lib/moon/action/model/store_spec.rb +77 -0
- data/spec/lib/moon/action/models/finder_spec.rb +25 -11
- data/spec/lib/moon/action/models/initializer_spec.rb +7 -5
- data/spec/lib/moon/action/models/updater_spec.rb +5 -3
- data/spec/lib/moon/action/rebuild_arrays_spec.rb +3 -3
- data/spec/lib/moon/action/{reference_object_spec.rb → reference_spec.rb} +1 -1
- data/spec/lib/moon/action/respond_blank_spec.rb +24 -0
- data/spec/lib/moon/action/transfer_spec.rb +30 -0
- data/spec/lib/moon/action/valid_models_required_spec.rb +19 -17
- data/spec/lib/moon/application/configuration_spec.rb +75 -0
- data/spec/lib/moon/application/rack_spec.rb +6 -5
- data/spec/lib/moon/formatter/generic_spec.rb +13 -3
- data/spec/lib/moon/formatter_spec.rb +4 -10
- data/spec/lib/moon/response/json/blank_spec.rb +19 -0
- data/spec/lib/moon/response/json/collection_spec.rb +5 -4
- data/spec/lib/moon/response/json/model_spec.rb +5 -4
- data/spec/lib/moon/validator/format_spec.rb +8 -25
- data/spec/lib/moon/validator/length_spec.rb +10 -36
- data/spec/lib/moon/validator/presence_spec.rb +11 -28
- data/spec/lib/moon/validator_spec.rb +10 -59
- metadata +34 -28
- data/lib/moon/action/base.rb +0 -16
- data/lib/moon/action/model/create.rb +0 -45
- data/lib/moon/action/model/update.rb +0 -42
- data/lib/moon/application/routes.rb +0 -16
- data/lib/moon/formatter/base.rb +0 -15
- data/spec/lib/moon/action/base_spec.rb +0 -29
- data/spec/lib/moon/action/model/create_spec.rb +0 -111
- data/spec/lib/moon/action/model/update_spec.rb +0 -91
- data/spec/lib/moon/application/routes_spec.rb +0 -26
- data/spec/lib/moon/formatter/base_spec.rb +0 -30
data/lib/moon/action/base.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
|
2
|
-
# Base class for all actions. It simply forces subclasses to implement a 'perform' method
|
3
|
-
# and makes the context available.
|
4
|
-
class Moon::Action::Base
|
5
|
-
|
6
|
-
attr_reader :context
|
7
|
-
|
8
|
-
def initialize(context)
|
9
|
-
@context = context
|
10
|
-
end
|
11
|
-
|
12
|
-
def perform
|
13
|
-
raise NotImplementedError, "method perform should be implemented in #{self.class}"
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
|
2
|
-
# Generic model create action. Stores the initialized model to the configured storage.
|
3
|
-
class Moon::Action::Model::Create
|
4
|
-
|
5
|
-
def initialize(context)
|
6
|
-
@context = context
|
7
|
-
end
|
8
|
-
|
9
|
-
def perform
|
10
|
-
model = self.model
|
11
|
-
if model
|
12
|
-
GOM::Storage.store model, @context.storage_name
|
13
|
-
Moon::Response::JSON::Model.new self.class.model_symbol, model
|
14
|
-
else
|
15
|
-
Moon::Response::JSON::Message.new 200, "No model initialized."
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def model
|
20
|
-
@context.models[self.class.model_symbol]
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.model_symbol
|
24
|
-
Moon::Utility::String.underscore(model_class.to_s).split("/").last.to_sym
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.model_class=(value)
|
28
|
-
@model_class = value
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.model_class
|
32
|
-
@model_class
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.perform(context)
|
36
|
-
new(context).perform
|
37
|
-
end
|
38
|
-
|
39
|
-
def self.[](model_class)
|
40
|
-
klass = self.dup
|
41
|
-
klass.model_class = model_class
|
42
|
-
klass
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
|
2
|
-
# Generic model update action. Stores the specified model to the storage.
|
3
|
-
class Moon::Action::Model::Update
|
4
|
-
|
5
|
-
def initialize(context)
|
6
|
-
@context = context
|
7
|
-
end
|
8
|
-
|
9
|
-
def perform
|
10
|
-
model = self.model
|
11
|
-
if model
|
12
|
-
storage_name = GOM::Object.storage_name model
|
13
|
-
GOM::Storage.store model, storage_name
|
14
|
-
Moon::Response::JSON::Message.new 200, "#{@model_symbol} updated."
|
15
|
-
else
|
16
|
-
Moon::Response::JSON::Message.new 401, "#{@model_symbol} update failed."
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def model
|
21
|
-
@context.models[self.class.model_symbol]
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.model_symbol=(value)
|
25
|
-
@model_symbol = value
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.model_symbol
|
29
|
-
@model_symbol
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.perform(context)
|
33
|
-
new(context).perform
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.[](model_symbol)
|
37
|
-
klass = self.dup
|
38
|
-
klass.model_symbol = model_symbol
|
39
|
-
klass
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'configure'
|
2
|
-
|
3
|
-
# Handler for the application routes.
|
4
|
-
class Moon::Application::Routes
|
5
|
-
|
6
|
-
attr_reader :rack
|
7
|
-
|
8
|
-
def initialize
|
9
|
-
@rack = Moon::Application::Rack.new
|
10
|
-
end
|
11
|
-
|
12
|
-
def define(&block)
|
13
|
-
@rack.routes = [ Configure.process(&block)[:route] ].flatten
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
data/lib/moon/formatter/base.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
|
2
|
-
|
3
|
-
describe Moon::Action::Base do
|
4
|
-
|
5
|
-
before :each do
|
6
|
-
@context = mock Moon::Context
|
7
|
-
|
8
|
-
@action = described_class.new @context
|
9
|
-
end
|
10
|
-
|
11
|
-
describe "context" do
|
12
|
-
|
13
|
-
it "should return the context" do
|
14
|
-
@action.context.should == @context
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
describe "perform" do
|
20
|
-
|
21
|
-
it "should raise a #{NotImplementedError}" do
|
22
|
-
lambda do
|
23
|
-
@action.perform
|
24
|
-
end.should raise_error(NotImplementedError)
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
@@ -1,111 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
|
2
|
-
|
3
|
-
describe Moon::Action::Model::Create do
|
4
|
-
|
5
|
-
before :each do
|
6
|
-
@model_class = mock Class, :to_s => "Model"
|
7
|
-
@model = mock Object, :class => @model_class
|
8
|
-
|
9
|
-
@context = Moon::Context.new
|
10
|
-
@context.storage_name = :dump
|
11
|
-
@context.models[:model] = @model
|
12
|
-
|
13
|
-
described_class.model_class = @model_class
|
14
|
-
@action = described_class.new @context
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "perform" do
|
18
|
-
|
19
|
-
before :each do
|
20
|
-
GOM::Storage.stub :store
|
21
|
-
GOM::Object.stub :id => "test:object_1"
|
22
|
-
|
23
|
-
@model_response = mock Moon::Response::JSON::Model
|
24
|
-
Moon::Response::JSON::Model.stub :new => @model_response
|
25
|
-
|
26
|
-
@message_response = mock Moon::Response::JSON::Message
|
27
|
-
Moon::Response::JSON::Message.stub :new => @message_response
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should store the model on the right storage" do
|
31
|
-
GOM::Storage.should_receive(:store).with(@model, :dump)
|
32
|
-
@action.perform
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should initialize #{Moon::Response::JSON::Model}" do
|
36
|
-
Moon::Response::JSON::Model.should_receive(:new).with(:model, @model).and_return(@model_response)
|
37
|
-
@action.perform
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should return the model response" do
|
41
|
-
response = @action.perform
|
42
|
-
response.should == @model_response
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should do nothing if the model isn't initialized" do
|
46
|
-
@context.models[:model] = nil
|
47
|
-
GOM::Storage.should_not_receive(:store)
|
48
|
-
@action.perform
|
49
|
-
end
|
50
|
-
|
51
|
-
it "should initialize the 'failed' message response if the model isn't initialized" do
|
52
|
-
@context.models[:model] = nil
|
53
|
-
Moon::Response::JSON::Message.should_receive(:new).with(200, "No model initialized.").and_return(@message_response)
|
54
|
-
@action.perform
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should return 'failed' message response if the model isn't initialized" do
|
58
|
-
@context.models[:model] = nil
|
59
|
-
response = @action.perform
|
60
|
-
response.should == @message_response
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
describe "model" do
|
66
|
-
|
67
|
-
it "should return the model" do
|
68
|
-
model = @action.model
|
69
|
-
model.should == @model
|
70
|
-
end
|
71
|
-
|
72
|
-
end
|
73
|
-
|
74
|
-
describe "self.model_symbol" do
|
75
|
-
|
76
|
-
it "should return the model symbol" do
|
77
|
-
model_symbol = described_class.model_symbol
|
78
|
-
model_symbol.should == :model
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
describe "self.model_class" do
|
84
|
-
|
85
|
-
it "should return the model class" do
|
86
|
-
model_class = described_class.model_class
|
87
|
-
model_class.should == @model_class
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
91
|
-
|
92
|
-
describe "self.[]" do
|
93
|
-
|
94
|
-
it "should return a class" do
|
95
|
-
result = described_class[Object]
|
96
|
-
result.should be_instance_of(Class)
|
97
|
-
end
|
98
|
-
|
99
|
-
it "should return a different class" do
|
100
|
-
result = described_class[Object]
|
101
|
-
result.should_not == described_class
|
102
|
-
end
|
103
|
-
|
104
|
-
it "should return a class connected to the given model class" do
|
105
|
-
result = described_class[Object]
|
106
|
-
result.model_class.should == Object
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
@@ -1,91 +0,0 @@
|
|
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
|
@@ -1,26 +0,0 @@
|
|
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
|
@@ -1,30 +0,0 @@
|
|
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
|