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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5363a542691cf187bc33c6af98944fddd629b593
4
- data.tar.gz: b86bd725ce6048ddb028316b574918980b844012
3
+ metadata.gz: 861d8373aad89fc2f9409572c87782223dd042e4
4
+ data.tar.gz: 40061fbf81bdf2ff8f57679e328d71c944260a71
5
5
  SHA512:
6
- metadata.gz: e451529cfaecd38210afa3d8e4206d23fbce09a1f966a8da564f20959eac30fc96c47e15ecf595ff7fd8b32ff818e5e06523e67b1f0d047f7fe678efddea4525
7
- data.tar.gz: cb9ad8b597a55ce3f10c8c0246bb4e439489f528dd5806ced91a4547bfb1dc57fee7bf6a86ab5197feb9309ec5155c97b1912a15bdc8ae275d153e2bd791cede
6
+ metadata.gz: 509e8e294856e4f6509c0d68a188533f33646152b1567114d60943fe84e3ab98ec5d21d55aad33e8a3c38706fd875fef57ff0eb9e37605727e3f0d1d3f081896
7
+ data.tar.gz: b3f38e5997f22130d83050d1a137110424a4dfee5d7544311f0ec75c9fb8524accb0566bc8d99fe2f780f06443b9f915bab6b845fd55c27888c550873c354015
@@ -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 bang in logical expressions:
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
@@ -74,15 +74,11 @@ module Hexx
74
74
  private
75
75
 
76
76
  # @abstract
77
- # @return [String] the getter definition
78
77
  def getter
79
- ""
80
78
  end
81
79
 
82
80
  # @abstract
83
- # @return [String] the setter definition
84
81
  def setter
85
- ""
86
82
  end
87
83
 
88
84
  # @api hide
@@ -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
@@ -24,22 +24,39 @@ module Hexx
24
24
  include Wisper::Publisher
25
25
  include ActiveModel::Validations
26
26
 
27
- # @api hide
28
- # Returns the list of allowed parameters for service objects.
29
- #
30
- # The parameters are added to the list by the {.allow_params} private
31
- # helper method.
32
- #
33
- # @example
34
- # class Service < Hexx::Service
35
- # allow_params :name
36
- # end
37
- #
38
- # Service.params # => "name"
39
- #
40
- # @return [Array<String>] The list of allowed instance parameters.
41
- def self.params
42
- @params ||= []
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(*(self.class.params))
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 :params, :messages
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
- # Sets a list of allowed parameters for the class constructor and
179
- # defines the corresponding instance attributes.
180
- #
181
- # @example (see Hexx::Service::Parameters.params)
182
- # @param [Array<Symbol, String>] keys The list of allowed parameters.
183
- def self.allow_params(*keys)
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 private methods
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 }_" : "")
@@ -2,5 +2,5 @@
2
2
  module Hexx
3
3
 
4
4
  # Current release.
5
- VERSION = "6.0.2"
5
+ VERSION = "6.0.3"
6
6
  end
@@ -25,12 +25,47 @@ module Hexx
25
25
 
26
26
  describe ".attr_coerced" do
27
27
 
28
- before { test_model.send :attr_coerced, :name, type: test_coercion }
29
- subject { test_model.new }
28
+ context "when the name is neither type nor symbol" do
30
29
 
31
- it "coerces an attribute with given type" do
32
- subject.name = "some name"
33
- expect(subject.name).to be_kind_of test_coercion
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
@@ -48,7 +48,8 @@ module Hexx
48
48
 
49
49
  it "is falsey" do
50
50
  expect(Null).to be_falsey
51
- expect(Null).not_to be_truthy
51
+ expect(Null.true?).to be_falsey
52
+ expect(Null.false?).to be_truthy
52
53
  expect(!Null).to eq true
53
54
  end
54
55
 
@@ -26,10 +26,65 @@ module Hexx
26
26
  expect(subject).to be_kind_of Wisper::Publisher
27
27
  end
28
28
 
29
- describe ".params" do
29
+ describe "class helpers" do
30
30
 
31
- it "returns an array" do
32
- expect(described_class.params).to be_kind_of Array
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 do
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' keys" do
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!(:service) { Service.new }
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.2
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-24 00:00:00.000000000 Z
11
+ date: 2014-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel