dry-core 0.3.0 → 0.3.1

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: 0d90cf172d8f4876fd72b7f471763252a96841ed
4
- data.tar.gz: 9470c4d45a7d5c37401edef71add0927d43dc2d6
3
+ metadata.gz: c6b9aba5ba2645ca6591d2977bf2a0935c155b09
4
+ data.tar.gz: 5df5c9727cc9c1a12c217d159928cafc30dab865
5
5
  SHA512:
6
- metadata.gz: f104b7b4ef55aeb2402e60862300c52fa8095711774f6b6a84c3932c6340a1c6ac10068f4b8d56207e48b24069f074a6ded3e432644d3368bbc9fdb761a5ab98
7
- data.tar.gz: 99364cefdb3d5b6a7b99de4f657831cd77e38157eceba1289eb7264cc189279d8744ffb57fbc724307fc600d168006c356169376d7525467dd0d26d7e8f5415e
6
+ metadata.gz: 45f58100a89f1132d5f9d0e456e7a498b33ee5545f0688828bae2b13762a8825b013d352f624dd6ba1ae22a15aced47c278dd0242df6cef23eea6bb297aa57f4
7
+ data.tar.gz: 35233a2424d7ae2672d74707e0f0c1e333fcf5f7a50c61e574965d6c35a78d64d1c2e7b35cc24f51ff02cfe4fb89e2dcc173dff476ae862f4b9bf0fb87ceb90a
data/.travis.yml CHANGED
@@ -9,18 +9,14 @@ script:
9
9
  - bundle exec rake
10
10
  rvm:
11
11
  - 2.1.10
12
- - 2.2.6
13
- - 2.3.3
12
+ - 2.2.7
13
+ - 2.3.4
14
14
  - 2.4.1
15
- - jruby-9.1.8.0
16
- - rbx-3
15
+ - jruby-9.1.9.0
17
16
  env:
18
17
  global:
19
18
  - COVERAGE=true
20
19
  - JRUBY_OPTS='--dev -J-Xmx1024M'
21
- matrix:
22
- allow_failures:
23
- - rvm: rbx-3
24
20
  notifications:
25
21
  email: false
26
22
  webhooks:
data/CHANGELOG.md CHANGED
@@ -1,10 +1,26 @@
1
+ # v0.3.1 to-be-released
2
+
3
+ ### Added
4
+
5
+ * Support for building classes within an existing namespace (flash-gordon)
6
+
7
+ [Compare v0.3.0...master](https://github.com/dry-rb/dry-core/compare/v0.3.0...master)
8
+
9
+ # v0.3.0 2017-05-05
10
+
11
+ ### Changed
12
+
13
+ * Class attributes are initialized _before_ running the `inherited` hook. It's slightly more convenient behavior and it's very unlikely anyone will be affected by this, but technically this is a breaking change (flash-gordon)
14
+
15
+ [Compare v0.2.4...v0.3.0](https://github.com/dry-rb/dry-core/compare/v0.2.4...v0.3.0)
16
+
1
17
  # v0.2.4 2017-01-26
2
18
 
3
19
  ### Fixed
4
20
 
5
21
  * Do not require deprecated method to be defined (flash-gordon)
6
22
 
7
- [Compare v0.2.3...v0.2.4](https://github.com/dry-rb/dry-core/compare/v0.2.2...v0.2.3)
23
+ [Compare v0.2.3...v0.2.4](https://github.com/dry-rb/dry-core/compare/v0.2.3...v0.2.4)
8
24
 
9
25
  # v0.2.3 2016-12-30
10
26
 
@@ -8,7 +8,7 @@ module Dry
8
8
  module ClassAttributes
9
9
  include Constants
10
10
 
11
- # Specify what attirubtes a class will use
11
+ # Specify what attributes a class will use
12
12
  #
13
13
  # @example
14
14
  # class MyClass
@@ -2,24 +2,52 @@ module Dry
2
2
  module Core
3
3
  # Class for generating more classes
4
4
  class ClassBuilder
5
+ ParentClassMismatch = Class.new(TypeError)
6
+
5
7
  attr_reader :name
6
8
  attr_reader :parent
9
+ attr_reader :namespace
7
10
 
8
- def initialize(name:, parent: Object)
11
+ def initialize(name:, parent: nil, namespace: nil)
9
12
  @name = name
10
- @parent = parent
13
+ @namespace = namespace
14
+ @parent = parent || Object
11
15
  end
12
16
 
13
17
  # Generate a class based on options
14
18
  #
15
- # @example
19
+ # @example Create anonymous class
16
20
  # builder = Dry::Core::ClassBuilder.new(name: 'MyClass')
17
21
  #
18
22
  # klass = builder.call
19
23
  # klass.name # => "MyClass"
20
24
  #
25
+ # @example Create named class
26
+ # builder = Dry::Core::ClassBuilder.new(name: 'User', namespace: Entities)
27
+ #
28
+ # klass = builder.call
29
+ # klass.name # => "Entities::User"
30
+ # klass.superclass.name # => "Entities::User"
31
+ # Entities::User # => "Entities::User"
32
+ # klass.superclass == Entities::User # => true
33
+ #
21
34
  # @return [Class]
22
35
  def call
36
+ klass = if namespace
37
+ create_named
38
+ else
39
+ create_anonymous
40
+ end
41
+
42
+ yield(klass) if block_given?
43
+
44
+ klass
45
+ end
46
+
47
+ private
48
+
49
+ # @api private
50
+ def create_anonymous
23
51
  klass = Class.new(parent)
24
52
  name = self.name
25
53
 
@@ -29,10 +57,44 @@ module Dry
29
57
  alias_method :to_s, :name
30
58
  end
31
59
 
32
- yield(klass) if block_given?
60
+ klass
61
+ end
62
+
63
+ # @api private
64
+ def create_named
65
+ name = self.name
66
+ base = create_base(namespace, name, parent)
67
+ klass = Class.new(base)
68
+
69
+ namespace.module_eval do
70
+ remove_const(name)
71
+ const_set(name, klass)
72
+
73
+ const_get(name).name if RUBY_VERSION < '2.4'
74
+
75
+ remove_const(name)
76
+ const_set(name, base)
77
+ end
33
78
 
34
79
  klass
35
80
  end
81
+
82
+ # @api private
83
+ def create_base(namespace, name, parent)
84
+ if namespace.const_defined?(name, false)
85
+ existing = namespace.const_get(name)
86
+
87
+ unless existing <= parent
88
+ raise ParentClassMismatch, "#{ existing.name } must be a subclass of #{ parent.name }"
89
+ end
90
+
91
+ existing
92
+ else
93
+ klass = Class.new(parent || Object)
94
+ namespace.const_set(name, klass)
95
+ klass
96
+ end
97
+ end
36
98
  end
37
99
  end
38
100
  end
@@ -84,7 +84,7 @@ module Dry
84
84
  end
85
85
  end
86
86
 
87
- # Sets a custom logger. This is a global settings.
87
+ # Sets a custom logger. This is a global setting.
88
88
  #
89
89
  # @option [IO] output output stream for messages
90
90
  def set_logger!(output = nil)
@@ -2,7 +2,7 @@ require 'set'
2
2
 
3
3
  module Dry
4
4
  module Core
5
- # Allows to define extensions that can be later enabled by the user.
5
+ # Define extensions that can be later enabled by the user.
6
6
  #
7
7
  # @example
8
8
  #
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Core
3
- VERSION = '0.3.0'.freeze
3
+ VERSION = '0.3.1'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikita Shilnikov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-05 00:00:00.000000000 Z
11
+ date: 2017-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby