active_store_accessor 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21798f125f793a3a4f664dd3c005871f64228658
4
- data.tar.gz: 8c0f9fabb18aa63200c14889911b86a346f3603d
3
+ metadata.gz: 06ba4627f521236634c1de9d03b20e672c5477aa
4
+ data.tar.gz: 139b01fbfc855b5959ef426844cd634fd1342f1a
5
5
  SHA512:
6
- metadata.gz: 30bd5cc30e6d13bc183c0889d2f19ebb9163e0bdf69c163ff63481ace00f76a4e9f3b2c460fc0ec1349b72be28ada3e2a214c17d885764135ae6b75ece8684f5
7
- data.tar.gz: d513515b3108bf6d697b59112e58f16f2cbcfce679d3fa9c4fadc3505f36e8ecd930ebe3b9459766b9fe80ad87e87753e01139ad16596a36ed32bf0dfd00c624
6
+ metadata.gz: 061f082b0c32e47db4489b6d07045b3d6557a57df737cb7373afa6046ff1693aa894ac8e52ad311810434508f26a76a9c6b8be24826b75255e076ad091889e67
7
+ data.tar.gz: 8e9952ba446c200649fea03e94a5ee4a6b96039de92f1e69b1843e92192a8667a443d709b65558822b48690c77a6bf7b0a0c738e0fde15642c36da592215931c
data/README.md CHANGED
@@ -4,11 +4,11 @@
4
4
  [![Gem Version](https://badge.fury.io/rb/active_store_accessor.svg)](http://badge.fury.io/rb/active_store_accessor)
5
5
  [![Code Climate](https://codeclimate.com/github/jalkoby/active_store_accessor.png)](https://codeclimate.com/github/jalkoby/active_store_accessor)
6
6
 
7
- With `active_store_accessor` you can have boolean, integer, float, time store attributes which would act like a regular schema columns.
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
- After you add gem into Gemfile everything is done for you. Now you can declare your serialized properties in a next way:
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+. Any other configurations might have potential issues. `active_store_accessor` doesn't have any external dependency(expect activerecord) and contains only one file with one module.
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.1.2"
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
- class_eval <<-RUBY
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.312"
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.312
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
@@ -25,6 +25,11 @@ class Profile < ActiveRecord::Base
25
25
 
26
26
  active_store_accessor :keys, active: :boolean
27
27
  active_store_accessor :keys, pi: :float
28
+
29
+ def rank=(value)
30
+ super(value)
31
+ super(rank.round(2)) if rank
32
+ end
28
33
  end
29
34
 
30
35
  class AdminProfile < Profile
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.1.2
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-06-01 00:00:00.000000000 Z
11
+ date: 2014-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord