This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to specify :default value for relationships
* Like with Property, :default may be a value or an object that responds to #call [#1099 state:resolved]
- Loading branch information
Showing
15 changed files
with
272 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module DataMapper | ||
module Subject | ||
# Returns a default value of the subject for given resource | ||
# | ||
# When default value is a callable object, it is called with resource | ||
# and subject passed as arguments. | ||
# | ||
# @param [Resource] resource | ||
# the model instance for which the default is to be set | ||
# | ||
# @return [Object] | ||
# the default value of this subject for +resource+ | ||
# | ||
# @api semipublic | ||
def default_for(resource) | ||
if @default.respond_to?(:call) | ||
@default.call(resource, self) | ||
else | ||
@default.try_dup | ||
end | ||
end | ||
|
||
# Returns true if the subject has a default value | ||
# | ||
# @return [Boolean] | ||
# true if the subject has a default value | ||
# | ||
# @api semipublic | ||
def default? | ||
@options.key?(:default) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) | ||
|
||
describe 'One to Many Associations' do | ||
before :all do | ||
module ::Blog | ||
class Article | ||
include DataMapper::Resource | ||
|
||
property :title, String, :key => true | ||
property :body, Text, :required => true | ||
end | ||
end | ||
|
||
@article_model = Blog::Article | ||
end | ||
|
||
supported_by :all do | ||
before :all do | ||
@article = @article_model.new(:title => 'DataMapper Rocks!', :body => 'TSIA') | ||
end | ||
|
||
describe 'acts like a subject' do | ||
before do | ||
n = @article_model.n | ||
|
||
@subject_without_default = @article_model.has(n, :without_default, @article_model, :through => DataMapper::Resource) | ||
@subject_with_default = @article_model.has(n, :with_default, @article_model, :through => DataMapper::Resource, :default => [ @article ]) | ||
@subject_with_default_callable = @article_model.has(n, :with_default_callable, @article_model, :through => DataMapper::Resource, :default => lambda { |resource, relationship| [ @article ] }) | ||
|
||
@subject_without_default_value = [] | ||
@subject_with_default_value = [ @article ] | ||
@subject_with_default_callable_value = [ @article ] | ||
|
||
@resource = @article_model.new | ||
end | ||
|
||
it_should_behave_like 'A semipublic Subject' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) | ||
|
||
describe 'One to Many Associations' do | ||
before :all do | ||
module ::Blog | ||
class Article | ||
include DataMapper::Resource | ||
|
||
property :title, String, :key => true | ||
property :body, Text, :required => true | ||
end | ||
end | ||
|
||
@article_model = Blog::Article | ||
end | ||
|
||
supported_by :all do | ||
before :all do | ||
@article = @article_model.new(:title => 'DataMapper Rocks!', :body => 'TSIA') | ||
end | ||
|
||
describe 'acts like a subject' do | ||
before do | ||
n = @article_model.n | ||
|
||
@subject_without_default = @article_model.has(n, :without_default, @article_model) | ||
@subject_with_default = @article_model.has(n, :with_default, @article_model, :default => [ @article ]) | ||
@subject_with_default_callable = @article_model.has(n, :with_default_callable, @article_model, :default => lambda { |resource, relationship| [ @article ] }) | ||
|
||
@subject_without_default_value = [] | ||
@subject_with_default_value = [ @article ] | ||
@subject_with_default_callable_value = [ @article ] | ||
|
||
@resource = @article_model.new | ||
end | ||
|
||
it_should_behave_like 'A semipublic Subject' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) | ||
|
||
describe 'One to One Associations' do | ||
before :all do | ||
module ::Blog | ||
class Article | ||
include DataMapper::Resource | ||
|
||
property :title, String, :key => true | ||
property :body, Text, :required => true | ||
end | ||
end | ||
|
||
@article_model = Blog::Article | ||
end | ||
|
||
supported_by :all do | ||
before :all do | ||
@article = @article_model.new(:title => 'DataMapper Rocks!', :body => 'TSIA') | ||
end | ||
|
||
describe 'acts like a subject' do | ||
before do | ||
@subject_without_default = @article_model.has(1, :without_default, @article_model) | ||
@subject_with_default = @article_model.has(1, :with_default, @article_model, :default => @article) | ||
@subject_with_default_callable = @article_model.has(1, :with_default_callable, @article_model, :default => lambda { |resource, relationship| @article }) | ||
|
||
@subject_without_default_value = nil | ||
@subject_with_default_value = @article | ||
@subject_with_default_callable_value = @article | ||
|
||
@resource = @article_model.new | ||
end | ||
|
||
it_should_behave_like 'A semipublic Subject' | ||
end | ||
end | ||
end |
Oops, something went wrong.