diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9d0ce88f..4f2ac36a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,5 +1,5 @@ # 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-10 13:14:22 +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 @@ -12,13 +12,13 @@ Metrics/AbcSize: # Offense count: 2 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 198 + Max: 202 # Offense count: 3 Metrics/CyclomaticComplexity: Max: 11 -# Offense count: 208 +# Offense count: 210 # Configuration parameters: AllowURI, URISchemes. Metrics/LineLength: Max: 146 diff --git a/CHANGELOG.md b/CHANGELOG.md index 66d57e44..87f55fcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ * [#151](https://github.com/ruby-grape/grape-entity/pull/151): Fix: deep projections (`:only`, `:except`) were unaware of nesting: [#156](https://github.com/ruby-grape/grape-entity/issues/156) - [@marshall-lee](https://github.com/marshall-lee). * Your contribution here. +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 e455323d..6e0ae877 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -99,15 +99,26 @@ def entity(options = {}) end class << self - attr_accessor :root_exposure + def root_exposure + @root_exposure ||= Exposure.new(nil, nesting: true) + end + + attr_writer :root_exposure + # Returns all formatters that are registered for this and it's ancestors # @return [Hash] of formatters - attr_accessor :formatters + def formatters + @formatters ||= {} + end + + attr_writer :formatters end + @formatters = {} + def self.inherited(subclass) - subclass.root_exposure = root_exposure.try(:dup) || build_root_exposure - subclass.formatters = formatters.try(:dup) || {} + subclass.root_exposure = root_exposure.dup + subclass.formatters = formatters.dup end # This method is the primary means by which you will declare what attributes @@ -175,10 +186,6 @@ def self.expose(*args, &block) end end - def self.build_root_exposure - Exposure.new(nil, nesting: true) - end - # Returns exposures that have been declared for this Entity on the top level. # @return [Array] of exposures def self.root_exposures diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index d3637f3c..6d952f11 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -322,6 +322,26 @@ class Parent < Person subject.expose(:size, format_with: :size_formatter) expect(subject.represent(object).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).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).value_for(:x)).to eq 11 + subject.expose :y + expect(subject.represent(object).value_for(:x)).to eq 11 + expect(subject.represent(object).value_for(:y)).to eq 22 + Grape::Entity.unexpose :x end end @@ -370,6 +390,13 @@ class Parent < Person end end.to raise_error(/You cannot call 'unexpose`/) end + + it 'works global on Grape::Entity' do + Grape::Entity.expose :x + expect(Grape::Entity.root_exposures[0].attribute).to eq(:x) + Grape::Entity.unexpose :x + expect(Grape::Entity.root_exposures).to eq([]) + end end describe '.with_options' do