diff options
author | Daniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org> | 2015-08-31 08:22:13 -0400 |
---|---|---|
committer | Daniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org> | 2015-08-31 08:22:13 -0400 |
commit | 11c0ac707f908c19dda72e6e863b06fb069f06b5 (patch) | |
tree | 93e3f0b4ad17e9c6461f2022d1be8e3a93870036 | |
parent | 8841996ff4d3099eb23fca989a40cc44ac0180b0 (diff) | |
parent | 816be07753285c1af4c4fa1736dccc998bbdc59e (diff) | |
download | hashie-11c0ac707f908c19dda72e6e863b06fb069f06b5.tar.gz |
Merge pull request #308 from marshall-lee/mash_micro_optimize
Some micro optimizations to Hashie::Mash.
-rw-r--r-- | lib/hashie/mash.rb | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index 430e615..8bd040f 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -58,7 +58,6 @@ module Hashie include Hashie::Extensions::PrettyInspect ALLOWED_SUFFIXES = %w(? ! = _) - SUFFIXES_PARSER = /(.*?)([#{ALLOWED_SUFFIXES.join}]?)$/ def self.load(path, options = {}) @_mashes ||= new @@ -211,10 +210,9 @@ module Hashie def respond_to_missing?(method_name, *args) return true if key?(method_name) - _, suffix = method_suffix(method_name) - case suffix - when '=', '?', '!', '_' - return true + suffix = method_suffix(method_name) + if suffix + true else super end @@ -227,15 +225,15 @@ module Hashie def method_missing(method_name, *args, &blk) return self.[](method_name, &blk) if key?(method_name) - name, suffix = method_suffix(method_name) + name, suffix = method_name_and_suffix(method_name) case suffix - when '=' + when '='.freeze assign_property(name, args.first) - when '?' + when '?'.freeze !!self[name] - when '!' + when '!'.freeze initializing_reader(name) - when '_' + when '_'.freeze underbang_reader(name) else self[method_name] @@ -254,9 +252,18 @@ module Hashie protected + def method_name_and_suffix(method_name) + method_name = method_name.to_s + if method_name.end_with?(*ALLOWED_SUFFIXES) + [method_name[0..-2], method_name[-1]] + else + [method_name[0..-1], nil] + end + end + def method_suffix(method_name) - match = method_name.to_s.match(SUFFIXES_PARSER) - [match[1], match[2]] + method_name = method_name.to_s + method_name[-1] if method_name.end_with?(*ALLOWED_SUFFIXES) end def convert_key(key) #:nodoc: |