storext 1.0.2 → 1.0.3

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: 6e8a8c0e422de600c714360df2977eea1c317b21
4
- data.tar.gz: 017ed53d2d1fb0cd445db1543c052b29e9e70eaa
3
+ metadata.gz: 575cadf889b06670fee076e367dbdd98f65dacdd
4
+ data.tar.gz: 9f16efe7dbfae9cd8c5982d84c2e8fc78f53c9bb
5
5
  SHA512:
6
- metadata.gz: eeb083dba050d12e715ddf45a1ab5b184dcefed11c9e183bd252c735e98e5e3366b507d1234132b9cfefd7ea0c24d7e2aaf2695ca5372fe8d438f66a3e87e6b4
7
- data.tar.gz: 49cf8072a4fe6b312b8917daf9a882bf0513863840a680799ad2a4c3f81671f084bba48bff406be97b6e4808e4ab45039050cbd5d62610f12cbaea896bd13efb
6
+ metadata.gz: 095db2aa2daefcd5b1f9202e43a9eccae3a7cd7341aa0ac335457c3560fb84456ffc88b21144fa7cac518cf4c6f044321568af3649783bbfb3f7f8ef9113d148
7
+ data.tar.gz: be9f734f099a4597ef6a8bf4b1862f1484598edb5d563fb6bf9e083cc24eb239098a207c9dfd72a3d0833ee072cfb5aedc9ecd5f4e649c3b8006c6c0637aaf75
@@ -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(attr).send("casted_attr=", value)
6
+ storext_cast_proxy(attr).send("#{attr}=", value)
7
7
  send("#{column}=", send(column) || {})
8
8
 
9
- attr_value = storext_cast_proxy(attr).send("casted_attr")
9
+ attr_value = storext_cast_proxy(attr).send(attr)
10
10
  write_store_attribute column, attr, value
11
11
  send(column)[attr.to_s] = value
12
12
  end
@@ -16,9 +16,9 @@ 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(attr).send("casted_attr=", store_val)
19
+ storext_cast_proxy(attr).send("#{attr}=", store_val)
20
20
  end
21
- storext_cast_proxy(attr).send("casted_attr")
21
+ storext_cast_proxy(attr).send("#{attr}")
22
22
  end
23
23
  end
24
24
 
@@ -60,8 +60,8 @@ module Storext
60
60
 
61
61
  def storext_check_attr_validity(attr, type, opts)
62
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"
63
+ storext_cast_proxy_class.attribute attr, type, opts
64
+ unless storext_cast_proxy_class.instance_methods.include? attr
65
65
  raise ArgumentError, "problem defining `#{attr}`. `#{type}` may not be a valid type."
66
66
  end
67
67
  end
@@ -38,7 +38,7 @@ module Storext
38
38
  end
39
39
 
40
40
  def default_store_value(attr)
41
- storext_cast_proxy(attr).send("casted_attr")
41
+ storext_cast_proxy(attr).send(attr)
42
42
  end
43
43
 
44
44
  def storext_cast_proxy(attr)
@@ -47,18 +47,39 @@ module Storext
47
47
  else
48
48
  @storext_cast_proxies ||= {}
49
49
 
50
- klass = Class.new do
51
- include Virtus.model
52
- end
50
+ definition = self.class.storext_definitions[attr]
51
+ klass = storext_create_proxy_class(attr, definition)
53
52
 
54
- klass.attribute(
55
- "casted_attr",
56
- self.class.storext_definitions[attr][:type],
57
- self.class.storext_definitions[attr][:opts],
58
- )
53
+ @storext_cast_proxies[attr] = klass.new(source: self)
54
+ end
55
+ end
59
56
 
60
- @storext_cast_proxies[attr] = klass.new
57
+ def storext_create_proxy_class(attr, definition)
58
+ klass = Class.new do
59
+ include Virtus.model
60
+
61
+ attribute :source
61
62
  end
63
+
64
+ klass.attribute(
65
+ attr,
66
+ definition[:type],
67
+ definition[:opts].merge(default: :compute_default),
68
+ )
69
+
70
+ klass.send :define_method, :compute_default do
71
+ default_value = definition[:opts][:default]
72
+ if default_value.is_a?(Symbol)
73
+ source.send(default_value)
74
+ elsif default_value.respond_to?(:call)
75
+ attribute = self.class.attribute_set[attr]
76
+ default_value.call(source, attribute)
77
+ else
78
+ default_value
79
+ end
80
+ end
81
+
82
+ klass
62
83
  end
63
84
 
64
85
  end
@@ -1,3 +1,3 @@
1
1
  module Storext
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
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.0.2
4
+ version: 1.0.3
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-04-09 00:00:00.000000000 Z
13
+ date: 2015-04-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails