offline_lookup 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +61 -0
  3. data/lib/offline_lookup.rb +6 -55
  4. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2db35bb5e31d4cfde77e91c6f67988c5cdf6dfe9
4
- data.tar.gz: 7743e8b15d2f1c93f121b99f5cb9cca0d302a2ce
3
+ metadata.gz: 887ba1b39a9ba252abb2a69d9ac652a8f9b3ded8
4
+ data.tar.gz: 8b0d2fc3a6d65b605fd951255e4a796c02dd58f3
5
5
  SHA512:
6
- metadata.gz: 0c1f72f28029f3d4e7d9bcad89996e3fc962147e886f9cdad6262b2e86b4fc3725aff4cfc6116e18a61312ed0d578f9cd0f29943da7c30f95a33b34a27161846
7
- data.tar.gz: c426d15af5a5a675f28aada0d1acb760ec3e68f83bc521f3b8e565d6df4f73310ab643f8f0fc342dc7a83af23dd33fc8943333af1e66c4501d9b00290b784a3c
6
+ metadata.gz: e1e931dc2e984e21823e18cbbfcb56c6ff8d441a04ad6c8ee90bd618333345de655afa40852c2474cd65e3a819f9b4a66bae68907d97318845645bcb2ac32368
7
+ data.tar.gz: 9afcc20a56a0e6aec6c79cfa1a92ab36ea8c3f9928cb93fee8d07ecc502f65dcdf2920bf838a214265e9df398ea5a07e64cbecffad93b43b9d8c55b64e217d82
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ ##Offline Lookup
2
+
3
+ Alhpa (0.0.2). Use at your own risk.
4
+
5
+ IMPORTANT: don't use this for models that have a lot of data!!
6
+ This is basically a in-memory pseudo-index intended to speed up quick, repeated
7
+ finds of index table values without trips to an external db machine.
8
+ It's also nicer syntax, e.g:
9
+
10
+ ```
11
+ TurnaroundLevel.two_hour_id
12
+ TurnaroundLevel.quick_lookup("Two Hour")
13
+ Service.last.turnaround_level.two_hour?
14
+ ```
15
+
16
+ In any ActiveRecord::Base subclass, use:
17
+
18
+ `use_offline_id_lookup column_name`
19
+
20
+ to define class methods that can convert between id and the corresponding
21
+ value in `column_name` for the class, without going to the database
22
+
23
+ `column_name` defaults to `:name`, but can be any column of the table
24
+ use the `:key` keyword arg if you're interested in a key colun other than `:id`
25
+ Usage example:
26
+
27
+ ```
28
+ class TurnaroundLevel < ActiveRecord::Base
29
+ use_offline_lookup :level
30
+ ...
31
+ end
32
+ ```
33
+
34
+ If, for example, the first row of the turnaround_levels table has a `:level` of `"Same Day"`, this gives you
35
+
36
+ ```
37
+ TurnaroundLevel.same_day_id
38
+ #=> 1
39
+ TurnaroundLevel.name_for_id(1)
40
+ #=> 'Same Day'
41
+ TurnaroundLevel.id_for_name('Same Day')
42
+ #=> 1
43
+ TurnaroundLevel.first.same_day?
44
+ #=> true
45
+ TurnaroundLevel.same_day
46
+ #=> <#TurnaroundLevel id: 1, level: "Same Day", ...>
47
+ ```
48
+
49
+ The last of these methods is the "lookup" method, and is not quite offline. This is because we only store the key - name mappings, not the entire objects, in memory when we declare a new offline_lookup model. However it is included by default for convenient syntax (and it uses a lookup on what is usually the primary key of the table, in case the extra few ms matter to you). You can disable it by using
50
+
51
+ `use_offline_lookup :level, lookup_methods: false`
52
+
53
+ You can also disable the identity methods (e.g. `TurnaroundLevel.first.same_day?`) by using
54
+
55
+ `use_offline_lookup :level, identity_methods: false`
56
+
57
+ (And yes, the keywords can be combined, they're all optional keyword args)
58
+
59
+ ## Known Issues
60
+
61
+ If two entries in the table have the same value in the specified field, all but one will get overwritten. In a future version, I plan to allow multiple-column specificaion, e.g. `use_offline_lookup [:firstname, :lastname]`
@@ -1,61 +1,10 @@
1
- # IMPORTANT: don't use this for models that have a lot of data!!
2
- #
3
- # This is basically a in-memory pseudo-index intended to speed up quick, repeated
4
- # finds of index table values without trips to an external db machine.
5
- # It's also nicer syntax, e.g:
6
- # TurnaroundLevel.two_hour_id
7
- # TurnaroundLevel.quick_lookup("Two Hour")
8
- # Service.last.turnaround_level.two_hour?
9
- #
10
- # In any ActiveRecord::Base subclass, use:
11
- # >> use_offline_id_lookup column_name
12
- # to define class methods that can convert between id and the corresponding
13
- # value in column_name for the class, without going to the database
14
- #
15
- # column_name defaults to :name, but can be any column of the table
16
- # use the :key keyword arg if you're interested in a key colun other than :id
17
- #
18
- # Usage example:
19
- # class JobType < ActiveRecord::Base
20
- # use_offline_id_lookup
21
- # ...
22
- # end
23
- #
24
- # This gives you:
25
- # JobType.editing_id
26
- # #=> 1
27
- # JobType.name_for_id(1)
28
- # #=> 'Editing'
29
- # JobType.id_for_name('Editing')
30
- # #=> 1
31
- # with no db queries.
32
- #
33
- # You can use this on multiple column names (currently
34
- # need to call use_offline_id_lookup once for each column name)
35
- # class InvoiceLineItemType < ActiveRecord::Base
36
- # use_offline_id_lookup :name
37
- # use_offline_id_lookup :accounting_label
38
- # ...
39
- #
40
- # You get InvoiceLineItemType.name_for_id(id) and
41
- # InvoiceLineItemType.account_label_for_id(id), etc.
42
- # Beware that if any value occurs more than once,
43
- # either in the same column or different columns for
44
- # which use_offline_id_lookup was called, the <name>_id
45
- # method will only use the last column for which it was observed.
46
-
47
-
48
- #!!!
49
- # TODO: requires :methodize
50
- #!!!
51
-
52
1
  # TODO: provide multiple column names in a single call (e.g. firstname, lastname)
53
2
  # TODO: support multiple offline lookups per model
54
3
  # TODO: support scope arg in use_offline_lookup (partial index)
55
4
 
56
5
  module OfflineLookup
57
6
  module ActiveRecord
58
- def use_offline_lookup(field = :name, key: :id, lookup_methods: true)
7
+ def use_offline_lookup(field = :name, key: :id, identiyy_methods: true, lookup_methods: true)
59
8
  class_attribute :offline_lookup_values, :offline_lookup_options
60
9
  self.offline_lookup_options = {field: field.to_s, key: key.to_s, lookup_methods: lookup_methods}.freeze
61
10
  self.offline_lookup_values = self.all.pluck(key, field).to_h.freeze
@@ -125,10 +74,12 @@ module OfflineLookup
125
74
  end
126
75
 
127
76
  # instance method: true if instance is of named type (e.g. FooType.first.bar?)
128
- define_method(builder.indentiy_method_name(value)) do
129
- self.attributes[self.offline_lookup_options[:key]] == key
77
+ if self.offline_lookup_options[:identiyy_methods]
78
+ define_method(builder.indentiy_method_name(value)) do
79
+ self.attributes[self.offline_lookup_options[:key]] == key
80
+ end
130
81
  end
131
-
82
+
132
83
  # class method: get instance by named method (e.g. FooType.bar)
133
84
  # not "Offline", but lookup by indexed key. Also, synactic sugar.
134
85
  if self.offline_lookup_options[:lookup_methods]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: offline_lookup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Schwartz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-13 00:00:00.000000000 Z
11
+ date: 2016-04-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Offline indexing of small tables for speed & convenience
14
14
  email: ozydingo@gmail.com
@@ -16,6 +16,7 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - README.md
19
20
  - lib/offline_lookup.rb
20
21
  homepage:
21
22
  licenses: