storext 0.1.3 → 0.1.4

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: e1d2b7891fc2026bb0689cd6986f65c4a10f4eba
4
- data.tar.gz: e498daeb28e562bbcbecb3c24e7589268f66aca2
3
+ metadata.gz: a63def211664fda8bf9e6551e1cdcf632ae0c557
4
+ data.tar.gz: d270b2a34ffe9cf9969480bb23398b6fb5e35da6
5
5
  SHA512:
6
- metadata.gz: 68807df855f69de632f74b962b2a246a7cc0c65fcd73456819a446acca19e583c50d6abacc988d35643681f839373ecfea7650978e95c52c48d84cd9ef1c921b
7
- data.tar.gz: 7d86b66e517338408c6317aa9ae384521d88d079df715b246dcc8c2fcee5bbdd7318c0a82633168677b9a5867050706b2fcb736c385a6ea7dfce2ebcd37467d0
6
+ metadata.gz: 8b6af2eb762d8319ae340f7839ffb2a118f11dab52bf18438450358fe6f6b51c8c566b350673b8267f7333f76537de2e4f0c417965533bd45b9bca96103306bd
7
+ data.tar.gz: 4df6dec32afed9fc67480894f23c3a4d8b8f8c269f7bce7eb0fac30203c7aa06134d59b29668520807e5d309d3f3cc30fc22243615c5e7b620fb3073cd73f8dd
data/lib/storext.rb CHANGED
@@ -2,20 +2,18 @@ require "active_support/concern"
2
2
  require "virtus"
3
3
  require "storext/attribute_proxy"
4
4
  require "storext/class_methods"
5
+ require "storext/instance_methods"
5
6
 
6
7
  module Storext
7
8
 
8
9
  extend ActiveSupport::Concern
9
10
 
10
11
  included do
12
+ include InstanceMethods
13
+
11
14
  class_attribute :store_attribute_defs
12
15
  self.store_attribute_defs = {}
13
16
 
14
- class_attribute :storext_cast_proxy_class
15
- self.storext_cast_proxy_class = Class.new do
16
- include Virtus.model
17
- end
18
-
19
17
  unless defined?(self::Boolean)
20
18
  self::Boolean = ::Axiom::Types::Boolean
21
19
  end
@@ -23,32 +21,4 @@ module Storext
23
21
  after_initialize :set_storext_defaults
24
22
  end
25
23
 
26
-
27
- private
28
-
29
- def set_storext_defaults
30
- store_attribute_defs.each do |store_column, store_keys|
31
- store_keys.each do |store_key|
32
- set_storext_default_for(store_column, store_key)
33
- end
34
- end
35
- end
36
-
37
- def set_storext_default_for(column, key)
38
- if self.send(column).nil? || !self.send(column).has_key?(key.to_s)
39
- write_store_attribute(column, key, default_store_value(key))
40
- end
41
- end
42
-
43
- def default_store_value(key)
44
- storext_cast_proxy.send("_casted_#{key}")
45
- end
46
-
47
- def storext_cast_proxy
48
- if @storext_cast_proxy
49
- return @storext_cast_proxy
50
- end
51
- @storext_cast_proxy ||= self.class.storext_cast_proxy_class.new
52
- end
53
-
54
24
  end
@@ -3,10 +3,10 @@ module Storext
3
3
 
4
4
  def storext_define_writer(column, attr)
5
5
  define_method "#{attr}=" do |value|
6
- storext_cast_proxy.send("_casted_#{attr}=", value)
6
+ storext_cast_proxy(attr).send("casted_attr=", value)
7
7
  send("#{column}=", send(column) || {})
8
8
 
9
- attr_value = storext_cast_proxy.send("_casted_#{attr}")
9
+ attr_value = storext_cast_proxy(attr).send("casted_attr")
10
10
  write_store_attribute column, attr, value
11
11
  send(column)[attr.to_s] = value
12
12
  end
@@ -16,33 +16,54 @@ module Storext
16
16
  define_method attr do
17
17
  if send(column) && send(column).has_key?(attr.to_s)
18
18
  store_val = read_store_attribute(column, attr)
19
- storext_cast_proxy.send("_casted_#{attr}=", store_val)
19
+ storext_cast_proxy(attr).send("casted_attr=", store_val)
20
20
  end
21
- storext_cast_proxy.send("_casted_#{attr}")
21
+ storext_cast_proxy(attr).send("casted_attr")
22
22
  end
23
23
  end
24
24
 
25
25
  def store_attribute(column, attr, type=nil, opts={})
26
- track_store_attribute(column, attr)
27
-
28
- storext_cast_proxy_class.attribute "_casted_#{attr}", type, opts
29
- unless storext_cast_proxy_class.instance_methods.include? :"_casted_#{attr}"
30
- raise ArgumentError, "problem defining `#{attr}`. `#{type}` may not be a valid type."
31
- end
26
+ track_store_attribute(column, attr, type, opts)
27
+ storext_check_attr_validity(attr, type, opts)
28
+ storext_define_accessor(column, attr)
29
+ store_accessor column, *storext_attrs_for(column)
30
+ end
32
31
 
32
+ def storext_define_accessor(column, attr)
33
33
  storext_define_writer(column, attr)
34
34
  storext_define_reader(column, attr)
35
-
36
- store_accessor column, *store_attribute_defs[column]
37
35
  end
38
36
 
39
37
  def store_attributes(column, &block)
40
38
  AttributeProxy.new(self, column, &block).define_store_attribute
41
39
  end
42
40
 
43
- def track_store_attribute(column, attr)
44
- store_attribute_defs[column] ||= []
45
- store_attribute_defs[column] << attr
41
+ private
42
+
43
+ def storext_attrs_for(column)
44
+ attrs = []
45
+ store_attribute_defs.each do |attr, definition|
46
+ attrs << attr if definition[:column] == column
47
+ end
48
+ attrs
49
+ end
50
+
51
+ def track_store_attribute(column, attr, type, opts)
52
+ self.store_attribute_defs = self.store_attribute_defs.dup
53
+
54
+ store_attribute_defs[attr] = {
55
+ column: column,
56
+ type: type,
57
+ opts: opts,
58
+ }
59
+ end
60
+
61
+ def storext_check_attr_validity(attr, type, opts)
62
+ storext_cast_proxy_class = Class.new { include Virtus.model }
63
+ storext_cast_proxy_class.attribute "casted_attr", type, opts
64
+ unless storext_cast_proxy_class.instance_methods.include? :"casted_attr"
65
+ raise ArgumentError, "problem defining `#{attr}`. `#{type}` may not be a valid type."
66
+ end
46
67
  end
47
68
 
48
69
  end
@@ -0,0 +1,43 @@
1
+ module Storext
2
+ module InstanceMethods
3
+
4
+ private
5
+
6
+ def set_storext_defaults
7
+ store_attribute_defs.each do |attr, definition|
8
+ set_storext_default_for(definition[:column], attr)
9
+ end
10
+ end
11
+
12
+ def set_storext_default_for(column, attr)
13
+ if self.send(column).nil? || !self.send(column).has_key?(attr.to_s)
14
+ write_store_attribute(column, attr, default_store_value(attr))
15
+ end
16
+ end
17
+
18
+ def default_store_value(attr)
19
+ storext_cast_proxy(attr).send("casted_attr")
20
+ end
21
+
22
+ def storext_cast_proxy(attr)
23
+ if @storext_cast_proxies && @storext_cast_proxies[attr]
24
+ return @storext_cast_proxies[attr]
25
+ else
26
+ @storext_cast_proxies ||= {}
27
+
28
+ klass = Class.new do
29
+ include Virtus.model
30
+ end
31
+
32
+ klass.attribute(
33
+ "casted_attr",
34
+ self.class.store_attribute_defs[attr][:type],
35
+ self.class.store_attribute_defs[attr][:opts],
36
+ )
37
+
38
+ @storext_cast_proxies[attr] = klass.new
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module Storext
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - G5
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-12-01 00:00:00.000000000 Z
13
+ date: 2015-01-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -96,6 +96,7 @@ files:
96
96
  - lib/storext.rb
97
97
  - lib/storext/attribute_proxy.rb
98
98
  - lib/storext/class_methods.rb
99
+ - lib/storext/instance_methods.rb
99
100
  - lib/storext/version.rb
100
101
  - lib/tasks/storext_tasks.rake
101
102
  homepage: http://github.com/g5/storext
@@ -118,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
119
  version: '0'
119
120
  requirements: []
120
121
  rubyforge_project:
121
- rubygems_version: 2.2.2
122
+ rubygems_version: 2.4.3
122
123
  signing_key:
123
124
  specification_version: 4
124
125
  summary: Extends ActiveRecord::Store.store_accessor