storext 1.1.0 → 1.1.1

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b724caadf1e54b22c8cab75f126182a1968a7314
4
- data.tar.gz: 221fc6202180a8c0c3ac3519b5811d4dc930fc29
3
+ metadata.gz: 654370a34da37189fcb61310813057df6646a51a
4
+ data.tar.gz: bbad9a052325b64305c563b2ac9001bea85137e0
5
5
  SHA512:
6
- metadata.gz: 7b1dfb86dee5422517a5704ae0c5a0c58a29f5944994bec4855f64647761a945887a3897549fbe77433c8c2f1a0be8aa97aebc4f38ef96678da107b9dd5d6932
7
- data.tar.gz: 5e40101f5840f73c08b09831ff9e7386eb08a1a2bbd2402d2fff8a6edd51303d29fcaeda5295d4b3537b4c48c975f81312c4759c8720930a9a91f684c28f39a1
6
+ metadata.gz: b8b83a87d44483d37e0824c5044a2ce04ac925269b3dcf111d5f2ed864691cd88ce2903d1f431ba3ae22d8ab8bf05d5c52127b28d2ab43ea7e88514fba93261b
7
+ data.tar.gz: 77ddc077b8d53e6594ad5586534f7b2054a799e3b69eb0de3cabf67e21ba25b42d03998e15ba29cca3fd6cebe7d4487a5d9f8dee65c764428fa8a659804052a8
@@ -1,4 +1,5 @@
1
1
  require "active_support/concern"
2
+ require "active_support/core_ext/module/attribute_accessors"
2
3
  require "virtus"
3
4
  require "storext/attribute_proxy"
4
5
  require "storext/class_methods"
@@ -6,6 +7,9 @@ require "storext/instance_methods"
6
7
 
7
8
  module Storext
8
9
 
10
+ mattr_accessor :proxy_classes
11
+ self.proxy_classes ||= {}
12
+
9
13
  def self.model(options={})
10
14
  mod = Module.new do
11
15
  mattr_accessor :storext_options
@@ -3,10 +3,7 @@ module Storext
3
3
 
4
4
  def storext_define_writer(column, attr)
5
5
  define_method "#{attr}=" do |value|
6
- storext_cast_proxy(attr).send("#{attr}=", value)
7
6
  send("#{column}=", send(column) || {})
8
-
9
- attr_value = storext_cast_proxy(attr).send(attr)
10
7
  write_store_attribute column, attr, value
11
8
  send(column)[attr.to_s] = value
12
9
  end
@@ -16,9 +13,9 @@ module Storext
16
13
  define_method attr do
17
14
  if send(column) && send(column).has_key?(attr.to_s)
18
15
  store_val = read_store_attribute(column, attr)
19
- storext_cast_proxy(attr).send("#{attr}=", store_val)
16
+ storext_cast_proxy.send("#{attr}=", store_val)
20
17
  end
21
- storext_cast_proxy(attr).send("#{attr}")
18
+ storext_cast_proxy.send("#{attr}")
22
19
  end
23
20
  end
24
21
 
@@ -32,12 +29,43 @@ module Storext
32
29
  def storext_define_accessor(column, attr)
33
30
  storext_define_writer(column, attr)
34
31
  storext_define_reader(column, attr)
32
+ storext_define_proxy_attribute(attr)
35
33
  end
36
34
 
37
35
  def store_attributes(column, &block)
38
36
  AttributeProxy.new(self, column, &block).define_store_attribute
39
37
  end
40
38
 
39
+ def storext_define_proxy_attribute(attr)
40
+ compute_default_method_name = :"compute_default_#{attr}"
41
+ definition = self.storext_definitions[attr]
42
+ proxy_class = self.storext_proxy_class
43
+ proxy_class.attribute(
44
+ attr,
45
+ definition[:type],
46
+ definition[:opts].merge(default: compute_default_method_name),
47
+ )
48
+
49
+ proxy_class.send :define_method, compute_default_method_name do
50
+ default_value = definition[:opts][:default]
51
+ if default_value.is_a?(Symbol)
52
+ source.send(default_value)
53
+ elsif default_value.respond_to?(:call)
54
+ attribute = self.class.attribute_set[attr]
55
+ default_value.call(source, attribute)
56
+ else
57
+ default_value
58
+ end
59
+ end
60
+ end
61
+
62
+ def storext_proxy_class
63
+ Storext.proxy_classes[self] ||= Class.new(*Storext.proxy_classes[self.superclass]) do
64
+ include Virtus.model
65
+ attribute :source
66
+ end
67
+ end
68
+
41
69
  private
42
70
 
43
71
  def storext_attrs_for(column)
@@ -42,48 +42,11 @@ module Storext
42
42
  end
43
43
 
44
44
  def default_store_value(attr)
45
- storext_cast_proxy(attr).send(attr)
45
+ storext_cast_proxy.send(attr)
46
46
  end
47
47
 
48
- def storext_cast_proxy(attr)
49
- if @storext_cast_proxies && @storext_cast_proxies[attr]
50
- return @storext_cast_proxies[attr]
51
- else
52
- @storext_cast_proxies ||= {}
53
-
54
- definition = self.class.storext_definitions[attr]
55
- klass = storext_create_proxy_class(attr, definition)
56
-
57
- @storext_cast_proxies[attr] = klass.new(source: self)
58
- end
59
- end
60
-
61
- def storext_create_proxy_class(attr, definition)
62
- klass = Class.new do
63
- include Virtus.model
64
-
65
- attribute :source
66
- end
67
-
68
- klass.attribute(
69
- attr,
70
- definition[:type],
71
- definition[:opts].merge(default: :compute_default),
72
- )
73
-
74
- klass.send :define_method, :compute_default do
75
- default_value = definition[:opts][:default]
76
- if default_value.is_a?(Symbol)
77
- source.send(default_value)
78
- elsif default_value.respond_to?(:call)
79
- attribute = self.class.attribute_set[attr]
80
- default_value.call(source, attribute)
81
- else
82
- default_value
83
- end
84
- end
85
-
86
- klass
48
+ def storext_cast_proxy
49
+ @storext_cast_proxy ||= self.class.storext_proxy_class.new(source: self)
87
50
  end
88
51
 
89
52
  end
@@ -1,3 +1,3 @@
1
1
  module Storext
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
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: 1.1.0
4
+ version: 1.1.1
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: 2015-06-26 00:00:00.000000000 Z
13
+ date: 2015-07-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
- rubygems_version: 2.4.5
122
+ rubygems_version: 2.4.6
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: Extends ActiveRecord::Store.store_accessor