led 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/lib/led.rb +2 -0
  3. data/lib/model.rb +157 -0
  4. metadata +26 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a21ab33fb2807835562b391886b84e45e89e82fa
4
- data.tar.gz: afe3ee4adfc69ddc739bb728532226fad6bc4566
2
+ SHA256:
3
+ metadata.gz: 6776f20829ef1a0ca9589f85858fff70557013fd112d34da605d156c464c6f45
4
+ data.tar.gz: 242c21a40245612dddb2378d439756cbf401fe6797e271f2b396e71cca57826f
5
5
  SHA512:
6
- metadata.gz: 7ab616d037a2101cdf6c4e280ca4d0e9e132221943d741bb1ee2bda5b780aa659cfd4d0740f9cb618662910580a35420de964cf1e64691a8b162106f67d462fc
7
- data.tar.gz: a0a373537868b23250aac4de3c0c33b5a47d0aabeba7428467349c9f5cc00d19b1a0736065c9c2fef51f33b854e8b2b9fc55e990f33116efd856a6fa386a3170
6
+ metadata.gz: 4f3221702781a91e6a191595b7f46660953e3cd1ae5945179f00a2be9e1b6a7990793bf2b9001c721f9832c7e2d4423f923d096e20101d8b0cf602ac68d647ec
7
+ data.tar.gz: 94d9142fe2452ad48b9a495bb334c1aa7f2d5de119e1ce8c98aef54bb2527cd02a0e200d5699e186a3c6b1a5c384e4a9972c5df3e82d6a6c728faeb60ed6ba2d
data/lib/led.rb CHANGED
@@ -4,6 +4,8 @@ require 'bundler/setup'
4
4
  require 'hiredis'
5
5
  require 'redis'
6
6
 
7
+ require File.join(__dir__, 'model')
8
+
7
9
  module Led
8
10
  def self.conn= (conn)
9
11
  @conn = conn
@@ -0,0 +1,157 @@
1
+ require 'json'
2
+
3
+ class Hash
4
+ alias_method :orig_fetch, :[]
5
+
6
+ # allow access by symbol key
7
+ def [](key)
8
+ if Symbol === key
9
+ has_key?(key) ? orig_fetch(key) : orig_fetch(key.to_s)
10
+ else
11
+ orig_fetch(key)
12
+ end
13
+ end
14
+
15
+ def symbolize_keys
16
+ inject({}) {|m, kv| v = kv[1]; m[kv[0].to_sym] = v.is_a?(Hash) ? v.symbolize_keys : v; m}
17
+ end
18
+ end
19
+
20
+ module Led
21
+ class Model
22
+ class << self
23
+ def set_primary_key(key)
24
+ @primary_key = key
25
+ end
26
+
27
+ def primary_key
28
+ @primary_key ||= :id
29
+ end
30
+
31
+ def add_index(key)
32
+ indexes[key] = "models:#{name.downcase}:indexes:#{key}"
33
+ end
34
+
35
+ def indexes
36
+ @indexes ||= {}
37
+ end
38
+
39
+ def [](key)
40
+ from_json(Led.conn.hget(object_map_key, key))
41
+ end
42
+
43
+ def from_json(json_str)
44
+ load(JSON.parse(json_str).symbolize_keys)
45
+ end
46
+
47
+ def object_map_key
48
+ @object_map_key ||= "models:#{name.downcase}:objects"
49
+ end
50
+
51
+ def index_set_key(key, value)
52
+ "models:#{name.downcase}:indexes:#{key}:#{value.inspect}"
53
+ end
54
+
55
+ def load(hash)
56
+ new(hash, hash.dup)
57
+ end
58
+
59
+ def create(hash)
60
+ new(hash).save(true)
61
+ end
62
+
63
+ def filter(term)
64
+ conditions = {}
65
+ sets = []
66
+
67
+ term.each do |k, v|
68
+ if indexes[k]
69
+ sets << index_set_key(k, v)
70
+ else
71
+ conditions[k] = v
72
+ end
73
+ end
74
+
75
+ # load objects
76
+ if sets.empty?
77
+ objects = Led.conn.hgetall(object_map_key).map do |s|
78
+ from_json(s)
79
+ end
80
+ else
81
+ intersected = Led.conn.sinter(*sets)
82
+ return [] if intersected.empty?
83
+
84
+ objects = Led.conn.hmget(object_map_key, intersected).map do |s|
85
+ from_json(s)
86
+ end
87
+ end
88
+
89
+ if conditions.empty?
90
+ # return all found objects
91
+ objects
92
+ else
93
+ objects.select do |o|
94
+ conditions.inject(true) {|m, kv| m && (o[kv[0]] == kv[1])}
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ attr_reader :values
101
+
102
+ def initialize(hash = {}, saved_hash = {})
103
+ @values = hash
104
+ @saved_values = @values
105
+ end
106
+
107
+ def pkey
108
+ self.class.primary_key
109
+ end
110
+
111
+ def save(create = false)
112
+ if @saved_values[pkey] && @saved_values[pkey] != @values[pkey]
113
+ # pkey value has changed, so first remove the old record
114
+ Led.conn.hdel(self.class.object_map_key, @saved_values[pkey])
115
+ end
116
+
117
+ pk = @values[pkey]
118
+
119
+ # save object
120
+ Led.conn.hset(self.class.object_map_key, pk, @values.to_json)
121
+
122
+ # update indexes
123
+ self.class.indexes.each do |key, _|
124
+ if create || (@saved_values[key] != @values[key])
125
+ k = self.class.index_set_key(key, @saved_values[:key])
126
+ Led.conn.srem(k, pk)
127
+ k = self.class.index_set_key(key, @values[key])
128
+ Led.conn.sadd(k, pk)
129
+ end
130
+ end
131
+
132
+ # update saved values
133
+ @saved_values = @values.dup
134
+
135
+ self
136
+ end
137
+
138
+ SETTER_REGEXP = /^([0-9a-zA-Z_]+)=$/.freeze
139
+
140
+ def method_missing(m, *args)
141
+ if m =~ SETTER_REGEXP
142
+ @values[$1.to_sym] = args[0]
143
+ else
144
+ @values[m]
145
+ end
146
+ end
147
+
148
+ def inspect
149
+ v = values.map {|k, v| "#{k}: #{v.inspect}"}.join(', ')
150
+ "#<#{self.class.name}:#{@values[:pkey]} #{v}>"
151
+ end
152
+
153
+ def ==(obj)
154
+ (obj.class == self.class) && (obj.values == @values)
155
+ end
156
+ end
157
+ end
metadata CHANGED
@@ -1,36 +1,51 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: led
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-30 00:00:00.000000000 Z
11
+ date: 2013-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0
19
+ version: 4.0.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 3.0.0
27
- description: Easy Lua scripting for Redis
26
+ version: 4.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: hiredis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.1
41
+ description: Script-based ORM for Redis
28
42
  email: ciconia@gmail.com
29
43
  executables: []
30
44
  extensions: []
31
45
  extra_rdoc_files: []
32
46
  files:
33
47
  - lib/led.rb
48
+ - lib/model.rb
34
49
  homepage: http://github.com/ciconia/led
35
50
  licenses: []
36
51
  metadata: {}
@@ -40,18 +55,18 @@ require_paths:
40
55
  - lib
41
56
  required_ruby_version: !ruby/object:Gem::Requirement
42
57
  requirements:
43
- - - '>='
58
+ - - ">="
44
59
  - !ruby/object:Gem::Version
45
60
  version: '0'
46
61
  required_rubygems_version: !ruby/object:Gem::Requirement
47
62
  requirements:
48
- - - '>='
63
+ - - ">="
49
64
  - !ruby/object:Gem::Version
50
65
  version: '0'
51
66
  requirements: []
52
67
  rubyforge_project:
53
- rubygems_version: 2.0.3
68
+ rubygems_version: 2.7.3
54
69
  signing_key:
55
70
  specification_version: 4
56
- summary: Redis script preprocessor
71
+ summary: Redis ORM
57
72
  test_files: []