lazier 3.3.10 → 3.4.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.
@@ -103,9 +103,9 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Mon Dec 2 15:35:19 2013 by
106
+ Generated on Sat Jan 25 11:01:53 2014 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
- 0.8.7.3 (ruby-2.0.0).
108
+ 0.8.7.3 (ruby-2.1.0).
109
109
  </div>
110
110
 
111
111
  </body>
@@ -25,9 +25,9 @@ Gem::Specification.new do |gem|
25
25
 
26
26
  gem.required_ruby_version = ">= 1.9.3"
27
27
 
28
- gem.add_dependency("json", "~> 1.8.0")
28
+ gem.add_dependency("json", "~> 1.8.1")
29
29
  gem.add_dependency("activesupport", ">= 3.2.13") # We don't use ~> to enable use with 4.0
30
30
  gem.add_dependency("tzinfo", ">= 0.3.37") # We don't use ~> to enable use with 0.3.37 (required by activesupport 4.0) and 1.x, which is the latest available
31
- gem.add_dependency("r18n-desktop", "~> 1.1.5")
31
+ gem.add_dependency("r18n-desktop", "~> 1.1.7")
32
32
  gem.add_dependency("hashie", "~> 2.0.5")
33
33
  end
@@ -9,12 +9,49 @@ module Lazier
9
9
  module Hash
10
10
  extend ::ActiveSupport::Concern
11
11
 
12
+ # Returns a new hash, removing all keys which values are blank.
13
+ #
14
+ # @param validator [Proc], if present all the keys which evaluates to true will be removed. Otherwise all blank values will be removed.
15
+ # @return [Hash] The hash with all blank values removed.
16
+ def compact(&validator)
17
+ validator ||= ->(_, v) { v.blank? }
18
+ reject(&validator)
19
+ end
20
+
21
+ # Compacts the current hash, removing all keys which values are blank.
22
+ #
23
+ # @param validator [Proc], if present all the keys which evaluates to true will be removed. Otherwise all blank values will be removed.
24
+ def compact!(&validator)
25
+ validator ||= ->(_, v) { v.blank? }
26
+ reject!(&validator)
27
+ end
28
+
12
29
  # Makes sure that the keys of the hash are accessible in the desired way.
13
30
  #
14
31
  # @param access [Symbol|NilClass] The requested access for the keys. Can be `:strings`, `:symbols` or `:indifferent`. If `nil` the keys are not modified.
15
32
  def ensure_access(access)
16
- method = {strings: :stringify_keys, symbols: :symbolize_keys, indifferent: :with_indifferent_access}.fetch(access, nil)
33
+ method = {strings: :stringify_keys, symbols: :symbolize_keys, indifferent: :with_indifferent_access, dotted: :enable_dotted_access}.fetch(access, nil)
17
34
  method ? send(method) : self
18
35
  end
36
+
37
+ # Makes sure that the hash is accessible using dotted notation. This is also applied to every embedded hash.
38
+ #
39
+ # @param readonly [Boolean] If the dotted notation is only enable for reading. `true` by default.
40
+ #
41
+ def enable_dotted_access(readonly = true)
42
+ extend(Hashie::Extensions::MethodReader)
43
+ extend(Hashie::Extensions::MethodQuery)
44
+ extend(Hashie::Extensions::MethodWriter) if !readonly
45
+
46
+ each do |_, value|
47
+ if value.is_a?(Hash) then
48
+ value.enable_dotted_access(readonly)
49
+ elsif value.respond_to?(:each) then
50
+ value.each do |element|
51
+ element.enable_dotted_access(readonly) if element.is_a?(Hash)
52
+ end
53
+ end
54
+ end
55
+ end
19
56
  end
20
57
  end
@@ -23,6 +23,7 @@ module Lazier
23
23
  # @param root [Symbol] The root level of the translation.
24
24
  # @param path [String] The path where the translations are stored.
25
25
  def i18n_setup(root, path)
26
+ ::I18n.enforce_available_locales = true
26
27
  @i18n_root = root.to_sym
27
28
  @i18n_locales_path = path
28
29
  end
@@ -50,6 +50,16 @@ module Lazier
50
50
  is_a?(::TrueClass) || !self || to_s =~ ::Lazier::Object::BOOLEAN_MATCHER
51
51
  end
52
52
 
53
+ # Sends a method to the object. If the objects doesn't not respond to the method, it returns `nil` instead of raising an exception.
54
+ #
55
+ # @param method [Symbol] The method to send.
56
+ # @param args [Array] The arguments to send.
57
+ # @param &block [Proc] The block to pass to the method.
58
+ # @return [Object|nil] The return value of the method or `nil`, if the object does not respond to the method.
59
+ def safe_send(method, *args, &block)
60
+ respond_to?(method) ? send(method, *args, &block) : nil
61
+ end
62
+
53
63
  # Makes sure that the object is set to something meaningful.
54
64
  #
55
65
  # @param default_value [String] The default value to return if the `verifier` or the block returns true.
@@ -13,10 +13,10 @@ module Lazier
13
13
  MAJOR = 3
14
14
 
15
15
  # The minor version.
16
- MINOR = 3
16
+ MINOR = 4
17
17
 
18
18
  # The patch version.
19
- PATCH = 10
19
+ PATCH = 0
20
20
 
21
21
  # The current version of lazier.
22
22
  STRING = [MAJOR, MINOR, PATCH].compact.join(".")
@@ -38,6 +38,36 @@ describe Lazier::Hash do
38
38
  end
39
39
  end
40
40
 
41
+ describe "#compact" do
42
+ it "should remove blank keys" do
43
+ expect({a: 1, b: nil}.compact).to eq({a: 1})
44
+ end
45
+
46
+ it "should use a custom validator" do
47
+ expect({a: 1, b: nil, c: 3}.compact {|k, v| v == 1 || k == :c}).to eq({b: nil})
48
+ end
49
+
50
+ it "should not be destructive" do
51
+ reference = {a: 1, b: nil}
52
+ reference.compact
53
+ expect(reference).to eq({a: 1, b: nil})
54
+ end
55
+ end
56
+
57
+ describe "#compact!" do
58
+ it "should remove blank keys" do
59
+ reference = {a: 1, b: nil}
60
+ reference.compact!
61
+ expect(reference).to eq({a: 1})
62
+ end
63
+
64
+ it "should use a custom validator" do
65
+ reference = {a: 1, b: nil, c: 3}
66
+ reference.compact! {|k, v| v == 1 || k == :c}
67
+ expect(reference).to eq({b: nil})
68
+ end
69
+ end
70
+
41
71
  describe "#ensure_access" do
42
72
  it "should make sure that the requested access is granted" do
43
73
  expect({"a" => "b"}.ensure_access(:strings)).to eq({"a" => "b"})
@@ -48,6 +78,26 @@ describe Lazier::Hash do
48
78
  expect({a: "b"}.ensure_access(:symbols)).to eq({a: "b"})
49
79
  expect({a: "b"}.ensure_access(:indifferent)).to be_a(::HashWithIndifferentAccess)
50
80
  expect({a: "b"}.ensure_access(:other)).to eq({a: "b"})
81
+
82
+ reference.ensure_access(:dotted)
83
+ expect(reference.a).to eq(1)
84
+ expect(reference.b.c).to eq(2)
85
+ end
86
+ end
87
+
88
+ describe "#with_dotted_access" do
89
+ it "should recursively enable dotted access on a hash" do
90
+ reference = {a: 1, b: {c: 3}, c: [1, {f: {g: 1}}]}
91
+
92
+ reference.enable_dotted_access
93
+ expect(reference.b.c).to eq(3)
94
+ expect(reference.c[1].f.g).to eq(1)
95
+ end
96
+
97
+ it "should also provide write access if asked" do
98
+ reference.enable_dotted_access(false)
99
+ expect { reference.b.f = 4 }.not_to raise_error
100
+ expect(reference["b"]["f"]).to eq(4)
51
101
  end
52
102
  end
53
103
  end
@@ -96,6 +96,17 @@ describe Lazier::Object do
96
96
  end
97
97
  end
98
98
 
99
+ describe "#safe_send" do
100
+ it "should run the method by default" do
101
+ expect("STRING".safe_send(:gsub, "T", "E")).to eq("SERING")
102
+ expect("STRING".safe_send(:gsub, "T") { "E" }).to eq("SERING")
103
+ end
104
+
105
+ it "should silently fail if the method is not supported" do
106
+ expect("STRING".safe_send(:invalid_gsub, "T", "E")).to be_nil
107
+ end
108
+ end
109
+
99
110
  describe "#ensure" do
100
111
  it "should assign a default value to an object" do
101
112
  expect(nil.ensure("VALUE")).to eq("VALUE")
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazier
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.10
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shogun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-02 00:00:00.000000000 Z
11
+ date: 2014-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.8.0
19
+ version: 1.8.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.8.0
26
+ version: 1.8.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 3.2.13
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 3.2.13
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: tzinfo
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: 0.3.37
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.3.37
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: r18n-desktop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 1.1.5
61
+ version: 1.1.7
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 1.1.5
68
+ version: 1.1.7
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: hashie
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: 2.0.5
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 2.0.5
83
83
  description: Several Ruby object enhancements.
@@ -87,10 +87,10 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - .gitignore
91
- - .travis-gemfile
92
- - .travis.yml
93
- - .yardopts
90
+ - ".gitignore"
91
+ - ".travis-gemfile"
92
+ - ".travis.yml"
93
+ - ".yardopts"
94
94
  - CHANGELOG.md
95
95
  - Gemfile
96
96
  - README.md
@@ -170,17 +170,17 @@ require_paths:
170
170
  - lib
171
171
  required_ruby_version: !ruby/object:Gem::Requirement
172
172
  requirements:
173
- - - '>='
173
+ - - ">="
174
174
  - !ruby/object:Gem::Version
175
175
  version: 1.9.3
176
176
  required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  requirements:
178
- - - '>='
178
+ - - ">="
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
181
  requirements: []
182
182
  rubyforge_project: lazier
183
- rubygems_version: 2.0.6
183
+ rubygems_version: 2.2.0
184
184
  signing_key:
185
185
  specification_version: 4
186
186
  summary: Several Ruby object enhancements.