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 24e6e8f
Show file tree
Hide file tree
Showing 12 changed files with 87 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
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ PLATFORMS
ruby

DEPENDENCIES
bright_serializer!
bundler (~> 2)
faker
bright_serializer!
oj
rake (~> 10.0)
rspec (~> 3.0)
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
12 changes: 6 additions & 6 deletions spec/lib/bright_serializer/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
}
end

describe 'serialize' do
describe 'serializable_hash' do
it 'serialize to hash' do
expect(instance.to_hash).to eq(result)
expect(instance.serializable_hash).to eq(result)
end

it 'serialize to json' do
expect(instance.to_json).to eq(result.to_json)
it 'serialize to serialized_json' do
expect(instance.serialized_json).to eq(result.to_json)
end
end

Expand All @@ -49,7 +49,7 @@
end

it 'serialize an array of hash' do
expect(instance.to_hash).to eq(result)
expect(instance.serializable_hash).to eq(result)
end
end
end
Expand Down Expand Up @@ -80,6 +80,6 @@
}
end

it { expect(instance.to_hash).to eq(result) }
it { expect(instance.serializable_hash).to eq(result) }
end
end
8 changes: 4 additions & 4 deletions spec/lib/bright_serializer/serializer_transform_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
end

it 'serialize with camel_lower key' do
expect(instance.to_hash).to eq(result)
expect(instance.serializable_hash).to eq(result)
end
end

Expand Down Expand Up @@ -57,7 +57,7 @@
end

it 'serialize with camel key' do
expect(instance.to_hash).to eq(result)
expect(instance.serializable_hash).to eq(result)
end
end

Expand Down Expand Up @@ -85,7 +85,7 @@
end

it 'serialize with dash key' do
expect(instance.to_hash).to eq(result)
expect(instance.serializable_hash).to eq(result)
end
end

Expand Down Expand Up @@ -113,7 +113,7 @@
end

it 'serialize with underscore key' do
expect(instance.to_hash).to eq(result)
expect(instance.serializable_hash).to eq(result)
end
end
end

0 comments on commit 24e6e8f

Please sign in to comment.