forked from ruby-grape/grape-entity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring: extract delegating logic.
This one solves many problems at once: - Fixes ruby-grape#140. For this one I changed specs a bit. It's breaking but I think it's worth it. - Fixes ruby-grape#142 differently because now `respond_to?(name, true)` stuff is incapsulated in `Delegator::PlainObject`. - Old `delegate_attribute` catched `NoMethodError` and re-raised it with custom message. It's incorrect because `NoMethodError` can occur deep inside in method call — this exception simply doesn't mean that attribute doesn't exist. - Solves the problem that object is checked to `is_a?(Hash)` and `respond_to?(:fetch, true)` multiple times. - Extracts delegating logic to separate delegator classes. - Removes constructing of `valid_exposures` at all — there's no need in this method now.
- Loading branch information
1 parent
78b141b
commit e9603fe
Showing
8 changed files
with
124 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'grape_entity/delegator/base' | ||
require 'grape_entity/delegator/hash_object' | ||
require 'grape_entity/delegator/fetchable_object' | ||
require 'grape_entity/delegator/plain_object' | ||
|
||
module Grape | ||
class Entity | ||
module Delegator | ||
def self.new(object) | ||
if object.is_a?(Hash) || object.is_a?(OpenStruct) | ||
HashObject.new object | ||
elsif object.respond_to? :fetch, true | ||
FetchableObject.new object | ||
else | ||
PlainObject.new object | ||
end | ||
end | ||
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,17 @@ | ||
module Grape | ||
class Entity | ||
module Delegator | ||
class Base | ||
attr_reader :object | ||
|
||
def initialize(object) | ||
@object = object | ||
end | ||
|
||
def delegatable?(_attribute) | ||
true | ||
end | ||
end | ||
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,11 @@ | ||
module Grape | ||
class Entity | ||
module Delegator | ||
class FetchableObject < Base | ||
def delegate(attribute) | ||
object.fetch attribute | ||
end | ||
end | ||
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,11 @@ | ||
module Grape | ||
class Entity | ||
module Delegator | ||
class HashObject < Base | ||
def delegate(attribute) | ||
object[attribute] | ||
end | ||
end | ||
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,15 @@ | ||
module Grape | ||
class Entity | ||
module Delegator | ||
class PlainObject < Base | ||
def delegate(attribute) | ||
object.send attribute | ||
end | ||
|
||
def delegatable?(attribute) | ||
object.respond_to? attribute, true | ||
end | ||
end | ||
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