From 215fb85c7fc10d8fce504e5a7f6256d8a364e16c Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Tue, 18 Aug 2015 17:32:27 -0400 Subject: [PATCH] Test caller line parsing and digesting --- lib/active_model/serializer.rb | 25 +++++++++++++++++++++++-- test/serializers/cache_test.rb | 27 ++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index d1ab351c5..1395b7781 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -12,6 +12,22 @@ class Serializer include Configuration include Associations + + # Matches + # "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `'" + # AND + # "/c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `'" + # AS + # c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb + CALLER_FILE = / + \A # start of string + \S+ # one or more non-spaces + (?= # stop previous match when + :\d+ # a colon is followed by one or more digits + :in # followed by a colon followed by in + ) + /x + class << self attr_accessor :_attributes attr_accessor :_attributes_keys @@ -29,8 +45,7 @@ def self.inherited(base) base._attributes = self._attributes.try(:dup) || [] base._attributes_keys = self._attributes_keys.try(:dup) || {} base._urls = [] - serializer_file_path = caller.first[/\A\S+(?=:\d+:in)/] - base._cache_digest = Digest::MD5.hexdigest(File.read(serializer_file_path)) + base._cache_digest = digest_caller_file(caller.first) super end @@ -161,6 +176,12 @@ def self.serializers_cache @serializers_cache ||= ThreadSafe::Cache.new end + def self.digest_caller_file(caller_line) + serializer_file_path = caller_line[CALLER_FILE] + serializer_file_contents = IO.read(serializer_file_path) + Digest::MD5.hexdigest(serializer_file_contents) + end + attr_reader :options def self.get_serializer_for(klass) diff --git a/test/serializers/cache_test.rb b/test/serializers/cache_test.rb index 60c351944..a57faa2d1 100644 --- a/test/serializers/cache_test.rb +++ b/test/serializers/cache_test.rb @@ -1,4 +1,5 @@ require 'test_helper' +require 'tempfile' module ActiveModel class Serializer class CacheTest < Minitest::Test @@ -125,10 +126,34 @@ def test_uses_file_digest_in_cache_key assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest)) end - def _cache_digest_definition + def test_cache_digest_definition assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest) end + def test_serializer_file_path_on_nix + path = "/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb" + caller_line = "#{path}:1:in `'" + assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path + end + + def test_serializer_file_path_on_windows + path = "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb" + caller_line = "#{path}:1:in `'" + assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path + end + + def test_digest_caller_file + contents = "puts 'AMS rocks'!" + file = Tempfile.new("some_ruby.rb") + file.write(contents) + path = file.path + caller_line = "#{path}:1:in `'" + file.close + assert_equal ActiveModel::Serializer.digest_caller_file(caller_line), Digest::MD5.hexdigest(contents) + ensure + file.unlink + end + private def render_object_with_cache(obj) ActiveModel::SerializableResource.new(obj).serializable_hash