look_up_table 0.1.0.rc9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,3 +1,31 @@
1
1
  = LookUpTable
2
+ *STILL IN DEVELOPMENT!*::
3
+
4
+ == Requirements:
5
+ * Rails 3.x
6
+ * Ruby 1.8.7, 1.9.2, 1.9.3
7
+
8
+ == Install
9
+ Put following to your gemfile:
10
+ gem 'look_up_table'
11
+
12
+ == Usage
13
+ class Foobar < ActiveRecord::Base
14
+ # Allows: Foobar.lut :id
15
+ look_up_table :id
16
+
17
+ # Allows: Foobar.lut :foo
18
+ look_up_table :foo
19
+
20
+ # Allows: Foobar.lut :bar
21
+ look_up_table :bar
22
+
23
+ # Allows: Foobar.lut :foobar
24
+ look_up_table :foobar
25
+ end
26
+
27
+
28
+ == TODOs
29
+ * Rename gem to 'rails-look_up_table'
30
+ * More DOCs
2
31
 
3
- This project rocks and uses MIT-LICENSE.
@@ -3,15 +3,41 @@ require 'look_up_table/base'
3
3
  require 'look_up_table/cache'
4
4
  require 'look_up_table/method_missing'
5
5
  require 'look_up_table/no_cache'
6
+ require 'look_up_table/support'
6
7
 
8
+ # CHECK: fail if Numbers as keys?
9
+ # TODO: Doc :look_up_table => options
7
10
  module LookUpTable
8
11
  module ClassMethods
9
- # Defining a LookUpTable
10
- # TODO: Usage
12
+
13
+ # == Defining LookUpTables
14
+ #
15
+ # # Sample class:
16
+ # Foobar(id: integer, foo: string, bar: integer)
17
+ #
18
+ # === Simplest way to define a LookUpTable:
19
+ # look_up_table :id
20
+ # look_up_table :foo
21
+ # look_up_table :bar
22
+ #
23
+ # === Add some options to your LookUpTable:
24
+ # look_up_table :foo, :batch_size => 5000, :where => "id > 10000"
25
+ #
26
+ # === Pass a block to define the LUT manually
27
+ # look_up_table :foo do |lut, foobar|
28
+ # lut[foobar.foo] = foobar.id
29
+ # end
30
+ #
31
+ # === Turn off AutoFinder and completly define the whole LUT yourself:
32
+ # look_up_table :foo, :sql_mode => false do |lut|
33
+ # Foobar.where("id > 10000").each do |foobar|
34
+ # lut[foobar.foo] = foobar.id
35
+ # end
36
+ # end
11
37
  def look_up_table(lut_key, options = {}, &block)
12
38
  options = {
13
39
  :batch_size => 10000,
14
- :prefix => self.name,
40
+ :prefix => "#{self.name}/",
15
41
  :read_on_init => false,
16
42
  :use_cache => true,
17
43
  :sql_mode => true,
@@ -24,13 +50,64 @@ module LookUpTable
24
50
  self.lut(lut_key) if options[:read_on_init]
25
51
  end
26
52
 
27
- # Call to a LookUpTable.
53
+ # == Calling LookUpTables
54
+ #
55
+ # === Call without any params
56
+ # * Returns: All LUTs defined within Foobar
57
+ # Foobar.lut
58
+ # =>
59
+ # {
60
+ # :foo => { :a => 1 },
61
+ # :bar => { :b => 2 },
62
+ # :foobar => { :c => 3, :d => 4, :e => 5 }
63
+ # }
64
+ #
65
+ # === Call with :lut_key:
66
+ # * Returns: Hash representing LUT defined by :lut_key
67
+ # Foobar.lut :foo
68
+ # => { :a => 1 }
69
+ #
70
+ # === Call with array of :lut_keys
71
+ # * Returns: Hash representing LUT defined with :lut_key in given Array
72
+ # Foobar.lut [:foo, :bar]
73
+ # =>
74
+ # {
75
+ # :foo => { :a => 1 },
76
+ # :bar => { :b => 2 }
77
+ # }
78
+ #
79
+ # === Call with Call with :lut_key and :lut_item_key
80
+ # * Returns: Value in LUT defined by :lut_key and :lut_item_key
81
+ # Foobar.lut :foo, "foobar"
82
+ # => 1
83
+ # # So we've got a Foobar with :foo => "foobar", its ID is '1'
84
+ #
85
+ # === Call with Call with :lut_key and :lut_item_key as Array
86
+ # * Returns: Hash representing LUT defined by :lut_key with
87
+ # :lut_item_keys in Array
88
+ # Foobar.lut :foobar, ["foo", "bar", "oof"]
89
+ # =>
90
+ # {
91
+ # "foo" => 3,
92
+ # "bar" => 4,
93
+ # "oof" => nil
94
+ # }
95
+ # # So we got Foobars with ID '3' and '4'
96
+ # # and no Foobar defined by :foobar => :oof
97
+ #
98
+ # === Call with :lut_key as a Hash
99
+ # * Returns: Hash representing LUTs given by keys of passed Hash.
100
+ # - If given value of Hash-Item is nil, will get whole LUT.
101
+ # - If given value is String or Symbol, will get value of LUT.
102
+ # - If given value is Array, will get values of entries.
28
103
  # * Example:
29
- # * Tag.lut (Returns all LookUpTables)
30
- # * Tag.lut :name (Returns LookUpTable given by :name)
31
- # * Tag.lut(:name, "Berlin") (Returns Value of LookUpTable named :name with :key "Berlin")
32
- # * Tag.lut({:name => "Berlin", :title => "Berlin"}) TODO: returns hash
33
- # TODO: TestCases
104
+ # Foobar.lut { :foo => :a, :bar => nil, :foobar => [:c, :d] }
105
+ # =>
106
+ # {
107
+ # :foo => 1,
108
+ # :bar => { :b => 2 },
109
+ # :foobar => { :c => 3, :d => 4 }
110
+ # }
34
111
  def lut(lut_key = nil, lut_item_key = nil)
35
112
  @lut ||= {}
36
113
 
@@ -40,24 +117,19 @@ module LookUpTable
40
117
  return hash
41
118
  end
42
119
 
43
- if (lut_key.respond_to?(:keys))
44
- hash = {}
45
- lut_key.each { |k,v| hash[k.intern] = self.lut(k,v) }
46
- return hash
47
- end
120
+ @lut[lut_key.intern] ||= lut_read(lut_key) || {} if lut_key.respond_to?(:intern)
48
121
 
49
- lut = @lut[lut_key.to_sym] ||= lut_read(lut_key) || {}
50
- lut_item_key ? lut[lut_item_key] : lut
122
+ self.lut_deep_hash_call(:lut, @lut, lut_key, lut_item_key)
51
123
  end
52
124
 
53
125
  # Reset complete lut if name is omitted, resets given lut otherwise.
54
126
  # HACK: not cool do access and define @lut here
55
- def lut_reset(name = nil)
127
+ def lut_reset(lut_key = nil)
56
128
  @lut ||= {}
57
129
 
58
- if name
59
- @lut[name.to_sym] = nil
60
- lut_write_cache_item(name, 0, nil) unless lut_options[:skip_memcached] # CHECK: options call w/o name?
130
+ if lut_key
131
+ @lut[lut_key.intern] = nil
132
+ lut_write_cache_item(lut_key, 0, nil) unless lut_options[:skip_memcached] # CHECK: options call w/o name?
61
133
  else
62
134
  lut_keys.each { |k| lut_reset(k) }
63
135
  @lut = {}
@@ -65,10 +137,10 @@ module LookUpTable
65
137
  end
66
138
 
67
139
  # Reading LUT and writing cache again
68
- def lut_reload(name = nil)
69
- if name
70
- lut_reset(name)
71
- lut(name)
140
+ def lut_reload(lut_key = nil)
141
+ if lut_key
142
+ lut_reset(lut_key)
143
+ lut(lut_key)
72
144
  else
73
145
  lut_keys.each { |k| lut_reload(k) }
74
146
  end
@@ -78,9 +150,12 @@ module LookUpTable
78
150
 
79
151
  # Init complete LUT with all keys define.
80
152
  # But won't rewrite cache if allready written!
81
- def lut_init(name = nil)
82
- if name
83
- lut(name)
153
+ # * Returns: Foobar.lut_keys
154
+ # Foobar.lut_init
155
+ # => [:id, :foo, :bar, :foobar]
156
+ def lut_init(lut_key = nil)
157
+ if lut_key
158
+ lut(lut_key)
84
159
  else
85
160
  lut_keys.each { |k| lut_init(k) }
86
161
  end
@@ -88,39 +163,29 @@ module LookUpTable
88
163
  lut_keys
89
164
  end
90
165
 
91
- # Returns keys of LookUpTables defined
166
+ # Returns: Keys of LookUpTables defined
167
+ # Foobar.lut_keys
168
+ # => [:id, :foo, :bar, :foobar]
92
169
  def lut_keys
93
170
  lut_options.keys
94
171
  end
95
172
 
96
- # Usage
97
- # * Klass.lut_options
98
- # * Klass.lut_options(:lut_key)
99
- # * Klass.lut_options(:lut_key, :option_key)
100
- # * Klass.lut_options(:lut_key => :option_key)
101
- # * Klass.lut_options(:lut_key => [:option_key, :second_option_key])
102
- # TODO: test_cases
103
- # CHECK: some sweeter implementation?
104
- def lut_options(name = nil)#, option_key = nil)
173
+ # Returns: Options defined
174
+ # * Accept same params as: Foobar.lut
175
+ # Foobar.lut_options :foobar
176
+ # =>
177
+ # {
178
+ # :batch_size=>10000,
179
+ # :prefix=>"Foobar/",
180
+ # :read_on_init=>false,
181
+ # :use_cache=>true,
182
+ # :sql_mode=>true,
183
+ # :where=>nil
184
+ # }
185
+ def lut_options(lut_key = nil, option_key = nil)
105
186
  @lut_options ||= {}
106
187
 
107
- # if name && name.respond_to?(:keys)
108
- # if name[:lut_key] && name[:lut_key].respond_to?(:values)
109
- # return name[:lut_key].each.inject([]) do |arr, el|
110
- # arr << @lut_options[name.intern][el.intern]
111
- # end
112
- # elsif name[:lut_key]
113
- # return @lut_options[name.intern][option_key.intern]
114
- # else
115
- # return @lut_options[name.intern]
116
- # end
117
- # elsif name
118
- # (option_key) ? @lut_options[name.intern][option_key.intern] : @lut_options[name.intern]
119
- # else
120
- # return @lut_options
121
- # end
122
-
123
- (name) ? @lut_options[name.to_sym] : @lut_options
188
+ self.lut_deep_hash_call(:lut_options, @lut_options, lut_key, option_key)
124
189
  end
125
190
 
126
191
  protected
@@ -0,0 +1,39 @@
1
+
2
+ module LookUpTable
3
+ module ClassMethods
4
+
5
+ protected
6
+
7
+ def lut_deep_hash_call(method, deep_hash, key, item_key)
8
+ hash = deep_hash ||= {}
9
+
10
+ # Passed a Hash as key
11
+ if key.respond_to?(:keys)
12
+ h = {}
13
+ key.each { |k,v| h[k.intern] = self.send(method, k, v) }
14
+ return h
15
+ end
16
+
17
+ # Passed a Array or Set as key
18
+ if key.respond_to?(:entries)
19
+ h = {}
20
+ key.entries.each { |e| h[e.intern] = self.send(method, e) }
21
+ return h
22
+ end
23
+
24
+ # Passed a Array or Set as item_key
25
+ if item_key.respond_to?(:entries) && key
26
+ h = {}
27
+ item_key.entries.each { |e| h[e] = self.send(method, key, e) }
28
+ return h
29
+ end
30
+
31
+ hash = hash[key.intern] if key.respond_to?(:intern)
32
+ hash = hash[item_key] if item_key
33
+
34
+ return hash
35
+ end
36
+
37
+ end
38
+ end
39
+
@@ -1,4 +1,4 @@
1
1
  module LookUpTable
2
- VERSION = "0.1.0.rc9"
2
+ VERSION = "0.1.0"
3
3
  end
4
4
 
@@ -1,13 +1,9 @@
1
1
  class Foobar < ActiveRecord::Base
2
2
 
3
- look_up_table :id, :batch_size => 500000
3
+ look_up_table :id
4
4
  look_up_table :foo
5
- look_up_table :bar, :sql_mode => false do |lut|
6
- lut[1] = []
7
- 500000.times { |key| lut[1] << Random.rand }
8
- lut[1].sort!
9
- end
10
- #look_up_table :foobar
5
+ look_up_table :bar
6
+ look_up_table :foobar
11
7
 
12
8
  end
13
9
 
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: look_up_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.rc9
5
- prerelease: 6
4
+ version: 0.1.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Patrick Helm
@@ -13,7 +13,7 @@ date: 2011-11-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &82859110 !ruby/object:Gem::Requirement
16
+ requirement: &72080760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.1.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *82859110
24
+ version_requirements: *72080760
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3
27
- requirement: &82858810 !ruby/object:Gem::Requirement
27
+ requirement: &72080410 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *82858810
35
+ version_requirements: *72080410
36
36
  description: A simple LookUpTable to cache large (!) and static(!) data
37
37
  email:
38
38
  - ph@werbeboten.de
@@ -43,6 +43,7 @@ extra_rdoc_files: []
43
43
  files:
44
44
  - lib/look_up_table.rb
45
45
  - lib/look_up_table/version.rb
46
+ - lib/look_up_table/support.rb
46
47
  - lib/look_up_table/base.rb
47
48
  - lib/look_up_table/no_cache.rb
48
49
  - lib/look_up_table/cache.rb
@@ -196,13 +197,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
196
197
  version: '0'
197
198
  segments:
198
199
  - 0
199
- hash: -655178989
200
+ hash: 965549137
200
201
  required_rubygems_version: !ruby/object:Gem::Requirement
201
202
  none: false
202
203
  requirements:
203
- - - ! '>'
204
+ - - ! '>='
204
205
  - !ruby/object:Gem::Version
205
- version: 1.3.1
206
+ version: '0'
207
+ segments:
208
+ - 0
209
+ hash: 965549137
206
210
  requirements: []
207
211
  rubyforge_project:
208
212
  rubygems_version: 1.8.10