dnz-client 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,11 @@
1
+ === 0.0.4 2009-09-14
2
+
3
+ * Bring memoizable from ActiveSupport into DNZ namespace.
4
+
5
+ === 0.0.3 2009-09-13
6
+
7
+ * Support for filtering by facet.
8
+
1
9
  === 0.0.1 2009-07-14
2
10
 
3
11
  * 1 major enhancement:
data/Manifest.txt CHANGED
@@ -11,6 +11,7 @@ lib/dnz/facet.rb
11
11
  lib/dnz/facet_array.rb
12
12
  lib/dnz/result.rb
13
13
  lib/dnz/search.rb
14
+ lib/dnz/memoizable.rb
14
15
  lib/dnz/error/invalid_api_key.rb
15
16
  script/console
16
17
  script/destroy
@@ -0,0 +1,100 @@
1
+ module DNZ
2
+ module Memoizable
3
+ def self.memoized_ivar_for(symbol)
4
+ "@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
5
+ end
6
+
7
+ module InstanceMethods
8
+ def self.included(base)
9
+ base.class_eval do
10
+ unless base.method_defined?(:freeze_without_memoizable)
11
+ alias_method_chain :freeze, :memoizable
12
+ end
13
+ end
14
+ end
15
+
16
+ def freeze_with_memoizable
17
+ memoize_all unless frozen?
18
+ freeze_without_memoizable
19
+ end
20
+
21
+ def memoize_all
22
+ prime_cache ".*"
23
+ end
24
+
25
+ def unmemoize_all
26
+ flush_cache ".*"
27
+ end
28
+
29
+ def prime_cache(*syms)
30
+ syms.each do |sym|
31
+ methods.each do |m|
32
+ if m.to_s =~ /^_unmemoized_(#{sym})/
33
+ if method(m).arity == 0
34
+ __send__($1)
35
+ else
36
+ ivar = DNZ::Memoizable.memoized_ivar_for($1)
37
+ instance_variable_set(ivar, {})
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ def flush_cache(*syms, &block)
45
+ syms.each do |sym|
46
+ (methods + private_methods + protected_methods).each do |m|
47
+ if m.to_s =~ /^_unmemoized_(#{sym})/
48
+ ivar = DNZ::Memoizable.memoized_ivar_for($1)
49
+ instance_variable_get(ivar).clear if instance_variable_defined?(ivar)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ def memoize(*symbols)
57
+ symbols.each do |symbol|
58
+ original_method = :"_unmemoized_#{symbol}"
59
+ memoized_ivar = DNZ::Memoizable.memoized_ivar_for(symbol)
60
+
61
+ class_eval <<-EOS, __FILE__, __LINE__
62
+ include InstanceMethods # include InstanceMethods
63
+ #
64
+ if method_defined?(:#{original_method}) # if method_defined?(:_unmemoized_mime_type)
65
+ raise "Already memoized #{symbol}" # raise "Already memoized mime_type"
66
+ end # end
67
+ alias #{original_method} #{symbol} # alias _unmemoized_mime_type mime_type
68
+ #
69
+ if instance_method(:#{symbol}).arity == 0 # if instance_method(:mime_type).arity == 0
70
+ def #{symbol}(reload = false) # def mime_type(reload = false)
71
+ if reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty? # if reload || !defined?(@_memoized_mime_type) || @_memoized_mime_type.empty?
72
+ #{memoized_ivar} = [#{original_method}.freeze] # @_memoized_mime_type = [_unmemoized_mime_type.freeze]
73
+ end # end
74
+ #{memoized_ivar}[0] # @_memoized_mime_type[0]
75
+ end # end
76
+ else # else
77
+ def #{symbol}(*args) # def mime_type(*args)
78
+ #{memoized_ivar} ||= {} unless frozen? # @_memoized_mime_type ||= {} unless frozen?
79
+ reload = args.pop if args.last == true || args.last == :reload # reload = args.pop if args.last == true || args.last == :reload
80
+ #
81
+ if defined?(#{memoized_ivar}) && #{memoized_ivar} # if defined?(@_memoized_mime_type) && @_memoized_mime_type
82
+ if !reload && #{memoized_ivar}.has_key?(args) # if !reload && @_memoized_mime_type.has_key?(args)
83
+ #{memoized_ivar}[args] # @_memoized_mime_type[args]
84
+ elsif #{memoized_ivar} # elsif @_memoized_mime_type
85
+ #{memoized_ivar}[args] = #{original_method}(*args).freeze # @_memoized_mime_type[args] = _unmemoized_mime_type(*args).freeze
86
+ end # end
87
+ else # else
88
+ #{original_method}(*args) # _unmemoized_mime_type(*args)
89
+ end # end
90
+ end # end
91
+ end # end
92
+ #
93
+ if private_method_defined?(#{original_method.inspect}) # if private_method_defined?(:_unmemoized_mime_type)
94
+ private #{symbol.inspect} # private :mime_type
95
+ end # end
96
+ EOS
97
+ end
98
+ end
99
+ end
100
+ end
data/lib/dnz/search.rb CHANGED
@@ -2,6 +2,7 @@ require 'nokogiri'
2
2
  require 'dnz/result'
3
3
  require 'dnz/facet_array'
4
4
  require 'dnz/facet'
5
+ require 'dnz/memoizable'
5
6
 
6
7
  begin
7
8
  gem 'mislav-will_paginate' rescue nil
@@ -19,7 +20,7 @@ module DNZ
19
20
  class Search
20
21
  attr_reader :result_count
21
22
 
22
- extend ActiveSupport::Memoizable
23
+ extend DNZ::Memoizable
23
24
 
24
25
  # Constructor for Search class. Do not call this directly, instead use the <tt>Client.search</tt> method.
25
26
  def initialize(client, search_options)
data/lib/dnz.rb CHANGED
@@ -4,5 +4,5 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'dnz/client'
5
5
 
6
6
  module Dnz
7
- VERSION = '0.0.3'
7
+ VERSION = '0.0.4'
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dnz-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Wells
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-12 00:00:00 +13:00
12
+ date: 2009-10-14 00:00:00 +13:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -68,6 +68,7 @@ files:
68
68
  - lib/dnz/facet_array.rb
69
69
  - lib/dnz/result.rb
70
70
  - lib/dnz/search.rb
71
+ - lib/dnz/memoizable.rb
71
72
  - lib/dnz/error/invalid_api_key.rb
72
73
  - script/console
73
74
  - script/destroy