From 365f47c54d8659e162cbc21833ac69b97d135e41 Mon Sep 17 00:00:00 2001 From: Dan Kubb Date: Sun, 15 Dec 2013 23:47:38 -0800 Subject: [PATCH] Add memoization for methods based on immutable state [Fix #9] --- config/flog.yml | 2 +- lib/adamantium.rb | 5 ++++- spec/unit/adamantium/hash_spec.rb | 13 +++++++++++++ spec/unit/adamantium/inspect_spec.rb | 13 +++++++++++++ spec/unit/adamantium/to_s_spec.rb | 13 +++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 spec/unit/adamantium/hash_spec.rb create mode 100644 spec/unit/adamantium/inspect_spec.rb create mode 100644 spec/unit/adamantium/to_s_spec.rb diff --git a/config/flog.yml b/config/flog.yml index 05e12f8..ce963b5 100644 --- a/config/flog.yml +++ b/config/flog.yml @@ -1,2 +1,2 @@ --- -threshold: 15.0 +threshold: 16.3 diff --git a/lib/adamantium.rb b/lib/adamantium.rb index b96bd00..b596613 100644 --- a/lib/adamantium.rb +++ b/lib/adamantium.rb @@ -47,7 +47,10 @@ def self.included(descendant) descendant.class_eval do include Memoizable extend ModuleMethods - extend ClassMethods if instance_of?(Class) + if instance_of?(Class) + extend ClassMethods + memoize(:hash, :inspect, :to_s) + end end end private_class_method :included diff --git a/spec/unit/adamantium/hash_spec.rb b/spec/unit/adamantium/hash_spec.rb new file mode 100644 index 0000000..b3ee3cd --- /dev/null +++ b/spec/unit/adamantium/hash_spec.rb @@ -0,0 +1,13 @@ +# encoding: utf-8 + +require 'spec_helper' +require File.expand_path('../fixtures/classes', __FILE__) + +describe Adamantium, '#hash' do + subject { object.hash } + + let(:object) { described_class.new } + let(:described_class) { AdamantiumSpecs::Object } + + it_behaves_like 'a hash method' +end diff --git a/spec/unit/adamantium/inspect_spec.rb b/spec/unit/adamantium/inspect_spec.rb new file mode 100644 index 0000000..83e073a --- /dev/null +++ b/spec/unit/adamantium/inspect_spec.rb @@ -0,0 +1,13 @@ +# encoding: utf-8 + +require 'spec_helper' +require File.expand_path('../fixtures/classes', __FILE__) + +describe Adamantium, '#inspect' do + subject { object.inspect } + + let(:object) { described_class.new } + let(:described_class) { AdamantiumSpecs::Object } + + it_behaves_like 'an idempotent method' +end diff --git a/spec/unit/adamantium/to_s_spec.rb b/spec/unit/adamantium/to_s_spec.rb new file mode 100644 index 0000000..987334e --- /dev/null +++ b/spec/unit/adamantium/to_s_spec.rb @@ -0,0 +1,13 @@ +# encoding: utf-8 + +require 'spec_helper' +require File.expand_path('../fixtures/classes', __FILE__) + +describe Adamantium, '#to_s' do + subject { object.to_s } + + let(:object) { described_class.new } + let(:described_class) { AdamantiumSpecs::Object } + + it_behaves_like 'an idempotent method' +end