active_redis 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OGYyYjNmN2ZjY2FhNjgyNmI0MDk0ZjYxZmM1YzhiMmUzOGVmMjQzNA==
4
+ MDI3NjI4MjQwYjg4NmI4YTQzMjk1Y2RkYTdmMzk1ZDA3MWM4YjM0OA==
5
5
  data.tar.gz: !binary |-
6
- Y2ZjMWYwODllNjMwMWQzMDg0MTRiMWIzYjY1YjFjZTI5MDUxNzg3Nw==
6
+ MDdjN2ViZGVlODZhNDdkMGQ1OWRmNWQ3NzE5ZGY1MGQ2NzkzMGFiOA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NGRjOWU0YzI3YTZhZDVjZmFiNjJhOGE1NzY1ZmFjNDQ5MmNkODZkNTlkMzVl
10
- NWJiNTBhMTM4ZjQzMzMzMDY4OWJkMGNkOTZjYzc4NTEyZTgwZjZkNjdmYjJm
11
- MjRmZTk0NmEzZjdkZTc3NzlhNWU1YWNiNTZmNWRlZjk4MTJmZDU=
9
+ MTliYzEyZDQxOTgxY2MyZWVhZjlmOTg5MTZkMjk3OWMwZjVhMmU4MzQyMDJj
10
+ Y2U0YzA3MTEyYTk5ZWU2OWQwYWM1ZWFhMTU1ZTIzNzY0ZDBkNDFkMjIyMWU5
11
+ NGM5NzEzOGFmZjA2MDU1MDIzNzNkYWVjNmRkNjg1M2VmNGM0NjQ=
12
12
  data.tar.gz: !binary |-
13
- OGQxNzY5YWQwYWE0YTdlZjJmYTQyNDEwYWE5Yjc4ZjRiZTdiNWVkZDc1MThm
14
- ZmMyNzA3YzMzMzY5MThhMDZmMzY5NzhhNmQ4ZTM4NTY5ZmI1OTkwYWVjZGQ4
15
- MWVmYmM4MTZhZWY1YTcyNTk1ZTUzNTE2NTU0ZDc2MjgxMDk1YTg=
13
+ Y2ZlMzRiYzAzNjJkMmRmY2RkZjFkNGFlMmEzMWFkNWMxYWE3NGVhZTVlNWVh
14
+ ZDY5Yjk4ZWFlZTU4MmUyZmI2ZDUxOGQyNjkxM2IyNTY3OWJlMWQzZmNhZDJk
15
+ YThiMmE4ZDMyMGI5YjY3Yjk0MmI5NzliYWI3NTc0ODlkZGVmYmU=
data/README.md CHANGED
@@ -20,9 +20,11 @@ Or install it yourself as:
20
20
 
21
21
  ### Basic usage
22
22
 
23
+ In model you may add attributes with next types: __string__, __integer__, __time__
24
+
23
25
  ```ruby
24
26
  class Article < ActiveRedis::Base
25
- attributes :title, :link
27
+ attributes title: :string, link: :string
26
28
  end
27
29
  ```
28
30
 
@@ -93,7 +95,7 @@ Also gem add support for some aggregation functions like __sum__, __min__, __max
93
95
 
94
96
  ```ruby
95
97
  class Article < ActiveRedis::Base
96
- attributes :link, :title, :views
98
+ attributes link: :string, title: :string, views: :integer
97
99
  end
98
100
 
99
101
  Article.create(views: 1000, link: "http://someblog.com/1", title: "Title article")
@@ -122,7 +124,7 @@ rails g active_redis:model ModelName attribute1 attribute2
122
124
  For example:
123
125
 
124
126
  ```bash
125
- Sergeys-MacBook-Pro-2:test_active_redis sergey$ rails g active_redis:model User name city
127
+ Sergeys-MacBook-Pro-2:test_active_redis sergey$ rails g active_redis:model User name:string city:string
126
128
  create app/models/user.rb
127
129
  ```
128
130
 
@@ -130,18 +132,10 @@ The result is app/models/user.rb with stub content
130
132
 
131
133
  ```ruby
132
134
  class User < ActiveRedis::Base
133
- attributes :name, :city
135
+ attributes name: :string, city: :string
134
136
  end
135
137
  ```
136
138
 
137
- ### Future work
138
-
139
- At an early time I want to implement such features:
140
-
141
- 1. Setting/getting attributes with it's types
142
- 2. Relational Operators in where
143
- 3. Scopes???
144
-
145
139
  ## Contributing
146
140
 
147
141
  1. Fork it
@@ -28,7 +28,7 @@ module ActiveRedis
28
28
  end
29
29
 
30
30
  def belongs_to(name)
31
- define_attributes_accessors "#{name.to_s}_id"
31
+ define_attributes_accessors "#{name.to_s}_id" => :integer
32
32
  define_method name.to_s do
33
33
  name.to_s.capitalize.constantize.find(self.send("#{name.to_s}_id"))
34
34
  end
@@ -0,0 +1,17 @@
1
+ module ActiveRedis
2
+ module Attributes
3
+
4
+ class Attribute
5
+
6
+ def self.load(value)
7
+ raise NotImplementedError
8
+ end
9
+
10
+ def self.dump(value)
11
+ raise NotImplementedError
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveRedis
2
+ module Attributes
3
+ class IntegerAttribute < Attribute
4
+
5
+ def self.load(value)
6
+ value.to_i
7
+ end
8
+
9
+ def self.dump(value)
10
+ value.to_s
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveRedis
2
+ module Attributes
3
+ class StringAttribute < Attribute
4
+
5
+ def self.load(value)
6
+ value.to_s
7
+ end
8
+
9
+ def self.dump(value)
10
+ value.to_s
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveRedis
2
+ module Attributes
3
+ class TimeAttribute < Attribute
4
+
5
+ def self.load(value)
6
+ Time.at(value.to_i)
7
+ end
8
+
9
+ def self.dump(value)
10
+ value.to_i.to_s
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -1,4 +1,7 @@
1
+ Dir[File.dirname(__FILE__) + '/attributes/*.rb'].each {|file| require file }
2
+
1
3
  module ActiveRedis
4
+
2
5
  module Attributes
3
6
 
4
7
  def self.included(base)
@@ -12,7 +15,7 @@ module ActiveRedis
12
15
  end
13
16
 
14
17
  def attributes
15
- self.class.defined_attributes.inject({}) do |hash, attribute|
18
+ self.class.attributes_list.inject({}) do |hash, attribute|
16
19
  hash[attribute.to_sym] = self.send(attribute.to_s)
17
20
  hash
18
21
  end
@@ -20,37 +23,51 @@ module ActiveRedis
20
23
 
21
24
  module ClassMethods
22
25
 
23
- def attributes(*attrs)
24
- attrs = attrs.concat(ActiveRedis::Constants::DEFAULT_ATTRIBUTES)
26
+ def attributes(attrs)
27
+ raise ActiveRedis::InvalidArgumentError, "Value must be a Hash!" unless attrs.is_a?(Hash)
28
+ attrs = attrs.merge(ActiveRedis::Constants::DEFAULT_ATTRIBUTES)
25
29
  class << self; attr_accessor :defined_attributes; end
26
- self.defined_attributes ||= []
30
+ self.defined_attributes ||= {}
27
31
  define_attributes_accessors attrs
28
32
  end
29
33
 
30
34
  def define_attributes_accessors(attrs)
31
- attrs = *attrs unless attrs.is_a?(Array)
32
- attrs.each do |attribute|
35
+ attrs.each do |attribute, type|
36
+ next unless register_attribute(attribute, type)
33
37
  read_attribute attribute
34
38
  write_attribute attribute
35
- register_attribute attribute
36
39
  end
37
40
  end
38
41
 
42
+ def attributes_list
43
+ self.defined_attributes.keys.map(&:to_s)
44
+ end
45
+
39
46
  private
40
47
 
41
- def register_attribute(attribute)
42
- self.defined_attributes << attribute unless self.defined_attributes.include?(attribute)
48
+ def register_attribute(attribute, type)
49
+ return if self.defined_attributes.has_key? attribute.to_sym
50
+ self.defined_attributes[attribute.to_sym] = {
51
+ class: "ActiveRedis::Attributes::#{type.to_s.capitalize}Attribute".constantize,
52
+ type: type
53
+ }
54
+ end
55
+
56
+ def attribute_class(attribute)
57
+ attr_class = self.defined_attributes[attribute.to_sym][:class]
43
58
  end
44
59
 
45
60
  def read_attribute(attribute)
46
61
  define_method attribute do
47
- instance_variable_get("@#{attribute}")
62
+ klass = self.class.send :attribute_class, attribute
63
+ klass.load(instance_variable_get("@#{attribute}"))
48
64
  end
49
65
  end
50
66
 
51
67
  def write_attribute(attribute)
52
68
  define_method "#{attribute}=" do |value|
53
- instance_variable_set("@#{attribute}", value)
69
+ klass = self.class.send :attribute_class, attribute
70
+ klass.dump(instance_variable_set("@#{attribute}", value))
54
71
  end
55
72
  end
56
73
 
@@ -4,6 +4,7 @@ require 'active_redis/persistence'
4
4
  require 'active_redis/naming'
5
5
  require 'active_redis/calculations'
6
6
  require 'active_redis/finders'
7
+ require 'active_redis/inspector'
7
8
 
8
9
  # TODO: Add Expiring module
9
10
 
@@ -16,6 +17,7 @@ module ActiveRedis
16
17
  include Attributes
17
18
  include Associations
18
19
  include Persistence
20
+ include Inspector
19
21
 
20
22
  end
21
23
  end
@@ -5,7 +5,7 @@ module ActiveRedis
5
5
 
6
6
  SAVE_SUCCESS_ANSWER = "OK"
7
7
 
8
- DEFAULT_ATTRIBUTES = [:id, :created_at, :updated_at]
8
+ DEFAULT_ATTRIBUTES = {id: :integer, created_at: :time, updated_at: :time}
9
9
 
10
10
  end
11
11
  end
@@ -0,0 +1,11 @@
1
+ module ActiveRedis
2
+ module Inspector
3
+
4
+ def inspect
5
+ string = "#<#{self.class.name}:#{self.object_id} "
6
+ fields = self.class.attributes_list.map{|field| "#{field}: #{self.send(field)}"}
7
+ string << fields.join(", ") << ">"
8
+ end
9
+
10
+ end
11
+ end
@@ -25,15 +25,15 @@ module ActiveRedis
25
25
  private
26
26
 
27
27
  def fill_attributes
28
- self.id ||= ActiveRedis.connection.next_id(self.class)
29
- self.created_at ||= Time.now.to_i
30
- self.updated_at = Time.now.to_i
28
+ self.id ||= ActiveRedis.connection.next_id(self.class)
29
+ self.created_at ||= Time.now
30
+ self.updated_at = Time.now
31
31
  end
32
32
 
33
33
  def prepare_hash
34
34
  fill_attributes
35
- self.class.defined_attributes.inject({}) do |hash, attribute|
36
- hash[attribute] = self.send("#{attribute}"); hash
35
+ self.class.attributes_list.inject({}) do |hash, attribute|
36
+ hash[attribute.to_sym] = self.send("#{attribute}"); hash
37
37
  end
38
38
  end
39
39
 
@@ -1,3 +1,3 @@
1
1
  module ActiveRedis
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -3,7 +3,7 @@ require 'generators/active_redis'
3
3
  module ActiveRedis
4
4
  module Generators
5
5
  class ModelGenerator < Base
6
- argument :actions, type: :array, default: [], banner: "attribute attribute"
6
+ argument :actions, type: :array, default: [], banner: "attribute:type attribute:type"
7
7
 
8
8
  def create_model
9
9
  template 'model.rb', File.join('app/models', "#{file_name}.rb")
@@ -1,3 +1,3 @@
1
1
  class <%= class_name %> < ActiveRedis::Base
2
- attributes <%= actions.map{|a| ":#{a}"}.join(", ") %>
2
+ attributes <%= actions.map{|a| str = a.split(':'); "#{str[0]}: :#{str[1]}"}.join(", ") %>
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.4
4
+ version: 0.0.5
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-28 00:00:00.000000000 Z
11
+ date: 2013-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -97,6 +97,10 @@ files:
97
97
  - lib/active_redis/adapters/basic_adapter.rb
98
98
  - lib/active_redis/associations.rb
99
99
  - lib/active_redis/attributes.rb
100
+ - lib/active_redis/attributes/attribute.rb
101
+ - lib/active_redis/attributes/integer_attribute.rb
102
+ - lib/active_redis/attributes/string_attribute.rb
103
+ - lib/active_redis/attributes/time_attribute.rb
100
104
  - lib/active_redis/base.rb
101
105
  - lib/active_redis/calculations.rb
102
106
  - lib/active_redis/config.rb
@@ -109,6 +113,7 @@ files:
109
113
  - lib/active_redis/errors.rb
110
114
  - lib/active_redis/finders.rb
111
115
  - lib/active_redis/helpers/lua_scripts.rb
116
+ - lib/active_redis/inspector.rb
112
117
  - lib/active_redis/lua_scripts/main.lua
113
118
  - lib/active_redis/naming.rb
114
119
  - lib/active_redis/persistence.rb