From 865464fdff184de48fa600af33042c2754cc64b4 Mon Sep 17 00:00:00 2001 From: Paolo Zaccagnini Date: Wed, 27 Apr 2016 10:58:05 +0200 Subject: [PATCH 1/3] Drop polyfills --- lib/hawk.rb | 8 +++++- lib/hawk/http.rb | 1 - lib/hawk/http/caching.rb | 2 -- lib/hawk/model/association.rb | 1 - lib/hawk/model/finder.rb | 1 - lib/hawk/model/lookup.rb | 1 - lib/hawk/model/proxy.rb | 2 -- lib/hawk/model/querying.rb | 1 - lib/hawk/polyfills/hash.rb | 48 ----------------------------------- lib/hawk/polyfills/module.rb | 42 ------------------------------ lib/hawk/polyfills/string.rb | 35 ------------------------- 11 files changed, 7 insertions(+), 135 deletions(-) delete mode 100644 lib/hawk/polyfills/hash.rb delete mode 100644 lib/hawk/polyfills/module.rb delete mode 100644 lib/hawk/polyfills/string.rb diff --git a/lib/hawk.rb b/lib/hawk.rb index f72dae8..db9d949 100644 --- a/lib/hawk.rb +++ b/lib/hawk.rb @@ -1,8 +1,14 @@ ## # Hawk entry point. # +# + +require 'active_support/core_ext/array/wrap' +require 'active_support/core_ext/hash/deep_merge' +require 'active_support/core_ext/module/introspection' +require 'active_support/core_ext/string/inflections' + require 'hawk/version' -require 'hawk/polyfills' # DIH require 'hawk/error' require 'hawk/http' diff --git a/lib/hawk/http.rb b/lib/hawk/http.rb index d51bce0..bb69a62 100644 --- a/lib/hawk/http.rb +++ b/lib/hawk/http.rb @@ -11,7 +11,6 @@ module Hawk # Represent an HTTP connector, to be linked to a {Model}. # class HTTP - using Hawk::Polyfills # Hash#deep_merge prepend Caching include Instrumentation diff --git a/lib/hawk/http/caching.rb b/lib/hawk/http/caching.rb index ced47bb..8bdd9b2 100644 --- a/lib/hawk/http/caching.rb +++ b/lib/hawk/http/caching.rb @@ -1,11 +1,9 @@ require 'dalli' -require 'active_support/core_ext/array/wrap' module Hawk class HTTP module Caching - using Hawk::Polyfills # Hash#deep_merge DEFAULTS = { server: 'localhost:11211', diff --git a/lib/hawk/model/association.rb b/lib/hawk/model/association.rb index 4cd56df..1305fe9 100644 --- a/lib/hawk/model/association.rb +++ b/lib/hawk/model/association.rb @@ -2,7 +2,6 @@ module Hawk module Model module Association - using Hawk::Polyfills # Hash#deep_merge, Module#parent, String#demodulize, String#underscore, String#camelize, String#singularize # Initialize the associations registry # diff --git a/lib/hawk/model/finder.rb b/lib/hawk/model/finder.rb index b1fb041..6a042f8 100644 --- a/lib/hawk/model/finder.rb +++ b/lib/hawk/model/finder.rb @@ -2,7 +2,6 @@ module Hawk module Model module Finder - using Hawk::Polyfills # Hash#deep_merge, String#underscore, String#demodulize, String#pluralize def self.included(base) base.extend ClassMethods diff --git a/lib/hawk/model/lookup.rb b/lib/hawk/model/lookup.rb index eb062ef..a79af4b 100644 --- a/lib/hawk/model/lookup.rb +++ b/lib/hawk/model/lookup.rb @@ -2,7 +2,6 @@ module Hawk module Model module Lookup - using Hawk::Polyfills # Module#parent def self.included(base) base.extend ClassMethods diff --git a/lib/hawk/model/proxy.rb b/lib/hawk/model/proxy.rb index d1e6873..2607c10 100644 --- a/lib/hawk/model/proxy.rb +++ b/lib/hawk/model/proxy.rb @@ -4,8 +4,6 @@ module Model class Proxy include Enumerable - using Hawk::Polyfills # Hash#deep_merge, Module#parents - def initialize(klass, params) @klass = klass @params = params diff --git a/lib/hawk/model/querying.rb b/lib/hawk/model/querying.rb index 71223e3..88591e3 100644 --- a/lib/hawk/model/querying.rb +++ b/lib/hawk/model/querying.rb @@ -2,7 +2,6 @@ module Hawk module Model module Querying - using Hawk::Polyfills # Hash#deep_merge def self.included(base) base.extend ClassMethods diff --git a/lib/hawk/polyfills/hash.rb b/lib/hawk/polyfills/hash.rb deleted file mode 100644 index 6fc8a95..0000000 --- a/lib/hawk/polyfills/hash.rb +++ /dev/null @@ -1,48 +0,0 @@ -module Hawk - module Polyfills - - polyfill Hash, :deep_merge do - using Hawk::Polyfills - - # Stolen from ActiveSupport. - # - # Returns a new hash with +self+ and +other_hash+ merged recursively. - # - # h1 = { a: true, b: { c: [1, 2, 3] } } - # h2 = { a: false, b: { x: [3, 4, 5] } } - # - # h1.deep_merge(h2) # => { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } } - # - # Like with Hash#merge in the standard library, a block can be provided - # to merge values: - # - # h1 = { a: 100, b: 200, c: { c1: 100 } } - # h2 = { b: 250, c: { c1: 200 } } - # h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val } - # # => { a: 100, b: 450, c: { c1: 300 } } - def deep_merge(other_hash, &block) - dup.deep_merge!(other_hash, &block) - end - - # Same as +deep_merge+, but modifies +self+. - def deep_merge!(other_hash, &block) - other_hash.each_pair do |current_key, other_value| - this_value = self[current_key] - - self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash) - this_value.deep_merge(other_value, &block) - else - if block_given? && key?(current_key) - block.call(current_key, this_value, other_value) - else - other_value - end - end - end - - self - end - end - - end -end diff --git a/lib/hawk/polyfills/module.rb b/lib/hawk/polyfills/module.rb deleted file mode 100644 index f8fb73c..0000000 --- a/lib/hawk/polyfills/module.rb +++ /dev/null @@ -1,42 +0,0 @@ -require 'English' - -module Hawk - module Polyfills - - polyfill Module, :parent do - using Hawk::Polyfills - - # Returns the name of the module containing this one. - # - # M::N.parent_name # => "M" - def parent_name - @_parent_name ||= self.name =~ /::[^:]+\Z/ ? $PREMATCH : nil - end - - def parent - parent_name ? constantize(parent_name) : Object - end - - def constantize(string) - string.split('::').inject(Object) {|ns, c| ns.const_get(c)} - end - - def parents - parents = [] - - if parent_name - parts = parent_name.split('::') - - until parts.empty? - parents << constantize(parts * '::') - parts.pop - end - end - - parents << Object unless parents.include? Object - parents - end - end - - end -end diff --git a/lib/hawk/polyfills/string.rb b/lib/hawk/polyfills/string.rb deleted file mode 100644 index 24c19fc..0000000 --- a/lib/hawk/polyfills/string.rb +++ /dev/null @@ -1,35 +0,0 @@ -module Hawk - module Polyfills - - polyfill String, :demodulize do - def demodulize - self.split('::').last || self - end - end - - polyfill String, :underscore do - def underscore - self.gsub(/(\w)([A-Z])/) { [$1, '_', $2.downcase].join }.downcase - end - end - - polyfill String, :pluralize do - def pluralize - self.sub(/y$/, 'ies').sub(/[^s]$/, '\0s') - end - end - - polyfill String, :camelize do - def camelize - self.gsub(/^(\w)|_(\w)/) { $+.upcase } - end - end - - polyfill String, :singularize do - def singularize - self.gsub(/s$/, '') # NAIVE - end - end - - end -end From 8404f996b1eb54a4253d2dbf75da61aeae4577d8 Mon Sep 17 00:00:00 2001 From: Paolo Zaccagnini Date: Wed, 27 Apr 2016 11:11:26 +0200 Subject: [PATCH 2/3] The previous pluralization wasn't optimal --- spec/basic_operations_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/basic_operations_spec.rb b/spec/basic_operations_spec.rb index 992922b..d046619 100644 --- a/spec/basic_operations_spec.rb +++ b/spec/basic_operations_spec.rb @@ -20,7 +20,7 @@ class Person < Hawk::Model::Base describe '.find(id)' do specify do - stub_request(:GET, "http://zombo.com/persons/2"). + stub_request(:GET, "http://zombo.com/people/2"). with(:headers => {'User-Agent'=>'Foobar'}). to_return(status: 200, body: person_attributes.to_json, headers: {}) @@ -33,7 +33,7 @@ class Person < Hawk::Model::Base describe '.first' do specify do - stub_request(:GET, "http://zombo.com/persons?limit=1"). + stub_request(:GET, "http://zombo.com/people?limit=1"). with(:headers => {'User-Agent'=>'Foobar'}). to_return(status: 200, body: [person_attributes].to_json, headers: {}) @@ -46,7 +46,7 @@ class Person < Hawk::Model::Base describe '.all' do specify do - stub_request(:GET, "http://zombo.com/persons"). + stub_request(:GET, "http://zombo.com/people"). with(:headers => {'User-Agent'=>'Foobar'}). to_return(status: 200, body: [person_attributes, person_attributes].to_json, headers: {}) @@ -57,7 +57,7 @@ class Person < Hawk::Model::Base describe '.where' do specify do - stub_request(:GET, "http://zombo.com/persons?name=Zelig"). + stub_request(:GET, "http://zombo.com/people?name=Zelig"). with(:headers => {'User-Agent'=>'Foobar'}). to_return(status: 200, body: [person_attributes].to_json, headers: {}) @@ -68,7 +68,7 @@ class Person < Hawk::Model::Base describe '.order' do specify do - stub_request(:GET, "http://zombo.com/persons?order=name"). + stub_request(:GET, "http://zombo.com/people?order=name"). with(:headers => {'User-Agent'=>'Foobar'}). to_return(status: 200, body: [person_attributes].to_json, headers: {}) @@ -83,7 +83,7 @@ class Person < Hawk::Model::Base end it 'allows active_record-like scopes' do - stub_request(:GET, "http://zombo.com/persons?limit=1&name=pluto"). + stub_request(:GET, "http://zombo.com/people?limit=1&name=pluto"). with(:headers => {'User-Agent'=>'Foobar'}). to_return(:status => 200, :body => [person_attributes].to_json, :headers => {}) person = Person.by_name('pluto').first From ce08d10190e9bfe0d93a4bef86348d6726e1f06e Mon Sep 17 00:00:00 2001 From: Paolo Zaccagnini Date: Wed, 27 Apr 2016 11:15:54 +0200 Subject: [PATCH 3/3] Make the ActiveSupport dependency explicit --- hawk.gemspec | 1 + lib/hawk.rb | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/hawk.gemspec b/hawk.gemspec index 147bafb..f9e8de1 100644 --- a/hawk.gemspec +++ b/hawk.gemspec @@ -25,6 +25,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'multi_json' spec.add_dependency 'dalli' spec.add_dependency 'activemodel' + spec.add_dependency 'activesupport' spec.add_development_dependency 'bundler', '~> 1.8' spec.add_development_dependency 'rake', '~> 10.0' diff --git a/lib/hawk.rb b/lib/hawk.rb index db9d949..2b86ad4 100644 --- a/lib/hawk.rb +++ b/lib/hawk.rb @@ -4,9 +4,6 @@ # require 'active_support/core_ext/array/wrap' -require 'active_support/core_ext/hash/deep_merge' -require 'active_support/core_ext/module/introspection' -require 'active_support/core_ext/string/inflections' require 'hawk/version'