constructable 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/constructable.gemspec +1 -1
- data/lib/constructable/constructor.rb +6 -2
- data/lib/constructable/core_ext.rb +10 -3
- data/test/constructable/test_constructor.rb +16 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.5
|
data/constructable.gemspec
CHANGED
@@ -5,15 +5,19 @@ module Constructable
|
|
5
5
|
def initialize(klass)
|
6
6
|
@attributes = []
|
7
7
|
@klass = klass
|
8
|
+
self.redefine_new(klass)
|
9
|
+
self.define_attributes_method
|
10
|
+
end
|
11
|
+
|
12
|
+
def redefine_new(klass)
|
8
13
|
constructor = self
|
9
|
-
|
14
|
+
klass.define_singleton_method(:new) do |*args, &block|
|
10
15
|
obj = self.allocate
|
11
16
|
constructor_hash = Hash === args.last ? args.last : {}
|
12
17
|
constructor.construct(constructor_hash, obj)
|
13
18
|
obj.send :initialize, *args, &block
|
14
19
|
obj
|
15
20
|
end
|
16
|
-
self.define_attributes_method
|
17
21
|
end
|
18
22
|
|
19
23
|
|
@@ -34,9 +34,16 @@ module Constructable
|
|
34
34
|
#
|
35
35
|
# @param [Array<[Array<Symbol, Hash>]>] args an array of symbols or arrays: the name of the attribute and it's configuration
|
36
36
|
def constructable(*args)
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
case self
|
38
|
+
when Class
|
39
|
+
@constructor ||= Constructor.new(self)
|
40
|
+
@constructor.define_attributes(args)
|
41
|
+
return nil
|
42
|
+
when Module
|
43
|
+
define_singleton_method :included do |klass|
|
44
|
+
klass.constructable *args
|
45
|
+
end
|
46
|
+
end
|
40
47
|
end
|
41
48
|
end
|
42
49
|
end
|
@@ -44,4 +44,20 @@ describe 'Constructor' do
|
|
44
44
|
assert_equal 'hello', instance.accessible_attribute
|
45
45
|
end
|
46
46
|
end
|
47
|
+
|
48
|
+
describe 'module support' do
|
49
|
+
before do
|
50
|
+
module Foo
|
51
|
+
constructable [:foo, readable: true]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'allows cosntructing classes after including the module' do
|
56
|
+
class Bar
|
57
|
+
include Foo
|
58
|
+
end
|
59
|
+
bar = Bar.new(foo: 'foo')
|
60
|
+
assert_equal 'foo', bar.foo
|
61
|
+
end
|
62
|
+
end
|
47
63
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: constructable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Manuel Korfmann
|
@@ -103,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
103
|
requirements:
|
104
104
|
- - ">="
|
105
105
|
- !ruby/object:Gem::Version
|
106
|
-
hash:
|
106
|
+
hash: 419636223836929306
|
107
107
|
segments:
|
108
108
|
- 0
|
109
109
|
version: "0"
|