doeskeyvalue 0.1.0 → 0.1.1
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.
- data/README.rdoc +68 -62
- data/VERSION +1 -1
- data/doeskeyvalue.gemspec +1 -1
- data/lib/doeskeyvalue/indexes.rb +0 -1
- data/lib/doeskeyvalue/keys.rb +0 -5
- data/lib/doeskeyvalue.rb +0 -4
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -12,68 +12,74 @@ Do the usual, of course:
|
|
12
12
|
|
13
13
|
And add a gem dependency to your Gemfile:
|
14
14
|
|
15
|
-
gem "doeskeyvalue", ">=0.0
|
16
|
-
|
17
|
-
|
18
|
-
== Example
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
=> [#<
|
75
|
-
|
76
|
-
|
15
|
+
gem "doeskeyvalue", ">=0.1.0"
|
16
|
+
|
17
|
+
|
18
|
+
== Example
|
19
|
+
|
20
|
+
Quick and simple. First, add the declaration to your ActiveRecord model:
|
21
|
+
|
22
|
+
doeskeyvalue :settings
|
23
|
+
|
24
|
+
In this invocation, "settings" is the name of TEXT or BLOB field on your model's database table. Think
|
25
|
+
of it as something you'd typically serialize, but would prefer solid accessor and finding behavior on.
|
26
|
+
|
27
|
+
To add keys to your document, using the name of the field you provided, before (in this case
|
28
|
+
"settings"), add keys individually like so:
|
29
|
+
|
30
|
+
settings_key :uid
|
31
|
+
settings_key :email
|
32
|
+
settings_key :name
|
33
|
+
|
34
|
+
This adds a uid, email, and name field to your record. You can use these fields just like regular
|
35
|
+
columns on your ActiveRecord object, but they're happily stored within your TEXT/BLOB column at the
|
36
|
+
database level. Check it out:
|
37
|
+
|
38
|
+
mod = Model.new
|
39
|
+
|
40
|
+
mod.uid = "mccolin"
|
41
|
+
=> "mccolin"
|
42
|
+
|
43
|
+
mod.uid
|
44
|
+
=> "mccolin"
|
45
|
+
|
46
|
+
You can see the serialized key-value structure at any time, as well. Just access your old filed:
|
47
|
+
|
48
|
+
mod.settings
|
49
|
+
=> {:uid=>"mccolin"}
|
50
|
+
|
51
|
+
At this point, we have a fancy serialized Hash on our hands. What we really want to be able to do is
|
52
|
+
search for objects of type Model that have certain values. That's possible, too. First, generate the
|
53
|
+
necessary migration:
|
54
|
+
|
55
|
+
rails g doeskeyvalue
|
56
|
+
|
57
|
+
This creates db/migrate/XXX_create_key_value_index.rb for you. Migrate your database:
|
58
|
+
|
59
|
+
rake db:migrate
|
60
|
+
|
61
|
+
Now, you can declare keys that you'd like to be searchable within Model like so:
|
62
|
+
|
63
|
+
settings_index :uid
|
64
|
+
settings_index :email
|
65
|
+
|
66
|
+
Now, you've added powerful finders to your objects. As your objects are created, accessed, and updated,
|
67
|
+
metadata in your index table will be updated that allows you to search against those objects similarly
|
68
|
+
to how you'd perform lookups with a regular finder. Like so:
|
69
|
+
|
70
|
+
Model.find_all_by_uid(123)
|
71
|
+
=> [#<Model:0x000001016b51a0 @settings=>{:uid=>123}>, #<Model:0x000001016b51a0 @settings=>{:uid=>123}>, ...]
|
72
|
+
|
73
|
+
Model.find_all_with_settings(:email=>"foo@bar.com")
|
74
|
+
=> [#<Model:0x000001016b51a0 @settings=>{:email=>"foo@bar"}>, ...]
|
75
|
+
|
76
|
+
You can only run finds on keys for which you've added an index, but you can add and remove indexes as
|
77
|
+
necessary simply by removing the index declaration from your model.
|
78
|
+
|
79
|
+
Likewise, you can remove keys from any model by removing the key declaration. This allows you to add
|
80
|
+
and remove keys from every ActiveRecord object at will, without having to build any migrations, but you
|
81
|
+
can still reap the benefits of whatever SQL-backed relational database you've chosen for the bulk of
|
82
|
+
your application's heavy lifting.
|
77
83
|
|
78
84
|
|
79
85
|
== Prior Versions
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/doeskeyvalue.gemspec
CHANGED
data/lib/doeskeyvalue/indexes.rb
CHANGED
@@ -8,7 +8,6 @@ module DoesKeyValue
|
|
8
8
|
module Indexes
|
9
9
|
|
10
10
|
def declare_index(key_value_column, key_name, opts={})
|
11
|
-
puts "DOES_KEY_VALUE: Index declared: #{key_value_column}, #{key_name}, #{opts.inspect}"
|
12
11
|
raise DoesKeyValue::NoColumnNameSpecified unless key_value_column
|
13
12
|
raise DoesKeyValue::NoKeyNameSpecified unless key_name
|
14
13
|
raise DoesKeyValue::KeyAndIndexOptionsMustBeHash unless opts.is_a?(Hash)
|
data/lib/doeskeyvalue/keys.rb
CHANGED
@@ -7,7 +7,6 @@ module DoesKeyValue
|
|
7
7
|
module Keys
|
8
8
|
|
9
9
|
def declare_key(key_value_column, key_name, opts={})
|
10
|
-
puts "DOES_KEY_VALUE: Key declared: #{key_value_column}, #{key_name}, #{opts.inspect}"
|
11
10
|
raise DoesKeyValue::NoColumnNameSpecified unless key_value_column
|
12
11
|
raise DoesKeyValue::NoKeyNameSpecified unless key_name
|
13
12
|
raise DoesKeyValue::KeyAndIndexOptionsMustBeHash unless opts.is_a?(Hash)
|
@@ -15,18 +14,14 @@ module DoesKeyValue
|
|
15
14
|
# Define accessors for the key column in the AR class:
|
16
15
|
class_eval <<-EOS
|
17
16
|
def #{key_name}
|
18
|
-
puts "DOES_KEY_VALUE: Accessor for `#{key_name}` invoked"
|
19
17
|
return (self.send(:#{key_value_column}) || Hash.new)[:#{key_name}]
|
20
18
|
end
|
21
|
-
puts "DOES_KEY_VALUE: Key accessor `#{key_name}` declared"
|
22
19
|
|
23
20
|
def #{key_name}=(value)
|
24
|
-
puts "DOES_KEY_VALUE: Setter for `#{key_name}` invoked"
|
25
21
|
key_set = self.send(:#{key_value_column}) || Hash.new
|
26
22
|
key_set[:#{key_name}] = value
|
27
23
|
self.send("#{key_value_column}=", key_set)
|
28
24
|
end
|
29
|
-
puts "DOES_KEY_VALUE: Key manipulator `#{key_name}=` declared"
|
30
25
|
EOS
|
31
26
|
|
32
27
|
# Check for opts[:index=>true] and if present, call declare_index
|
data/lib/doeskeyvalue.rb
CHANGED
@@ -47,7 +47,6 @@ module ActiveRecord
|
|
47
47
|
# Call this method within your class to establish key-value behavior and prep
|
48
48
|
# the internal structure that will hold the blob
|
49
49
|
def self.doeskeyvalue(column, opts={})
|
50
|
-
puts "DOES_KEY_VALUE: Turned on for AR Column:#{column}"
|
51
50
|
self.instance_eval do
|
52
51
|
extend DoesKeyValue::Keys
|
53
52
|
extend DoesKeyValue::Indexes
|
@@ -64,18 +63,15 @@ module ActiveRecord
|
|
64
63
|
|
65
64
|
instance_eval <<-EOS
|
66
65
|
def #{@@key_value_column}_key(key_name, opts={})
|
67
|
-
puts "DOES_KEY_VALUE: Inside defined method #{@@key_value_column}_key"
|
68
66
|
key_name = key_name.to_sym
|
69
67
|
declare_key(@@key_value_column, key_name, opts)
|
70
68
|
end
|
71
69
|
|
72
70
|
def #{@@key_value_column}_index(key_name, opts={})
|
73
|
-
puts "DOES_KEY_VALUE: Inside defined method #{@@key_value_column}_index"
|
74
71
|
key_name = key_name.to_sym
|
75
72
|
declare_index(@@key_value_column, key_name, opts)
|
76
73
|
end
|
77
74
|
EOS
|
78
|
-
puts "DOES_KEY_VALUE: key and index methods declared"
|
79
75
|
end
|
80
76
|
|
81
77
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: doeskeyvalue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Awexome Labs
|
@@ -73,7 +73,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
73
|
requirements:
|
74
74
|
- - ">="
|
75
75
|
- !ruby/object:Gem::Version
|
76
|
-
hash:
|
76
|
+
hash: 136860461958198637
|
77
77
|
segments:
|
78
78
|
- 0
|
79
79
|
version: "0"
|