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 CHANGED
@@ -1 +1 @@
1
- 0.2.4
1
+ 0.2.5
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{constructable}
8
- s.version = "0.2.4"
8
+ s.version = "0.2.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Manuel Korfmann"]
@@ -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
- @klass.define_singleton_method(:new) do |*args, &block|
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
- @constructor ||= Constructor.new(self)
38
- @constructor.define_attributes(args)
39
- return nil
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.4
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: -1734599671061646461
106
+ hash: 419636223836929306
107
107
  segments:
108
108
  - 0
109
109
  version: "0"