Skip to content

Commit

Permalink
Finish extension spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz committed Feb 14, 2020
1 parent ad03919 commit 0b135bc
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 54 deletions.
11 changes: 10 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ Layout/LineLength:
Max: 120

Style/Documentation:
Enabled: false
Enabled: false

RSpec/LetSetup:
Enabled: false

RSpec/MultipleExpectations:
Enabled: false

RSpec/MessageChain:
Enabled: false
122 changes: 121 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,122 @@ PATH
GEM
remote: https://rubygems.org/
specs:
actioncable (6.0.1)
actionpack (= 6.0.1)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.0.1)
actionpack (= 6.0.1)
activejob (= 6.0.1)
activerecord (= 6.0.1)
activestorage (= 6.0.1)
activesupport (= 6.0.1)
mail (>= 2.7.1)
actionmailer (6.0.1)
actionpack (= 6.0.1)
actionview (= 6.0.1)
activejob (= 6.0.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.0.1)
actionview (= 6.0.1)
activesupport (= 6.0.1)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.0.1)
actionpack (= 6.0.1)
activerecord (= 6.0.1)
activestorage (= 6.0.1)
activesupport (= 6.0.1)
nokogiri (>= 1.8.5)
actionview (6.0.1)
activesupport (= 6.0.1)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.0.1)
activesupport (= 6.0.1)
globalid (>= 0.3.6)
activemodel (6.0.1)
activesupport (= 6.0.1)
activerecord (6.0.1)
activemodel (= 6.0.1)
activesupport (= 6.0.1)
activestorage (6.0.1)
actionpack (= 6.0.1)
activejob (= 6.0.1)
activerecord (= 6.0.1)
marcel (~> 0.3.1)
activesupport (6.0.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2)
ast (2.4.0)
builder (3.2.4)
concurrent-ruby (1.1.5)
crass (1.0.5)
diff-lcs (1.3)
erubi (1.9.0)
faker (2.9.0)
i18n (>= 1.6, < 1.8)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.4)
loofah (2.4.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.2)
mimemagic (0.3.3)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.13.0)
nio4r (2.5.2)
nokogiri (1.10.7)
mini_portile2 (~> 2.4.0)
oj (3.10.0)
parallel (1.19.1)
parser (2.7.0.0)
ast (~> 2.4.0)
rack (2.0.8)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (6.0.1)
actioncable (= 6.0.1)
actionmailbox (= 6.0.1)
actionmailer (= 6.0.1)
actionpack (= 6.0.1)
actiontext (= 6.0.1)
actionview (= 6.0.1)
activejob (= 6.0.1)
activemodel (= 6.0.1)
activerecord (= 6.0.1)
activestorage (= 6.0.1)
activesupport (= 6.0.1)
bundler (>= 1.3.0)
railties (= 6.0.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.3.0)
loofah (~> 2.3)
railties (6.0.1)
actionpack (= 6.0.1)
activesupport (= 6.0.1)
method_source
rake (>= 0.8.7)
thor (>= 0.20.3, < 2.0)
rainbow (3.0.0)
rake (10.5.0)
rspec (3.9.0)
Expand Down Expand Up @@ -46,16 +150,32 @@ GEM
rubocop-rspec (1.37.1)
rubocop (>= 0.68.1)
ruby-progressbar (1.10.1)
sprockets (4.0.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.1)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.20.3)
thread_safe (0.3.6)
tzinfo (1.2.6)
thread_safe (~> 0.1)
unicode-display_width (1.6.0)
websocket-driver (0.7.1)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.4)
zeitwerk (2.2.2)

PLATFORMS
ruby

DEPENDENCIES
bright_serializer!
bundler (~> 2)
faker
bright_serializer!
oj
rails
rake (~> 10.0)
rspec (~> 3.0)
rubocop
Expand Down
7 changes: 2 additions & 5 deletions lib/bright_serializer/extensions/cache.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# frozen_string_literal: true

require 'active_support/notifications'

module BrightSerializer
module Extensions
module Cache

def serialize(object)
if @object.respond_to?(:cache_key) && self.class.cache_options
Rails.cache.fetch("#{self.class.name}-#{@object.cache_key}", self.class.cache_options) do
if @object.respond_to?(:cache_key) && self.class.cache_option_attributes
Rails.cache.fetch("#{self.class.name}-#{@object.cache_key}", **self.class.cache_option_attributes) do
super
end
else
Expand Down
10 changes: 4 additions & 6 deletions lib/bright_serializer/extensions/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
# frozen_string_literal: true

require 'active_support/notifications'

module BrightSerializer
module Extensions
module Instrumentation
SERIALIZABLE_HASH_NOTIFICATION = 'render.bright_serializer.serializable_hash'
SERIALIZED_JSON_NOTIFICATION = 'render.bright_serializer.serialized_json'

def to_hash
def serializable_hash
ActiveSupport::Notifications.instrument(SERIALIZABLE_HASH_NOTIFICATION, name: self.class.name) do
super
end
end

alias serialize_hash to_hash
alias to_hash serializable_hash

def to_json(*_args)
def serialized_json(*_args)
ActiveSupport::Notifications.instrument(SERIALIZED_JSON_NOTIFICATION, name: self.class.name) do
super
end
end

alias serialize_json to_json
alias to_json serialized_json
end
end
end
18 changes: 10 additions & 8 deletions lib/bright_serializer/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def self.included(base)
base.prepend Extensions::Cache
end

if defined? ActiveSupport
require_relative 'extensions/instrumentation'
base.prepend Extensions::Instrumentation
end
return unless defined? ActiveSupport

require_relative 'extensions/instrumentation'
base.prepend Extensions::Instrumentation
end

def initialize(object, **options)
Expand Down Expand Up @@ -50,14 +50,14 @@ def serializable_hash

alias to_hash serializable_hash

def serializable_json(*_args)
def serialized_json(*_args)
::Oj.dump(to_hash, DEFAULT_OJ_OPTIONS)
end

alias to_json serializable_json
alias to_json serialized_json

module ClassMethods
attr_reader :attributes_to_serialize, :transform_method
attr_reader :attributes_to_serialize, :transform_method, :cache_option_attributes

def attributes(*attributes, **options, &block)
attributes.each do |key|
Expand Down Expand Up @@ -86,7 +86,9 @@ def run_transform_key(input)
end

def cache_options(cache_options)
@cache_options = {
return unless defined? Rails

@cache_option_attributes = {
cache_length: cache_options[:cache_length] || 5.minutes,
race_condition_ttL: cache_options[:race_condition_ttl] || 5.seconds
}
Expand Down
39 changes: 20 additions & 19 deletions spec/lib/bright_serializer/extensions/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@
require 'bright_serializer/extensions/cache'

RSpec.describe BrightSerializer::Extensions::Cache do
let(:instance) { described_class.new }
let(:instance) { described_class.new }

describe 'using extension' do
context 'When Rails is defined' do
context 'when Rails is defined' do
let!(:rails_class) do
stub_const 'Rails', Class.new
end

let(:serializer_class) do
Class.new do
include BrightSerializer::Serializer
end
end

it 'have Extension::Cache in ancestors' do
expect(serializer_class.ancestors).include? described_class
end
end
end
let(:serializer_class) do
Class.new do
include BrightSerializer::Serializer
cache_options cache_length: 1, race_condition_ttl: 1
end
end

describe '#serialize' do
subject { instance.serialize(object) }
let(:respond_to_cache_key) do
Class.new do
def cache_key; end
end
end

it '' do

end
end
it 'have Extension::Cache in ancestors' do
expect(Rails).to receive_message_chain(:cache, :fetch)
expect(serializer_class.ancestors).to include described_class
serializer_class.new(respond_to_cache_key.new).serializable_hash
end
end
end
end
26 changes: 26 additions & 0 deletions spec/lib/bright_serializer/extensions/instrumentation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require 'bright_serializer/extensions/instrumentation'

RSpec.describe BrightSerializer::Extensions::Instrumentation do
let(:instance) { described_class.new }

describe 'using extension' do
context 'when Rails is defined' do
let(:rails_class) do
stub_const 'ActiveSupport', Class.new
end

let(:serializer_class) do
rails_class
Class.new do
include BrightSerializer::Serializer
end
end

it 'have Extension::Instrumentation in ancestors' do
expect(serializer_class.ancestors).to include described_class
end
end
end
end
4 changes: 2 additions & 2 deletions spec/lib/bright_serializer/serializer_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
end

it 'serialize without name' do
expect(instance.to_hash).to eq(result)
expect(instance.serializable_hash).to eq(result)
end

context 'when condition with params' do
Expand All @@ -41,7 +41,7 @@
let(:instance) { serializer_class.new(user, params: 0) }

it 'serialize without name' do
expect(instance.to_hash).to eq(result)
expect(instance.serializable_hash).to eq(result)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/bright_serializer/serializer_fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
end

it 'serialize without name' do
expect(instance.to_hash).to eq(result)
expect(instance.serializable_hash).to eq(result)
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/bright_serializer/serializer_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
end

it 'serialize params' do
expect(instance.to_hash).to eq(result)
expect(instance.serializable_hash).to eq(result)
end
end
end
Loading

0 comments on commit 0b135bc

Please sign in to comment.