hexx 5.4.0 → 6.0.0

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.
@@ -1,7 +1,9 @@
1
1
  module Hexx
2
2
  class Service
3
3
 
4
- # Wraps the service object and grants access to its private methods.
4
+ # @api hide
5
+ # Service object decorator that grants access to its private methods
6
+ # whose names begins from given prefix.
5
7
  #
6
8
  # @example
7
9
  # class GetItem < Hexx::Service
@@ -19,17 +21,14 @@ module Hexx
19
21
  class WithCallbacks
20
22
 
21
23
  # @!scope class
22
- # @!method new(object, options)
23
- # Initializes the decorator of given object.
24
+ # @!method new(object, prefix: nil)
25
+ # Constructs the decorator.
24
26
  #
25
27
  # @example (see Hexx::Service::WithCallbacks)
26
28
  #
27
29
  # @param [Hexx::Service] object The service object to be decorated.
28
- # @param [Hash] options The list of wrapper options.
29
- # @option options [Symbol] :prefix (nil) The prefix for private methods
30
+ # @param [Symbol, nil] prefix (nil) The prefix for private methods
30
31
  # to be accessible.
31
-
32
- # @api hide
33
32
  def initialize(object, prefix: nil)
34
33
  @object = object
35
34
  @prefix = Regexp.new(prefix ? "^#{ prefix }_" : "")
data/lib/hexx/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  module Hexx
3
3
 
4
4
  # Current release.
5
- VERSION = "5.4.0"
5
+ VERSION = "6.0.0"
6
6
  end
@@ -0,0 +1,56 @@
1
+ require "spec_helper"
2
+
3
+ module Hexx
4
+ describe Coercible do
5
+
6
+ # ==========================================================================
7
+ # Prepare environment
8
+ # ==========================================================================
9
+
10
+ before { class TestModel; extend Coercible; end }
11
+ before { module TestModule; end }
12
+ before { class TestCoersion < Struct.new(:string); end }
13
+
14
+ let(:test_model) { TestModel }
15
+ let(:test_module) { TestModule }
16
+ let(:test_coersion) { TestCoersion }
17
+
18
+ after { Hexx.send :remove_const, :TestCoersion }
19
+ after { Hexx.send :remove_const, :TestModule }
20
+ after { Hexx.send :remove_const, :TestModel }
21
+
22
+ # ==========================================================================
23
+ # Run tests
24
+ # ==========================================================================
25
+
26
+ describe ".coersion" do
27
+
28
+ it "is defined" do
29
+ expect(test_model).to respond_to :coersion
30
+ end
31
+
32
+ it "is set by default" do
33
+ expect(test_model.coersion).to be_kind_of Module
34
+ end
35
+ end
36
+
37
+ describe ".coersion=" do
38
+
39
+ it "sets the .coersion" do
40
+ expect { test_model.coersion = test_module }
41
+ .to change { test_model.coersion }.to test_module
42
+ end
43
+ end
44
+
45
+ describe ".attr_coerced" do
46
+
47
+ before { test_model.send :attr_coerced, :name, type: test_coersion }
48
+ subject { test_model.new }
49
+
50
+ it "coerces an attribute with given type" do
51
+ subject.name = "some name"
52
+ expect(subject.name).to be_kind_of test_coersion
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,11 +1,12 @@
1
1
  require "spec_helper"
2
2
 
3
3
  module Hexx
4
- describe Dependencies do
4
+ describe Configurable do
5
5
 
6
6
  # Mocks gem core module to test dependencies from
7
- before { module Test; extend Dependencies; end }
7
+ before { module Test; extend Configurable; end }
8
8
  after { Hexx.send :remove_const, :Test }
9
+
9
10
  let(:described_module) { Test }
10
11
 
11
12
  describe ".configure" do
@@ -69,33 +70,19 @@ module Hexx
69
70
 
70
71
  before { described_module.depends_on :item }
71
72
 
72
- it "allows setting string and constantizes it" do
73
- described_module.item = "String"
74
- expect(described_module.item).to eq String
75
- end
76
-
77
- it "allows setting symbol and constantizes it" do
78
- described_module.item = :String
79
- expect(described_module.item).to eq String
80
- end
81
-
82
- it "allows setting constant" do
73
+ it "makes a setter to allow modules" do
83
74
  described_module.item = String
84
75
  expect(described_module.item).to eq String
85
76
  end
86
77
 
87
- it "fails if wrong value type given" do
88
- expect { described_module.item = 1 }.to raise_error TypeError
89
- end
90
-
91
- it "fails if blank string given" do
92
- expect { described_module.item = "" }.to raise_error ArgumentError
78
+ it "makes a setter to require modules" do
79
+ expect { described_module.item = :String }.to raise_error TypeError
93
80
  end
94
81
  end
95
82
 
96
83
  context ":item, default: Hexx::Service" do
97
84
 
98
- before { described_module.depends_on :item, default: "Hexx::Service" }
85
+ before { described_module.depends_on :item, default: Hexx::Service }
99
86
 
100
87
  it "sets default value for the item" do
101
88
  expect(described_module.item).to eq Hexx::Service
@@ -0,0 +1,125 @@
1
+ require "spec_helper"
2
+
3
+ module Hexx
4
+ describe Dependable do
5
+
6
+ # ==========================================================================
7
+ # Prepare environment
8
+ # ==========================================================================
9
+
10
+ before { class Test; extend Dependable; end }
11
+ before { module TestModule; end }
12
+ before { class TestClass; end }
13
+
14
+ let(:described_class) { Test }
15
+ let(:test_module) { TestModule }
16
+ let(:test_class) { TestModule }
17
+
18
+ after { Hexx.send :remove_const, :TestClass }
19
+ after { Hexx.send :remove_const, :TestModule }
20
+ after { Hexx.send :remove_const, :Test }
21
+
22
+ # ==========================================================================
23
+ # Run tests
24
+ # ==========================================================================
25
+
26
+ describe ".depends_on" do
27
+
28
+ it "is private" do
29
+ expect { described_class.depends_on :item }.to raise_error
30
+ expect { described_class.send :depends_on, :item }.not_to raise_error
31
+ end
32
+
33
+ it "accepts string argument" do
34
+ expect { described_class.send :depends_on, "item" }.not_to raise_error
35
+ end
36
+
37
+ it "requires non-empty argument" do
38
+ expect { described_class.send :depends_on, "" }
39
+ .to raise_error(ArgumentError)
40
+ end
41
+
42
+ it "requires string or symbol" do
43
+ expect { described_class.send :depends_on, 1 }
44
+ .to raise_error(TypeError)
45
+ end
46
+
47
+ it "accepts the :default option with class value" do
48
+ expect { described_class.send :depends_on, :item, default: test_class }
49
+ .not_to raise_error
50
+ end
51
+
52
+ it "accepts the :default option with module value" do
53
+ expect { described_class.send :depends_on, :item, default: test_module }
54
+ .not_to raise_error
55
+ end
56
+
57
+ it "accepts the :default option with nil" do
58
+ expect { described_class.send :depends_on, :item, default: nil }
59
+ .not_to raise_error
60
+ end
61
+
62
+ it "requires the :default option to be nil, class or module" do
63
+ expect { described_class.send :depends_on, :item, default: 1 }
64
+ .to raise_error { TypeError }
65
+ end
66
+
67
+ context "with a default implementation" do
68
+
69
+ before { described_class.send :depends_on, :item, default: test_class }
70
+ subject { described_class.new }
71
+
72
+ it "declares a getter" do
73
+ expect(subject).to respond_to :item
74
+ end
75
+
76
+ it "declares a setter" do
77
+ expect(subject).to respond_to :item=
78
+ expect { subject.item = described_class }
79
+ .to change { subject.item }.to described_class
80
+ end
81
+
82
+ it "makes a setter to require module or class" do
83
+ expect { subject.item = 1 }.to raise_error { TypeError }
84
+ end
85
+
86
+ it "sets implementation by default" do
87
+ expect(subject.item).to eq test_class
88
+ end
89
+
90
+ it "resets implementation to default" do
91
+ expect { subject.item = nil }.not_to change { subject.item }
92
+ end
93
+ end
94
+
95
+ context "without a default implementation" do
96
+
97
+ before { described_class.send :depends_on, :item }
98
+ subject { described_class.new }
99
+
100
+ it "declares a getter" do
101
+ expect(subject).to respond_to :item
102
+ end
103
+
104
+ it "declares a setter" do
105
+ expect(subject).to respond_to :item=
106
+ subject.item = test_class
107
+ expect(subject.item).to be test_class
108
+ end
109
+
110
+ it "makes a setter to require module or class" do
111
+ expect { subject.item = 1 }.to raise_error(TypeError)
112
+ end
113
+
114
+ it "fails before the dependency not implemented" do
115
+ expect { subject.item }.to raise_error(NotImplementedError)
116
+ end
117
+
118
+ it "fails when the dependency implementation resets" do
119
+ subject.item = test_class
120
+ expect { subject.item = nil }.to raise_error(NotImplementedError)
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -12,26 +12,34 @@ module Hexx
12
12
  Hexx.send :remove_const, :Test
13
13
  end
14
14
 
15
+ let!(:described_class) { Test }
16
+
17
+ it "extends Hexx::Dependable" do
18
+ expect(described_class).to be_kind_of Hexx::Dependable
19
+ end
20
+
15
21
  it "includes ActiveModel::Validations" do
16
- expect(Service.ancestors).to be_include ActiveModel::Validations
22
+ expect(subject).to be_kind_of ActiveModel::Validations
17
23
  end
18
24
 
19
25
  it "includes Whisper::Publisher" do
20
- expect(Service.ancestors).to be_include Wisper::Publisher
26
+ expect(subject).to be_kind_of Wisper::Publisher
21
27
  end
22
28
 
23
29
  describe ".params" do
24
30
 
25
31
  it "returns an array" do
26
- expect(Test.params).to be_kind_of Array
32
+ expect(described_class.params).to be_kind_of Array
27
33
  end
28
34
  end
29
35
 
30
36
  describe ".new" do
31
37
 
32
38
  let(:value) { "text" }
33
- before { Test.send :allow_params, :name }
34
- subject { Test.new(name: value, wrong: "wrong").with_callbacks }
39
+ before { described_class.send :allow_params, :name }
40
+ subject do
41
+ described_class.new(name: value, wrong: "wrong").with_callbacks
42
+ end
35
43
 
36
44
  it "stringifies params' keys" do
37
45
  expect(subject.params["name"]).to eq value
@@ -57,7 +65,7 @@ module Hexx
57
65
 
58
66
  context "without a prefix" do
59
67
 
60
- subject { Test.new.with_callbacks }
68
+ subject { described_class.new.with_callbacks }
61
69
 
62
70
  it "makes all private methods public" do
63
71
  expect(subject.respond_to? :something).to be_truthy
@@ -69,7 +77,7 @@ module Hexx
69
77
 
70
78
  context "with a prefix" do
71
79
 
72
- subject { Test.new.with_callbacks prefix: :on }
80
+ subject { described_class.new.with_callbacks prefix: :on }
73
81
 
74
82
  it "makes prefixed private methods public" do
75
83
  expect(subject.respond_to? :on_something).to be_truthy
@@ -111,15 +119,15 @@ module Hexx
111
119
 
112
120
  describe ".allow_params" do
113
121
 
114
- before { Test.send :allow_params, :name }
122
+ before { described_class.send :allow_params, :name }
115
123
 
116
124
  it "sets class params" do
117
- expect(Test.params).to eq %w(name)
125
+ expect(described_class.params).to eq %w(name)
118
126
  end
119
127
 
120
128
  it "defines instance attributes" do
121
- expect(Test.instance_methods).to be_include :name
122
- expect(Test.instance_methods).to be_include :name=
129
+ expect(described_class.instance_methods).to be_include :name
130
+ expect(described_class.instance_methods).to be_include :name=
123
131
  end
124
132
  end
125
133
 
@@ -232,7 +240,7 @@ module Hexx
232
240
  describe "#run_service" do
233
241
 
234
242
  let!(:other) { double "other", subscribe: nil, run: nil }
235
- before { allow(Test).to receive(:new).and_return other }
243
+ before { allow(described_class).to receive(:new).and_return other }
236
244
 
237
245
  it "if wrong class given it fails with TypeError" do
238
246
  expect { subject.run_service String, :on_string }
@@ -241,8 +249,8 @@ module Hexx
241
249
 
242
250
  it "creates a service object" do
243
251
  options = { "name" => "some name" }
244
- expect(Test).to receive(:new).with options
245
- subject.run_service Test, :on_service, options
252
+ expect(described_class).to receive(:new).with options
253
+ subject.run_service described_class, :on_service, options
246
254
  end
247
255
 
248
256
  it "subscribes self for the service notifications" do
@@ -250,13 +258,13 @@ module Hexx
250
258
  expect(listener).to eq subject.with_callbacks
251
259
  expect(params).to eq(prefix: :on_service)
252
260
  end
253
- subject.run_service Test, :on_service
261
+ subject.run_service described_class, :on_service
254
262
  end
255
263
 
256
264
  it "runs the service object after subscriptions" do
257
265
  expect(other).to receive(:subscribe).ordered
258
266
  expect(other).to receive(:run).ordered
259
- subject.run_service Test, :on_service
267
+ subject.run_service described_class, :on_service
260
268
  end
261
269
  end
262
270
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hexx
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.4.0
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kozin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-23 00:00:00.000000000 Z
11
+ date: 2014-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -149,18 +149,23 @@ files:
149
149
  - README.rdoc
150
150
  - Rakefile
151
151
  - lib/hexx.rb
152
- - lib/hexx/dependencies.rb
153
- - lib/hexx/models.rb
154
- - lib/hexx/models/base_coercer.rb
152
+ - lib/hexx/coercible.rb
153
+ - lib/hexx/configurable.rb
154
+ - lib/hexx/dependable.rb
155
+ - lib/hexx/helpers/base.rb
156
+ - lib/hexx/helpers/coersion.rb
157
+ - lib/hexx/helpers/dependency.rb
158
+ - lib/hexx/helpers/module_dependency.rb
159
+ - lib/hexx/helpers/parameter.rb
155
160
  - lib/hexx/null.rb
156
161
  - lib/hexx/service.rb
157
162
  - lib/hexx/service/invalid.rb
158
163
  - lib/hexx/service/message.rb
159
- - lib/hexx/service/parameters.rb
160
164
  - lib/hexx/service/with_callbacks.rb
161
165
  - lib/hexx/version.rb
162
- - spec/hexx/dependencies_spec.rb
163
- - spec/hexx/models_spec.rb
166
+ - spec/hexx/coercible_spec.rb
167
+ - spec/hexx/configurable_spec.rb
168
+ - spec/hexx/dependable_spec.rb
164
169
  - spec/hexx/null_spec.rb
165
170
  - spec/hexx/service/invalid_spec.rb
166
171
  - spec/hexx/service/message_spec.rb
@@ -198,11 +203,12 @@ summary: Service objects for Rails.
198
203
  test_files:
199
204
  - spec/spec_helper.rb
200
205
  - spec/hexx/service_spec.rb
201
- - spec/hexx/dependencies_spec.rb
202
206
  - spec/hexx/null_spec.rb
203
207
  - spec/hexx/service/message_spec.rb
204
208
  - spec/hexx/service/invalid_spec.rb
205
- - spec/hexx/models_spec.rb
209
+ - spec/hexx/coercible_spec.rb
210
+ - spec/hexx/dependable_spec.rb
211
+ - spec/hexx/configurable_spec.rb
206
212
  - spec/initializers/garbage_collection.rb
207
213
  - spec/initializers/i18n.rb
208
214
  - spec/initializers/focus.rb