From 9a3dfc66b1a13f6d947e9566e2054f9743246b88 Mon Sep 17 00:00:00 2001 From: Vladimir Kochnev Date: Mon, 10 Aug 2015 12:25:21 +0300 Subject: [PATCH] Allow global settings on Grape::Entity. This fixes a regression introduced after merging a #134. It's something undocumented before but it worked before and I don't see anything harmful in the presence of this feature. Fixes #166. --- .rubocop_todo.yml | 20 ++++++++++---------- CHANGELOG.md | 4 ++++ lib/grape_entity/entity.rb | 19 ++++++++++++++----- lib/grape_entity/version.rb | 2 +- spec/grape_entity/entity_spec.rb | 27 +++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 16 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index cacf406b..b9dbc0b0 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,38 +1,38 @@ # This configuration was generated by `rubocop --auto-gen-config` -# on 2015-05-21 22:47:03 +0700 using RuboCop version 0.31.0. +# on 2015-08-10 12:30:52 +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: 8 +# Offense count: 7 Metrics/AbcSize: - Max: 51 + Max: 45 # Offense count: 1 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 328 + Max: 333 -# Offense count: 5 +# Offense count: 4 Metrics/CyclomaticComplexity: Max: 17 -# Offense count: 176 +# Offense count: 193 # Configuration parameters: AllowURI, URISchemes. Metrics/LineLength: Max: 146 -# Offense count: 7 +# Offense count: 8 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 32 + Max: 26 -# Offense count: 5 +# Offense count: 7 Metrics/PerceivedComplexity: Max: 15 -# Offense count: 31 +# Offense count: 37 Style/Documentation: Enabled: false diff --git a/CHANGELOG.md b/CHANGELOG.md index e7ddfe51..4ce05886 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +0.4.8 (2015-08-10) +================== +* [#167](https://github.com/ruby-grape/grape-entity/pull/167): Regression: global settings (exposures, formatters) on `Grape::Entity` should work: [#166](https://github.com/ruby-grape/grape-entity/issues/166) - [@marshall-lee](http://github.com/marshall-lee). + 0.4.7 (2015-08-03) ================== * [#164](https://github.com/ruby-grape/grape-entity/pull/164): Regression: entity instance methods were exposed with `NoMethodError`: [#163](https://github.com/ruby-grape/grape-entity/issues/163) - [@marshall-lee](http://github.com/marshall-lee). diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index 644d928d..88327b9b 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -112,12 +112,18 @@ class << self attr_accessor :nested_exposures end + @exposures = {} + @root_exposures = {} + @nested_exposures = {} + @nested_attribute_names = {} + @formatters = {} + def self.inherited(subclass) - subclass.exposures = exposures.try(:dup) || {} - subclass.root_exposures = root_exposures.try(:dup) || {} - subclass.nested_exposures = nested_exposures.try(:dup) || {} - subclass.nested_attribute_names = nested_attribute_names.try(:dup) || {} - subclass.formatters = formatters.try(:dup) || {} + subclass.exposures = exposures.try(:dup) + subclass.root_exposures = root_exposures.try(:dup) + subclass.nested_exposures = nested_exposures.try(:dup) + subclass.nested_attribute_names = nested_attribute_names.try(:dup) + subclass.formatters = formatters.try(:dup) end # This method is the primary means by which you will declare what attributes @@ -183,7 +189,10 @@ def self.expose(*args, &block) end def self.unexpose(attribute) + root_exposures.delete(attribute) exposures.delete(attribute) + nested_exposures.delete(attribute) + nested_attribute_names.delete(attribute) end # Set options that will be applied to any exposures declared inside the block. diff --git a/lib/grape_entity/version.rb b/lib/grape_entity/version.rb index ff8bdcf1..04cd28fa 100644 --- a/lib/grape_entity/version.rb +++ b/lib/grape_entity/version.rb @@ -1,3 +1,3 @@ module GrapeEntity - VERSION = '0.4.7' + VERSION = '0.4.8' end diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index af4ac549..d344aab6 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -278,6 +278,26 @@ class Parent < Person subject.expose(:size, format_with: :size_formatter) expect(subject.represent(object).send(:value_for, :size)).to eq object.class.to_s end + + it 'works global on Grape::Entity' do + Grape::Entity.format_with :size_formatter do |_date| + self.object.class.to_s + end + object = {} + + subject.expose(:size, format_with: :size_formatter) + expect(subject.represent(object).send(:value_for, :size)).to eq object.class.to_s + end + end + + it 'works global on Grape::Entity' do + Grape::Entity.expose :x + object = { x: 11, y: 22 } + expect(Grape::Entity.represent(object).send(:value_for, :x)).to eq 11 + subject.expose :y + expect(subject.represent(object).send(:value_for, :x)).to eq 11 + expect(subject.represent(object).send(:value_for, :y)).to eq 22 + Grape::Entity.unexpose :x end end @@ -310,6 +330,13 @@ class Parent < Person end end end + + it 'works global on Grape::Entity' do + Grape::Entity.expose :x + expect(Grape::Entity.exposures).to eq(x: {}) + Grape::Entity.unexpose :x + expect(Grape::Entity.exposures).to eq({}) + end end describe '.with_options' do