hexx 6.0.2 → 6.0.3
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.
- checksums.yaml +4 -4
- data/README.rdoc +1 -1
- data/lib/hexx/helpers/base.rb +0 -4
- data/lib/hexx/helpers/parameter.rb +2 -2
- data/lib/hexx/service.rb +41 -26
- data/lib/hexx/service/with_callbacks.rb +2 -4
- data/lib/hexx/version.rb +1 -1
- data/spec/hexx/coercible_spec.rb +40 -5
- data/spec/hexx/null_spec.rb +2 -1
- data/spec/hexx/service_spec.rb +63 -44
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 861d8373aad89fc2f9409572c87782223dd042e4
|
4
|
+
data.tar.gz: 40061fbf81bdf2ff8f57679e328d71c944260a71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 509e8e294856e4f6509c0d68a188533f33646152b1567114d60943fe84e3ab98ec5d21d55aad33e8a3c38706fd875fef57ff0eb9e37605727e3f0d1d3f081896
|
7
|
+
data.tar.gz: b3f38e5997f22130d83050d1a137110424a4dfee5d7544311f0ec75c9fb8524accb0566bc8d99fe2f780f06443b9f915bab6b845fd55c27888c550873c354015
|
data/README.rdoc
CHANGED
@@ -258,7 +258,7 @@ The class implements the {Null object}[http://robots.thoughtbot.com/rails-refact
|
|
258
258
|
+to_i+, +to_f+, +to_c+, +to_r+, +to_nil+
|
259
259
|
* responds with +self+ to any other method call
|
260
260
|
|
261
|
-
Providing {this problem}[http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/], use double
|
261
|
+
Providing {this problem}[http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/], use double negation in logical expressions:
|
262
262
|
|
263
263
|
# Though:
|
264
264
|
Hexx::Null && true # => true
|
data/lib/hexx/helpers/base.rb
CHANGED
@@ -28,12 +28,12 @@ module Hexx
|
|
28
28
|
|
29
29
|
# @api hide
|
30
30
|
def setter
|
31
|
-
"def #{ name }; params[\"#{ name }\"]; end"
|
31
|
+
"private def #{ name }; params[\"#{ name }\"]; end"
|
32
32
|
end
|
33
33
|
|
34
34
|
# @api hide
|
35
35
|
def getter
|
36
|
-
"def #{ name }=(value); params[\"#{ name }\"] = value; end"
|
36
|
+
"private def #{ name }=(value); params[\"#{ name }\"] = value; end"
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
data/lib/hexx/service.rb
CHANGED
@@ -24,22 +24,39 @@ module Hexx
|
|
24
24
|
include Wisper::Publisher
|
25
25
|
include ActiveModel::Validations
|
26
26
|
|
27
|
-
#
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
27
|
+
# Class helper methods
|
28
|
+
class << self
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# @api hide
|
33
|
+
# Returns the list of allowed parameters for service objects.
|
34
|
+
#
|
35
|
+
# The parameters are added to the list by the {.allow_params} private
|
36
|
+
# helper method.
|
37
|
+
#
|
38
|
+
# @example
|
39
|
+
# class Service < Hexx::Service
|
40
|
+
# allow_params :name
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# Service.params # => "name"
|
44
|
+
#
|
45
|
+
# @return [Array<String>] Whitelist of parameters.
|
46
|
+
def params
|
47
|
+
@params ||= []
|
48
|
+
end
|
49
|
+
|
50
|
+
# Sets a list of allowed parameters for the class constructor and
|
51
|
+
# defines the corresponding instance attributes.
|
52
|
+
#
|
53
|
+
# @example (see Hexx::Service::Parameters.params)
|
54
|
+
# @param [Array<Symbol, String>] keys The list of allowed parameters.
|
55
|
+
def allow_params(*keys)
|
56
|
+
@params = keys.flatten.map(&:to_s)
|
57
|
+
fail ArgumentError if @params == []
|
58
|
+
params.each { |name| Helpers::Parameter.add self, name }
|
59
|
+
end
|
43
60
|
end
|
44
61
|
|
45
62
|
# @!scope class
|
@@ -72,7 +89,7 @@ module Hexx
|
|
72
89
|
# @param (see Hexx::Service.new)
|
73
90
|
# @return (see Hexx::Service.new)
|
74
91
|
def initialize(params = {})
|
75
|
-
@params = params.dup.stringify_keys.slice(*
|
92
|
+
@params = params.dup.stringify_keys.slice(*class_params)
|
76
93
|
@messages = []
|
77
94
|
end
|
78
95
|
|
@@ -111,7 +128,7 @@ module Hexx
|
|
111
128
|
#
|
112
129
|
# @return [Array<Hexx::Service::Message>] The array of messages.
|
113
130
|
|
114
|
-
attr_reader :
|
131
|
+
attr_reader :messages
|
115
132
|
|
116
133
|
# @!scope class
|
117
134
|
# @!visibility private
|
@@ -175,14 +192,12 @@ module Hexx
|
|
175
192
|
|
176
193
|
private
|
177
194
|
|
178
|
-
|
179
|
-
|
180
|
-
#
|
181
|
-
# @
|
182
|
-
|
183
|
-
|
184
|
-
@params = keys.map(&:to_s)
|
185
|
-
params.each { |name| Helpers::Parameter.add self, name }
|
195
|
+
attr_reader :params
|
196
|
+
|
197
|
+
# @api hide
|
198
|
+
# @return [Array<String>] Whitelist of parameters.
|
199
|
+
def class_params
|
200
|
+
self.class.send :params
|
186
201
|
end
|
187
202
|
|
188
203
|
# The helper runs another service object and subscribes +self+ for the
|
@@ -20,15 +20,13 @@ module Hexx
|
|
20
20
|
# wrapper.respond_to? :something # => false
|
21
21
|
class WithCallbacks
|
22
22
|
|
23
|
+
# @api hide
|
23
24
|
# @!scope class
|
24
25
|
# @!method new(object, prefix: nil)
|
25
26
|
# Constructs the decorator.
|
26
|
-
#
|
27
27
|
# @example (see Hexx::Service::WithCallbacks)
|
28
|
-
#
|
29
28
|
# @param [Hexx::Service] object The service object to be decorated.
|
30
|
-
# @param [Symbol, nil] prefix (nil) The prefix for
|
31
|
-
# to be accessible.
|
29
|
+
# @param [Symbol, nil] prefix (nil) The prefix for accessible methods.
|
32
30
|
def initialize(object, prefix: nil)
|
33
31
|
@object = object
|
34
32
|
@prefix = Regexp.new(prefix ? "^#{ prefix }_" : "")
|
data/lib/hexx/version.rb
CHANGED
data/spec/hexx/coercible_spec.rb
CHANGED
@@ -25,12 +25,47 @@ module Hexx
|
|
25
25
|
|
26
26
|
describe ".attr_coerced" do
|
27
27
|
|
28
|
-
|
29
|
-
subject { test_model.new }
|
28
|
+
context "when the name is neither type nor symbol" do
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
it "fails with TypeError" do
|
31
|
+
expect { test_model.send :attr_coerced, 1, type: test_coercion }
|
32
|
+
.to raise_error(TypeError)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "when the name is empty" do
|
37
|
+
|
38
|
+
it "fails with ArgumentError" do
|
39
|
+
expect { test_model.send :attr_coerced, "", type: test_coercion }
|
40
|
+
.to raise_error(ArgumentError)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when no type is set" do
|
45
|
+
|
46
|
+
it "fails with ArgumentError" do
|
47
|
+
expect { test_model.send :attr_coerced, :name }
|
48
|
+
.to raise_error(ArgumentError)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when a type is not a class" do
|
53
|
+
|
54
|
+
it "fails with TypeError" do
|
55
|
+
expect { test_model.send :attr_coerced, :name, type: test_module }
|
56
|
+
.to raise_error(TypeError)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "with valid arguments" do
|
61
|
+
|
62
|
+
before { test_model.send :attr_coerced, :name, type: test_coercion }
|
63
|
+
subject { test_model.new }
|
64
|
+
|
65
|
+
it "coerces an attribute with given type" do
|
66
|
+
subject.name = "some name"
|
67
|
+
expect(subject.name).to be_kind_of test_coercion
|
68
|
+
end
|
34
69
|
end
|
35
70
|
end
|
36
71
|
end
|
data/spec/hexx/null_spec.rb
CHANGED
data/spec/hexx/service_spec.rb
CHANGED
@@ -26,10 +26,65 @@ module Hexx
|
|
26
26
|
expect(subject).to be_kind_of Wisper::Publisher
|
27
27
|
end
|
28
28
|
|
29
|
-
describe "
|
29
|
+
describe "class helpers" do
|
30
30
|
|
31
|
-
|
32
|
-
|
31
|
+
describe ".allow_params" do
|
32
|
+
|
33
|
+
it "accepts one argument" do
|
34
|
+
expect { described_class.send :allow_params, :name }
|
35
|
+
.not_to raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
it "accepts a list of arguments" do
|
39
|
+
expect { described_class.send :allow_params, :name, :type }
|
40
|
+
.not_to raise_error
|
41
|
+
end
|
42
|
+
|
43
|
+
it "accepts an array of arguments" do
|
44
|
+
expect { described_class.send :allow_params, %w(name type) }
|
45
|
+
.not_to raise_error
|
46
|
+
end
|
47
|
+
|
48
|
+
it "requires strings or symbols" do
|
49
|
+
expect { described_class.send :allow_params, 1 }
|
50
|
+
.to raise_error SyntaxError
|
51
|
+
end
|
52
|
+
|
53
|
+
it "requires non-empty values" do
|
54
|
+
expect { described_class.send :allow_params, "" }
|
55
|
+
.to raise_error ArgumentError
|
56
|
+
end
|
57
|
+
|
58
|
+
it "requires values" do
|
59
|
+
expect { described_class.send :allow_params }
|
60
|
+
.to raise_error ArgumentError
|
61
|
+
end
|
62
|
+
|
63
|
+
context "with valid argument" do
|
64
|
+
|
65
|
+
before { described_class.send :allow_params, :name }
|
66
|
+
subject do
|
67
|
+
described_class.new(name: "name", wrong: "wrong").with_callbacks
|
68
|
+
end
|
69
|
+
|
70
|
+
it "allows initialization of the parameter" do
|
71
|
+
expect(subject.params).to eq("name" => "name")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "defines private getter for the parameter" do
|
75
|
+
expect(described_class.private_instance_methods)
|
76
|
+
.to be_include :name
|
77
|
+
expect { subject.params["name"] = "new" }
|
78
|
+
.to change { subject.name }.to "new"
|
79
|
+
end
|
80
|
+
|
81
|
+
it "defines private setter for the parameter" do
|
82
|
+
expect(described_class.private_instance_methods)
|
83
|
+
.to be_include :name=
|
84
|
+
expect { subject.name = "new" }
|
85
|
+
.to change { subject.params["name"] }.to "new"
|
86
|
+
end
|
87
|
+
end
|
33
88
|
end
|
34
89
|
end
|
35
90
|
|
@@ -37,30 +92,15 @@ module Hexx
|
|
37
92
|
|
38
93
|
let(:value) { "text" }
|
39
94
|
before { described_class.send :allow_params, :name }
|
40
|
-
subject
|
41
|
-
described_class.new(name: value, wrong: "wrong").with_callbacks
|
42
|
-
end
|
95
|
+
subject { described_class.new(name: value).with_callbacks }
|
43
96
|
|
44
|
-
it "stringifies params
|
97
|
+
it "stringifies params keys" do
|
45
98
|
expect(subject.params["name"]).to eq value
|
46
99
|
expect(subject.params[:name]).to be_nil
|
47
100
|
end
|
48
|
-
|
49
|
-
it "whitelists params" do
|
50
|
-
expect(subject.params["wrong"]).to be_nil
|
51
|
-
end
|
52
|
-
|
53
|
-
it "define individual params' getters" do
|
54
|
-
expect(subject.name).to eq value
|
55
|
-
end
|
56
|
-
|
57
|
-
it "defines individual params' setters" do
|
58
|
-
subject.name = :another_name
|
59
|
-
expect(subject.name).to eq :another_name
|
60
|
-
expect(subject.params["name"]).to eq :another_name
|
61
|
-
end
|
62
101
|
end
|
63
102
|
|
103
|
+
# TODO: move this to private helpers
|
64
104
|
describe "#with_callbacks" do
|
65
105
|
|
66
106
|
context "without a prefix" do
|
@@ -98,13 +138,6 @@ module Hexx
|
|
98
138
|
end
|
99
139
|
end
|
100
140
|
|
101
|
-
describe "#params" do
|
102
|
-
|
103
|
-
it "exists" do
|
104
|
-
expect { subject.params }.not_to raise_error
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
141
|
describe "#messages" do
|
109
142
|
|
110
143
|
it "returns an array" do
|
@@ -112,25 +145,11 @@ module Hexx
|
|
112
145
|
end
|
113
146
|
end
|
114
147
|
|
115
|
-
describe "helpers" do
|
148
|
+
describe "instance helpers" do
|
116
149
|
|
117
|
-
let
|
150
|
+
let(:service) { Service.new name: "name" }
|
118
151
|
subject { service.with_callbacks }
|
119
152
|
|
120
|
-
describe ".allow_params" do
|
121
|
-
|
122
|
-
before { described_class.send :allow_params, :name }
|
123
|
-
|
124
|
-
it "sets class params" do
|
125
|
-
expect(described_class.params).to eq %w(name)
|
126
|
-
end
|
127
|
-
|
128
|
-
it "defines instance attributes" do
|
129
|
-
expect(described_class.instance_methods).to be_include :name
|
130
|
-
expect(described_class.instance_methods).to be_include :name=
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
153
|
describe "#validate!" do
|
135
154
|
|
136
155
|
it "passes when service is valid" do
|
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: 6.0.
|
4
|
+
version: 6.0.3
|
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-
|
11
|
+
date: 2014-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|