redis_storage_methods 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.
data/README.md CHANGED
@@ -18,28 +18,55 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
+ Presumably, you'll be passing hashes into your create method. Now you will need to pass them into your
22
+
23
+ create_with_associations_and_redis(params)
24
+
25
+ method. This is to differentiate between the two: This method will take your object and create it, and then store it in redis as a hash. It will not store it's associations unless you instruct it to do so. This is because redis has no ability to store foreign keyed objects, if a Battle has a Soldier in it, you can really only store it in a redis hash for Battle like this: soldier0hp, soldier0stamina, soldier1hp, soldier1stamina, etc.
26
+
27
+ You need to instruct it to do this.
28
+
21
29
  Pretty easy here, just do:
22
30
 
23
31
  class Battle < ActiveRecord::Base
24
32
  include RedisStorageMethods
33
+ has_many :soldiers
25
34
  ...
26
35
  end
27
36
 
28
- Once you've done this, you'll need to add a few methods to Battle that the Mixin requires to be defined(even empty methods will do).
37
+ Once you've done this, you'll need to add a few methods to Battle that the Mixin requires if you want to store nested objects, like Soldiers for a battle. I am going to provide an example set of methods below assuming Battle has a nested object called Soldier.
29
38
 
30
- #accepts the part of the form hash with nested atributes(but with attributes taken off keys), like { "videos" => {"0" => {field => value}}}
39
+ #accepts the part of the form hash with nested atributes(but with attributes taken off keys), like { "soldiers" => {"0" => {field => value}}}
31
40
  #and then creates models. Because we don't support nested creation at the meta level, must be handled locally.
32
41
  def create_associations_from_params(params)
42
+ params["soldiers"].andand.each do |index, values|
43
+ self.soldiers << Soldier.create(values.merge("battle_id" => self.id))
44
+ end
33
45
  end
34
46
 
35
47
  #No nesting in Redis, no methods for listing associations on DataMapper/AR, This is custom, returns nested objects for use in storage
36
48
  #with redis. Because I dont want to write metacode that detects nested objects within my own nested objects, I'm just leaving it
37
49
  # to each model to implement this in a custom fashion.
38
50
  def add_key_value_pairs_of_associations_for_redis!(array)
51
+ i=0
52
+ soldiers.each do |video|
53
+ Soldier.properties.map { |prop| prop.name.to_s }.each do |p|
54
+ array<<"soldier#{i}#{p}"
55
+ array<< soldier.send(p)
56
+ end
57
+ i+=1
58
+ end
59
+
60
+ array
39
61
  end
40
62
 
41
- #expects to get hash of things like video0video_id => "555" from redis, needs to create associations from it.
63
+ #expects to get hash of things like video0video_id => "555" from redis, needs to create associations from it. This is used when we GET from Redis, and need to reconstruct a model.
42
64
  def populate_associations_from_redis(redis_hash)
65
+ construct_low_level_model_from_redis_hash(redis_hash, "soldiers")
66
+ #bonus, this method above is included in RedisStorageMethods for simple models that have no
67
+ #other associations and only attribute values. It will take fields like "soldier0hp", realize
68
+ #it's part of a soldier object, the zeroth in the index, and make that soldier.
69
+
43
70
  end
44
71
 
45
72
  #if you have sets stored outside the redis hash, need to make a call to get them, haven't you?
@@ -47,8 +74,20 @@ Once you've done this, you'll need to add a few methods to Battle that the Mixin
47
74
  def populate_custom_fields_from_extraneous_redis_calls
48
75
  end
49
76
 
50
- #method to sync object in db with redis.
77
+ #method to sync object in db with redis. You may want to set this up on a cron script to keep
78
+ #the databases in sync. A lot of developers don't want to call the DB for PUT requests, they
79
+ #just find & change the redis object instead. This is where you reconcile
51
80
  def sync_with_redis
81
+ me_fake = Battle.find_with_redis(self.id)
82
+
83
+ self.soldiers.each do |soldier|
84
+ me_fake.soldiers.each do |f_s|
85
+ soldier.hp = f_s.hp if(soldier.id == f_s.id)
86
+ end
87
+ end
88
+
89
+ self.save
90
+
52
91
  end
53
92
 
54
93
  #create the custom fields you need like lists for arrays etc, artist:name => id references,
@@ -56,6 +95,7 @@ Once you've done this, you'll need to add a few methods to Battle that the Mixin
56
95
  def create_custom_redis_fields
57
96
  end
58
97
 
98
+ #after hook for after you put hash in redis.
59
99
  def after_redis_create_do
60
100
  end
61
101
 
@@ -1,3 +1,3 @@
1
1
  module RedisStorageMethods
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_storage_methods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: