-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adrian Mugnolo and Santiago Pastorino
committed
Jan 10, 2014
1 parent
cf91146
commit 0ba4830
Showing
6 changed files
with
137 additions
and
134 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 was deleted.
Oops, something went wrong.
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,82 @@ | ||
require 'singleton' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class Configuration | ||
class Null | ||
include Singleton | ||
|
||
def method_missing(*) | ||
nil | ||
end | ||
|
||
def respond_to?(*) | ||
true | ||
end | ||
end | ||
|
||
attr_accessor :parent | ||
|
||
class << self | ||
def global | ||
@global ||= new default_options | ||
end | ||
|
||
def default_options | ||
{ embed: :objects } | ||
end | ||
end | ||
|
||
def build(options = {}) | ||
self.class.new options, self | ||
end | ||
|
||
def initialize(options = {}, parent = Null.instance) | ||
@root = read_option options, :root | ||
@embed = read_option options, :embed | ||
@embed_in_root = read_option options, :embed_in_root | ||
@parent = parent | ||
end | ||
|
||
def root | ||
return_first @root, parent.root | ||
end | ||
|
||
def embed | ||
return_first @embed, parent.embed | ||
end | ||
|
||
def embed_in_root | ||
return_first @embed_in_root, parent.embed_in_root | ||
end | ||
|
||
# FIXME: Get rid of this mess. | ||
def embed_objects=(value) | ||
@embed = :objects if value | ||
end | ||
|
||
# FIXME: Get rid of this mess. | ||
def embed_ids=(value) | ||
@embed = :ids if value | ||
end | ||
|
||
def embed_objects | ||
[:objects, :object].include? embed | ||
end | ||
|
||
def embed_ids | ||
[:ids, :id].include? embed | ||
end | ||
|
||
private | ||
|
||
def read_option(options, name) | ||
options[name] || false if options.has_key? name | ||
end | ||
|
||
def return_first(*values) | ||
values.compact.first | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class Configuration | ||
class GlobalTest < Minitest::Test | ||
def test_returns_global_configuration | ||
assert_kind_of Configuration, Configuration.global | ||
end | ||
|
||
def test_global_configuration_returns_the_same_instance | ||
assert_equal Configuration.global.object_id, Configuration.global.object_id | ||
end | ||
|
||
def test_global_configuration_has_default_options_set | ||
assert Configuration.default_options.all? do |name, value| | ||
Configuration.global.send(name) == value | ||
end | ||
end | ||
end | ||
|
||
class OptionsTest < Minitest::Test | ||
def setup | ||
@configuration = Configuration.global.build(root: 'root', embed: :ids, embed_in_root: false) | ||
end | ||
|
||
def test_configuration_has_root_option | ||
assert_equal 'root', @configuration.root | ||
end | ||
|
||
def test_configuration_has_embed_option | ||
assert_equal :ids, @configuration.embed | ||
end | ||
|
||
def test_configuration_has_embed_in_root_option | ||
assert_equal false, @configuration.embed_in_root | ||
end | ||
end | ||
end | ||
end | ||
end |