Skip to content

Class reopening and inheritance. #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-08-02 19:30:25 +0300 using RuboCop version 0.31.0.
# on 2015-08-07 13:25:47 +0300 using RuboCop version 0.31.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 6
Metrics/AbcSize:
Max: 33
Max: 36

# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 198
Max: 208

# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 11

# Offense count: 208
# Offense count: 209
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 146

# Offense count: 8
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 28
Max: 31

# Offense count: 5
Metrics/PerceivedComplexity:
Expand Down
13 changes: 13 additions & 0 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,16 @@ class << self
# Returns all formatters that are registered for this and it's ancestors
# @return [Hash] of formatters
attr_accessor :formatters
attr_accessor :inherited_entities
end

@inherited_entities = []

def self.inherited(subclass)
subclass.root_exposure = root_exposure.try(:dup) || build_root_exposure
subclass.formatters = formatters.try(:dup) || {}
inherited_entities << subclass
subclass.inherited_entities = []
end

# This method is the primary means by which you will declare what attributes
Expand Down Expand Up @@ -173,6 +178,10 @@ def self.expose(*args, &block)
@nesting_stack.pop
end
end

inherited_entities.each do |entity|
entity.expose(*args, &block)
end
end

def self.build_root_exposure
Expand Down Expand Up @@ -264,6 +273,10 @@ def self.documentation
def self.format_with(name, &block)
fail ArgumentError, 'You must pass a block for formatters' unless block_given?
formatters[name.to_sym] = block

inherited_entities.each do |entity|
entity.format_with(name, &block)
end
end

# This allows you to set a root element name for your representation.
Expand Down
48 changes: 48 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,47 @@ class Parent < Person
expect(subject.represent({ name: 'bar' }, serializable: true)).to eq(email: nil, name: 'bar')
expect(child_class.represent({ name: 'bar' }, serializable: true)).to eq(email: nil, name: 'foo')
end

describe 'parent class reopening' do
it 'is supported' do
subject.expose :name
child_class = Class.new(subject)
subject.expose :email

object = OpenStruct.new(name: 'bar', email: 'foo@bar')
expected = { name: 'bar', email: 'foo@bar' }

expect(subject.represent(object, serializable: true)).to eq(expected)
expect(child_class.represent(object, serializable: true)).to eq(expected)
end

it 'puts exposures in the right order' do
subject.expose :x
child_class = Class.new(subject) do
expose :z
end
subject.expose :y
object = {
x: 1,
y: 2,
z: 3
}
expect(child_class.represent(object, serializable: true).keys).to eq([:x, :y, :z])
end

it 'just prepends parent class exposures to the inherited class' do
subject.expose :x
child_class = Class.new(subject) do
expose :y, proc: -> (_obj, _opts) { 'specific' }
end
subject.expose :y
object = {
x: 1,
y: 2
}
expect(child_class.represent(object, serializable: true)).to eq(x: 1, y: 'specific')
end
end
end

context 'register formatters' do
Expand All @@ -290,6 +331,13 @@ class Parent < Person
expect(child_class.formatters).to eq subject.formatters
end

it 'inherits formatters from ancestors with reopening' do
child_class = Class.new(subject)
subject.format_with :timestamp, &date_formatter

expect(child_class.formatters).to eq subject.formatters
end

it 'does not allow registering a formatter without a block' do
expect { subject.format_with :foo }.to raise_error ArgumentError
end
Expand Down