active_store_accessor 0.1.2 → 0.2.0
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 +4 -4
- data/README.md +22 -3
- data/active_store_accessor.gemspec +1 -1
- data/lib/active_store_accessor.rb +12 -2
- data/tests/lib/active_store_accessor_test.rb +2 -2
- data/tests/test_helper.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06ba4627f521236634c1de9d03b20e672c5477aa
|
4
|
+
data.tar.gz: 139b01fbfc855b5959ef426844cd634fd1342f1a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 061f082b0c32e47db4489b6d07045b3d6557a57df737cb7373afa6046ff1693aa894ac8e52ad311810434508f26a76a9c6b8be24826b75255e076ad091889e67
|
7
|
+
data.tar.gz: 8e9952ba446c200649fea03e94a5ee4a6b96039de92f1e69b1843e92192a8667a443d709b65558822b48690c77a6bf7b0a0c738e0fde15642c36da592215931c
|
data/README.md
CHANGED
@@ -4,11 +4,11 @@
|
|
4
4
|
[](http://badge.fury.io/rb/active_store_accessor)
|
5
5
|
[](https://codeclimate.com/github/jalkoby/active_store_accessor)
|
6
6
|
|
7
|
-
|
7
|
+
`active_store_accessor` makes work with store accessors more productive. There is no need to cast a serialized attribute to required type(boolean, time, float, etc). Just define it with a tiny wrapper method and everything is done for you.
|
8
8
|
|
9
9
|
## Usage
|
10
10
|
|
11
|
-
|
11
|
+
Basic use case:
|
12
12
|
|
13
13
|
```ruby
|
14
14
|
class Profile < ActiveRecord::Base
|
@@ -30,6 +30,25 @@ profile.score = 4.5
|
|
30
30
|
profile.score # => 4.5
|
31
31
|
```
|
32
32
|
|
33
|
+
Extra logic in property methods:
|
34
|
+
```ruby
|
35
|
+
# Story:
|
36
|
+
# users have a rank, but if an user was locked by admins
|
37
|
+
# nobody can change it rank & it's value should be equal to zero
|
38
|
+
class User
|
39
|
+
active_store_accessor :info, rank: :float
|
40
|
+
|
41
|
+
def rank
|
42
|
+
0 if locked?
|
43
|
+
super
|
44
|
+
end
|
45
|
+
|
46
|
+
def rank=(value)
|
47
|
+
super unless locked?
|
48
|
+
end
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
33
52
|
## Installation
|
34
53
|
|
35
54
|
Add this line to your application's Gemfile:
|
@@ -42,7 +61,7 @@ And then execute:
|
|
42
61
|
|
43
62
|
## Requirements & dependencies
|
44
63
|
|
45
|
-
This library has been tested on ruby 1.9.3+ and activerecord 4.0+.
|
64
|
+
This library has been tested on ruby 1.9.3+ and activerecord 4.0+.
|
46
65
|
|
47
66
|
## Contributing
|
48
67
|
|
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "active_store_accessor"
|
7
|
-
spec.version = "0.
|
7
|
+
spec.version = "0.2.0"
|
8
8
|
spec.authors = ["Sergey Pchelincev"]
|
9
9
|
spec.email = ["mail@sergeyp.me"]
|
10
10
|
spec.summary = %q{Get more from ActiveRecord::Store}
|
@@ -14,7 +14,7 @@ module ActiveStoreAccessor
|
|
14
14
|
|
15
15
|
attrs.each do |attr_name, options|
|
16
16
|
options = { type: options.to_s } unless options.is_a?(Hash)
|
17
|
-
type = options.fetch(:type) { raise ArgumentError, "please specify type of attribute" }.to_s
|
17
|
+
type = options.fetch(:type) { raise ArgumentError, "please specify type of `#{ attr_name }` attribute" }.to_s
|
18
18
|
|
19
19
|
# time for activerecord is only a hours and minutes without date part
|
20
20
|
# but for most rubist and rails developer it should contains a date too
|
@@ -23,7 +23,7 @@ module ActiveStoreAccessor
|
|
23
23
|
args = [attr_name.to_s, options[:default], type]
|
24
24
|
active_store_attributes[attr_name] = ActiveRecord::ConnectionAdapters::Column.new(*args)
|
25
25
|
|
26
|
-
|
26
|
+
_active_store_accessor_module.module_eval <<-RUBY
|
27
27
|
def #{ attr_name }
|
28
28
|
column = self.class.active_store_attributes[:#{ attr_name }]
|
29
29
|
value = column.type_cast(super)
|
@@ -36,6 +36,16 @@ module ActiveStoreAccessor
|
|
36
36
|
RUBY
|
37
37
|
end
|
38
38
|
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def _active_store_accessor_module
|
43
|
+
@_active_store_accessor_module ||= begin
|
44
|
+
mod = Module.new
|
45
|
+
include mod
|
46
|
+
mod
|
47
|
+
end
|
48
|
+
end
|
39
49
|
end
|
40
50
|
|
41
51
|
ActiveSupport.on_load(:active_record) { ActiveRecord::Base.extend(ActiveStoreAccessor) }
|
@@ -12,13 +12,13 @@ describe ActiveStoreAccessor do
|
|
12
12
|
|
13
13
|
profile.age = "20"
|
14
14
|
profile.score = 100.32
|
15
|
-
profile.rank = "3213.
|
15
|
+
profile.rank = "3213.317"
|
16
16
|
profile.birthday = Time.utc(2014, 5, 12)
|
17
17
|
profile.confirmed = "1"
|
18
18
|
|
19
19
|
assert_equal profile.age, 20
|
20
20
|
assert_equal profile.score, 100
|
21
|
-
assert_equal profile.rank, 3213.
|
21
|
+
assert_equal profile.rank, 3213.32
|
22
22
|
assert_equal profile.birthday, Time.utc(2014, 5, 12)
|
23
23
|
assert profile.confirmed
|
24
24
|
end
|
data/tests/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_store_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Pchelincev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|