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 +4 -4
- data/lib/storext.rb +3 -33
- data/lib/storext/class_methods.rb +36 -15
- data/lib/storext/instance_methods.rb +43 -0
- data/lib/storext/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a63def211664fda8bf9e6551e1cdcf632ae0c557
|
4
|
+
data.tar.gz: d270b2a34ffe9cf9969480bb23398b6fb5e35da6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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("
|
6
|
+
storext_cast_proxy(attr).send("casted_attr=", value)
|
7
7
|
send("#{column}=", send(column) || {})
|
8
8
|
|
9
|
-
attr_value = storext_cast_proxy.send("
|
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("
|
19
|
+
storext_cast_proxy(attr).send("casted_attr=", store_val)
|
20
20
|
end
|
21
|
-
storext_cast_proxy.send("
|
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
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
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
|
data/lib/storext/version.rb
CHANGED
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.
|
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:
|
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.
|
122
|
+
rubygems_version: 2.4.3
|
122
123
|
signing_key:
|
123
124
|
specification_version: 4
|
124
125
|
summary: Extends ActiveRecord::Store.store_accessor
|