hexx 7.0.1 → 7.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: adb6424142ab3c257299fcb041460531b522fb07
4
- data.tar.gz: e431cd32de61cb5d0cbb7d6a04a0857c2aa13afd
3
+ metadata.gz: 8b0ca93b80a2f6c0c3748f0a273d9edda36972ba
4
+ data.tar.gz: 19527f2b06838db356f0bb99df053671bc1fce70
5
5
  SHA512:
6
- metadata.gz: bfcd1ea97d548e82af5b5970b143c446ad7525ba1a0acd01d63ad27b0dbd0a72aa247e16fe15744e9e3a0b223f70d5eabfa33b88fcbd815a104fee5a53554db1
7
- data.tar.gz: 5cdf1a02b98f49ef603e5ac33019606d066dcf04e8d8ee3827c5456be5e197d2f02dd49a1ee2c75a915c60a944e91c05430e54b5da0bd6e4a879f9c25da16d95
6
+ metadata.gz: 74c24d5eed54e869db7b3fdcbfe9cfbe3b3e1f8fa11cb287c2c51b9c9cc568ec7b067894526b2d68de238c78d94398fbf4d080a6ccd3e5b6c31bdc31b49ea945
7
+ data.tar.gz: d66c7f7a5eb5dbc20b2b1fdf78c53fdbaa99c36290267fddf1941cc7634af2dd5a072650d2546d78c9dc7b34004215c05fd3755fdfd293bc02bad7afe6fb69d3
data/README.rdoc CHANGED
@@ -300,6 +300,56 @@ It is possible to test a service in isolation from its dependencies.
300
300
  end
301
301
  end
302
302
 
303
+ === Hexx::Name
304
+
305
+ The module provides the base class for name constructors for various instances.
306
+
307
+ It declares helpers:
308
+
309
+ * +object+:: the attribute for the object to be named
310
+ * +locale+:: the locale to name the object in
311
+ * +scope+:: the current translation scope
312
+ * +t(value, options):: the translator of the value in current scope and locale
313
+
314
+ Inherit the class and reload its <tt>#for</tt> instance method:
315
+
316
+ module Names
317
+ class Hash < Hexx::Name
318
+ def for
319
+ user ? t(:user, value: user) : t(:empty)
320
+ end
321
+
322
+ def user
323
+ value = object[:user]
324
+ value ? value.upcase : nil
325
+ end
326
+ end
327
+ end
328
+
329
+ Add the necessary translations:
330
+
331
+ # config/locales/ru.yml
332
+ ru:
333
+ activemodel:
334
+ names/hash:
335
+ user: "чувак %{value}"
336
+ empty: "н/д"
337
+
338
+ # config/locales/en.yml
339
+ en:
340
+ activemodel:
341
+ names/hash:
342
+ user: "dude %{value}"
343
+ empty: "-"
344
+
345
+ Then use its <tt>for</tt> class method to construct names:
346
+
347
+ Names::Hash.for { user: "Ivan" }, locale: :en
348
+ # => "dude IVAN"
349
+
350
+ Names::Hash.for { user: nil }, locale: :ru
351
+ # => "н/д"
352
+
303
353
  === Hexx::Null
304
354
 
305
355
  The class implements the {Null object}[http://robots.thoughtbot.com/rails-refactoring-example-introduce-null-object] pattern. The object:
data/lib/hexx/name.rb ADDED
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+ # require "extlib"
3
+
4
+ module Hexx
5
+
6
+ class Name
7
+
8
+ # @!attribute [r] object
9
+ # The object to be named.
10
+ # @return [Object] the object.
11
+
12
+ # @!attribute [r] locale
13
+ # The locale to be used for naming.
14
+ # Equals <tt>I18n.locale</tt> by default.
15
+ # @return [Symbol] the locale.
16
+
17
+ attr_reader :object, :locale
18
+
19
+ # @note The class has a private constructor. Use {.for} method instead.
20
+ private_class_method :new
21
+
22
+ # @api hide
23
+ # The class instance initializer.
24
+ #
25
+ # @param [Object] object The object to be named.
26
+ # @param [Symbol] locale The locale to be used for naming.
27
+ # @return [Hexx::Name] the class instance.
28
+ def initialize(object, locale)
29
+ @object, @locale = object, locale
30
+ end
31
+
32
+ # @abstract
33
+ # Returns the name for the current {#object}.
34
+ #
35
+ # @return [String] the name.
36
+ def for
37
+ ""
38
+ end
39
+
40
+ # Constructs the name for given object and locale.
41
+ #
42
+ # @param [Object] object The object to be named.
43
+ # @param [Symbol, nil] locale (I18n.locale)
44
+ # The locale to be used for naming.
45
+ # @return [String] the name.
46
+ def self.for(object, locale: I18n.locale)
47
+ new(object, locale).for
48
+ end
49
+
50
+ # Translates the value in the current scope and locale.
51
+ #
52
+ # @param [Symbol, String] value The value to be translated.
53
+ # @param [Hash] options ({}) The options for the translation.
54
+ # @return [String] The translation.
55
+ def t(value, options = {})
56
+ return value unless value.is_a? Symbol
57
+ I18n.t value, options.merge(locale: locale, scope: scope)
58
+ end
59
+
60
+ # The scope for object translation.
61
+ # @return [Array<String, Symbol>] the list of scopes.
62
+ def scope
63
+ @scope ||= %w(activemodel models) << object_name
64
+ end
65
+
66
+ private
67
+
68
+ def object_name
69
+ self.class.name.underscore.gsub("::", "/")
70
+ end
71
+ end
72
+ end
data/lib/hexx/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  module Hexx
3
3
 
4
4
  # Current release.
5
- VERSION = "7.0.1"
5
+ VERSION = "7.1.0"
6
6
  end
@@ -0,0 +1,80 @@
1
+ require "spec_helper"
2
+
3
+ module Hexx
4
+ describe Name do
5
+
6
+ before { class Test < Name; end }
7
+ after { Hexx.send :remove_const, :Test }
8
+
9
+ let(:object) { double :object }
10
+ let(:described_class) { Test }
11
+ subject { described_class.send :new, object, :ua }
12
+
13
+ describe "#scope" do
14
+
15
+ it "is set by default" do
16
+ expect(subject.scope).to eq %w(activemodel models hexx/test)
17
+ end
18
+ end
19
+
20
+ describe "#object" do
21
+
22
+ it "is set by the initializer" do
23
+ expect(subject.object).to eq object
24
+ end
25
+ end
26
+
27
+ describe "#locale" do
28
+
29
+ it "can be set by the initializer" do
30
+ subject = described_class.send :new, object, :ua
31
+ expect(subject.locale).to eq :ua
32
+ end
33
+ end
34
+
35
+ describe "#t" do
36
+
37
+ it "translates the symbol in current scope and locale" do
38
+ result = subject.t :text
39
+ expect(result)
40
+ .to eq "translation missing: ua.activemodel.models.hexx/test.text"
41
+ end
42
+
43
+ it "returns non-symbols as is" do
44
+ value = "something"
45
+ expect(subject.t value).to eq value
46
+ end
47
+ end
48
+
49
+ describe "#for" do
50
+
51
+ it "returns a blank string" do
52
+ expect(subject.for).to eq ""
53
+ end
54
+ end
55
+
56
+ describe ".new" do
57
+
58
+ it "is private" do
59
+ expect(described_class.private_methods).to include :new
60
+ end
61
+ end
62
+
63
+ describe ".for" do
64
+
65
+ let(:name) { double for: nil }
66
+ before { allow(described_class).to receive(:new).and_return name }
67
+
68
+ it "constructs the object and calls its #for method" do
69
+ expect(described_class).to receive(:new).with(object, :ua)
70
+ expect(name).to receive(:for)
71
+ described_class.for object, locale: :ua
72
+ end
73
+
74
+ it "sets locale by default" do
75
+ expect(described_class).to receive(:new).with(object, I18n.locale)
76
+ described_class.for object
77
+ end
78
+ end
79
+ end
80
+ 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: 7.0.1
4
+ version: 7.1.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: 2015-01-13 00:00:00.000000000 Z
11
+ date: 2015-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -162,6 +162,7 @@ files:
162
162
  - lib/hexx/helpers/parameters.rb
163
163
  - lib/hexx/helpers/validations.rb
164
164
  - lib/hexx/message.rb
165
+ - lib/hexx/name.rb
165
166
  - lib/hexx/null.rb
166
167
  - lib/hexx/service.rb
167
168
  - lib/hexx/service/with_callbacks.rb
@@ -175,6 +176,7 @@ files:
175
176
  - spec/hexx/helpers/parameters_spec.rb
176
177
  - spec/hexx/helpers/validations_spec.rb
177
178
  - spec/hexx/message_spec.rb
179
+ - spec/hexx/name_spec.rb
178
180
  - spec/hexx/null_spec.rb
179
181
  - spec/hexx/service_invalid_spec.rb
180
182
  - spec/hexx/service_spec.rb
@@ -221,6 +223,7 @@ test_files:
221
223
  - spec/hexx/service_invalid_spec.rb
222
224
  - spec/hexx/coercible_spec.rb
223
225
  - spec/hexx/dependable_spec.rb
226
+ - spec/hexx/name_spec.rb
224
227
  - spec/hexx/configurable_spec.rb
225
228
  - spec/support/matchers/methods.rb
226
229
  - spec/support/initializers/garbage_collection.rb