active_redis 0.0.2 → 0.0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjMwYzRjYzgwNGQzN2U4MTk4MDc0NGVhNzE5YmUzOWEyNjZjNmQ0Mw==
4
+ YWEwZjlmY2YyODRkNTY3ZTU3NmVlZTlkOTFhNDU0MTM5NjMwNGY3NQ==
5
5
  data.tar.gz: !binary |-
6
- MmYwMzc0ZGI0YmZjMzczMTU2MTI0MzE2YzMyMjU0YTU3NDg2ZGQyNA==
6
+ YzNmNjQwZGVhNDVmOTdlZWNmNTM4OTdhNmY2NzhiZTI4YmU2MDUyYg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MzM1ZDAzMzZmMTE2ZjIxNTYxYzIzNTdhZjhkYzA0ZWM5OTljYmY2ZjM4NzEw
10
- ZTEwYjZkMDg0NDJhYmNkODNiNmU5NjM1MDQ5N2FmZDk2NDQzODBmNjRhYTY1
11
- NGI0MzA3YjM3MWQ2YTUwMWM0ZGZhOTgyZDQ1NGFjMjFkYmRiMDY=
9
+ ZjIyZDkwNDk5ODIyMmIzMDEzNzdlZjA3NWIzZmI1MmE5ZDU4YTJhMGYyNDA0
10
+ YTRlNjJhMWUwNDY0YTdmMjQ4NTdkMmU5MWY5ZDNlNjY5ZmZiYzE1ODk1NGI1
11
+ NWZjZDk2ZGI2N2ZhYmJhMmI1ZDZjN2U4YTY3ZDU0MTZhYzQ1NzY=
12
12
  data.tar.gz: !binary |-
13
- MjNmMTdiYWY1M2E3OWFiOWRlMTRlYzg3MmM5Mzg2MDQwYjg5YmQyMjM1Mjll
14
- NzUzYjI4ZmU1OWIyMmZkYTgyZTY1NTUyZmI3OWY4ZjRhNjQ2YTBiZjFiYTM5
15
- ZjliYWE0ODM0MDkxMTVmOWI0NWI1YjMzMTc4NjMxMWI2ZjkzOGY=
13
+ ZTRjYTNjY2EwNTNkMTEwOWQwOGJkMjJlZGMxNjdhY2FmODdhYjMxNDNmNTY3
14
+ Y2ExZTFkMmQ3OGU3NTQ1YmUyNWM4NjFkNWU4ODI3Njg3MjczYjBiZDAzYzZm
15
+ ZjkzNzE1MDllYTY4N2E5ZTFkZWIxMjQ5YjYxMWQzN2YwZWFjMWU=
@@ -7,9 +7,13 @@ module ActiveRedis
7
7
 
8
8
  module ClassMethods
9
9
 
10
- # TODO: add has_one functionality
11
- def has_one
12
-
10
+ def has_one(name)
11
+ define_method name.to_s do
12
+ name.to_s.capitalize.constantize.where("#{self.class.foreign_key_name}" => self.id).first
13
+ end
14
+ define_method "#{name.to_s}=" do |value|
15
+ value.send "#{self.class.foreign_key_name}=", self.id
16
+ end
13
17
  end
14
18
 
15
19
  # TODO: add has_many functionality
@@ -18,8 +22,14 @@ module ActiveRedis
18
22
  end
19
23
 
20
24
  # TODO: add belongs_to functionality
21
- def belongs_to
22
-
25
+ def belongs_to(name)
26
+ define_attributes_accessors "#{name.to_s}_id"
27
+ define_method name.to_s do
28
+ name.to_s.capitalize.constantize.find(self.send("#{name.to_s}_id"))
29
+ end
30
+ define_method "#{name.to_s}=" do |value|
31
+ self.send "#{name.to_s}_id=", value.id
32
+ end
23
33
  end
24
34
 
25
35
  end
@@ -6,7 +6,8 @@ module ActiveRedis
6
6
  end
7
7
 
8
8
  def attributes=(value)
9
- raise ActiveRedis::InvalidArgumentError, "Value must be a Hash" unless value.is_a? Hash
9
+ raise ActiveRedis::InvalidArgumentError, "Value must be a Hash or Array" if !value.is_a?(Hash) && !value.is_a?(Array)
10
+ value = Hash[*value] if value.is_a?(Array)
10
11
  value.each { |attribute, value| self.send("#{attribute}=", value) }
11
12
  end
12
13
 
@@ -21,18 +22,36 @@ module ActiveRedis
21
22
 
22
23
  def attributes(*attrs)
23
24
  attrs = attrs.concat(ActiveRedis::Constants::DEFAULT_ATTRIBUTES)
25
+ class << self; attr_accessor :defined_attributes; end
26
+ self.defined_attributes ||= []
27
+ define_attributes_accessors attrs
28
+ end
29
+
30
+ def define_attributes_accessors(attrs)
31
+ attrs = *attrs unless attrs.is_a?(Array)
24
32
  attrs.each do |attribute|
25
- define_method "#{attribute}=" do |value|
26
- instance_variable_set("@#{attribute}", value)
27
- end
28
- define_method attribute do
29
- instance_variable_get("@#{attribute}")
30
- end
33
+ read_attribute attribute
34
+ write_attribute attribute
35
+ register_attribute attribute
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def register_attribute(attribute)
42
+ self.defined_attributes << attribute unless self.defined_attributes.include?(attribute)
43
+ end
44
+
45
+ def read_attribute(attribute)
46
+ define_method attribute do
47
+ instance_variable_get("@#{attribute}")
31
48
  end
32
- class << self
33
- attr_accessor :defined_attributes
49
+ end
50
+
51
+ def write_attribute(attribute)
52
+ define_method "#{attribute}=" do |value|
53
+ instance_variable_set("@#{attribute}", value)
34
54
  end
35
- self.defined_attributes = attrs
36
55
  end
37
56
 
38
57
  end
@@ -9,6 +9,7 @@ module ActiveRedis
9
9
 
10
10
  include ActiveRedis::ConnectionExt::FindersLayer
11
11
  include ActiveRedis::ConnectionExt::PersistenceLayer
12
+ include ActiveRedis::ConnectionExt::ScriptsLayer
12
13
 
13
14
  calculations ActiveRedis::Constants::CALCULATION_METHODS
14
15
 
@@ -18,5 +19,11 @@ module ActiveRedis
18
19
 
19
20
  attr_accessor :adapter
20
21
 
22
+ private
23
+
24
+ def run_eval(type, keys = [], argv = [])
25
+ adapter.eval send("#{type}_script"), keys: keys, argv: argv
26
+ end
27
+
21
28
  end
22
29
  end
@@ -12,7 +12,7 @@ module ActiveRedis::ConnectionExt
12
12
  methods.each do |method|
13
13
  eval_string += <<-EVAL
14
14
  def calculate_#{method}(model, attributes = "")
15
- adapter.eval send("#{method}_script"), keys: [model.key_name], argv: [attributes]
15
+ run_eval :#{method}, [model.key_name], [attributes]
16
16
  end
17
17
  EVAL
18
18
  end
@@ -2,7 +2,6 @@ module ActiveRedis::ConnectionExt
2
2
  module FindersLayer
3
3
 
4
4
  def fetch_row(model, id)
5
- raise ActiveRedis::NotSpecifiedIdError, "Must specified ID for finding record!" unless id
6
5
  adapter.hgetall model.table_name(id)
7
6
  end
8
7
 
@@ -11,7 +10,11 @@ module ActiveRedis::ConnectionExt
11
10
  end
12
11
 
13
12
  def fetch_where(model, params)
14
- adapter.eval where_script, keys: [model.key_name], argv: params.flatten
13
+ run_eval :where, [model.key_name], params.flatten
14
+ end
15
+
16
+ def fetch_all(model)
17
+ fetch_where model, {}
15
18
  end
16
19
 
17
20
  end
@@ -8,9 +8,8 @@ module ActiveRedis::ConnectionExt
8
8
 
9
9
  def next_id(model)
10
10
  table = model.info_table_name
11
- create_info_table(model) unless @adapter.exists(table)
11
+ create_info_table(model) unless adapter.exists(table)
12
12
  adapter.hincrby table, "next_id", 1
13
- adapter.hget table, "next_id"
14
13
  end
15
14
 
16
15
  def create_info_table(model)
@@ -0,0 +1,9 @@
1
+ module ActiveRedis::ConnectionExt
2
+ module ScriptsLayer
3
+
4
+ def flush_script
5
+ adapter.script :flush
6
+ end
7
+
8
+ end
9
+ end
@@ -2,12 +2,21 @@ module ActiveRedis
2
2
  module Finders
3
3
 
4
4
  def find(*ids)
5
- ids.map { |id| self.new(ActiveRedis.connection.fetch_row(self, id)) }
5
+ res = ids.inject([]) do |result, id|
6
+ attrs = ActiveRedis.connection.fetch_row(self, id)
7
+ result << self.new(attrs) if attrs.any?
8
+ result
9
+ end
10
+ ids.count == 1 ? res.first : res
6
11
  end
7
12
 
8
13
  def where(params)
9
14
  ActiveRedis.connection.fetch_where(self, params).map { |attrs| self.new(attrs) }
10
15
  end
11
16
 
17
+ def all
18
+ ActiveRedis.connection.fetch_all(self).map { |attrs| self.new(attrs) }
19
+ end
20
+
12
21
  end
13
22
  end
@@ -1,3 +1,5 @@
1
+ require 'active_redis/constants'
2
+
1
3
  module ActiveRedis
2
4
  module Helpers
3
5
  module LuaScripts
@@ -14,6 +14,10 @@ module ActiveRedis
14
14
  "#{self.name.downcase.pluralize}:info"
15
15
  end
16
16
 
17
+ def foreign_key_name
18
+ "#{self.name.downcase}_id"
19
+ end
20
+
17
21
  end
18
22
 
19
23
  end
@@ -6,7 +6,7 @@ module ActiveRedis
6
6
  end
7
7
 
8
8
  def initialize(attrs = {})
9
- self.attributes = Hash[*attrs]
9
+ self.attributes = attrs
10
10
  end
11
11
 
12
12
  def save
@@ -1,3 +1,3 @@
1
1
  module ActiveRedis
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Gernyak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-27 00:00:00.000000000 Z
11
+ date: 2013-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -90,6 +90,7 @@ files:
90
90
  - lib/active_redis/connection_ext/calculations_layer.rb
91
91
  - lib/active_redis/connection_ext/finders_layer.rb
92
92
  - lib/active_redis/connection_ext/persistence_layer.rb
93
+ - lib/active_redis/connection_ext/scripts_layer.rb
93
94
  - lib/active_redis/constants.rb
94
95
  - lib/active_redis/errors.rb
95
96
  - lib/active_redis/finders.rb