sugar_utils 0.5.0 → 0.7.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.
data/lib/sugar_utils.rb CHANGED
@@ -1,19 +1,22 @@
1
- # encoding : utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
3
  require 'sugar_utils/version'
5
4
  require 'sugar_utils/file'
6
5
 
6
+ # @api
7
7
  module SugarUtils
8
- # @param [Object] value
8
+ # @param value [Object]
9
9
  #
10
10
  # @return [Boolean]
11
11
  def self.ensure_boolean(value)
12
- return false if value.respond_to?(:to_s) && value.to_s.casecmp('false').zero?
12
+ return false if value == 0 # rubocop:disable Style/NumericPredicate
13
+
14
+ return false if value.respond_to?(:to_s) && value.to_s.strip.casecmp('false').zero?
15
+
13
16
  value ? true : false
14
17
  end
15
18
 
16
- # @param [String, Float, Integer] value
19
+ # @param value [String, Float, Integer]
17
20
  #
18
21
  # @raise [ArgumentError] if the value is a string which cannot be converted
19
22
  # @raise [TypeError] if value is type which cannot be converted
@@ -22,6 +25,33 @@ module SugarUtils
22
25
  def self.ensure_integer(value)
23
26
  return value if value.is_a?(Integer)
24
27
  return value.to_i if value.is_a?(Float)
28
+
25
29
  Float(value).to_i
26
30
  end
31
+
32
+ # @overload scrub_encoding(data)
33
+ # Scrub the string's encoding, and replace any bad characters with ''.
34
+ # @param data [String]
35
+ # @overload scrub_encoding(data, replacement_character)
36
+ # Scrub the string's encoding, and replace any bad characters with the
37
+ # specified character.
38
+ # @param data [String]
39
+ # @param replacement_character [String]
40
+ #
41
+ # @return [String]
42
+ def self.scrub_encoding(data, replacement_character = nil)
43
+ replacement_character = '' unless replacement_character.is_a?(String)
44
+
45
+ # If the Ruby version being used supports String#scrub, then just use it.
46
+ return data.scrub(replacement_character) if data.respond_to?(:scrub)
47
+
48
+ # Otherwise, fall back to String#encode.
49
+ data.encode(
50
+ data.encoding,
51
+ 'binary',
52
+ invalid: :replace,
53
+ undef: :replace,
54
+ replace: replacement_character
55
+ )
56
+ end
27
57
  end
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,9 @@
1
- # encoding : utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
5
- require 'sugar_utils'
3
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
6
4
  require 'rspec/tabular'
7
5
  require 'fakefs/spec_helpers'
6
+ require 'rspec/side_effects'
8
7
  require 'etc'
9
8
  # HACK: including pp seems to resolve an error with FakeFS and File.read
10
9
  # This seems to be related to but not the same as the problem mentioned in the
@@ -16,6 +15,9 @@ require 'pp'
16
15
  require 'simplecov'
17
16
  SimpleCov.start
18
17
 
18
+ require 'sugar_utils'
19
+ MultiJson.use(:ok_json)
20
+
19
21
  SolidAssert.enable_assertions
20
22
 
21
23
  RSpec.configure do |config|
@@ -48,8 +50,8 @@ RSpec::Matchers.define :have_file_permission do |expected|
48
50
  match do |actual|
49
51
  next false unless File.exist?(actual)
50
52
 
51
- @actual = format('%o', File.stat(actual).mode)
52
- @expected = format('%o', expected)
53
+ @actual = format('%<mode>o', mode: File.stat(actual).mode)
54
+ @expected = format('%<mode>o', mode: expected)
53
55
  values_match?(@expected, @actual)
54
56
  end
55
57
  end
@@ -78,7 +80,7 @@ RSpec::Matchers.define :have_mtime do |expected|
78
80
  match do |actual|
79
81
  next false unless File.exist?(actual)
80
82
 
81
- @actual = File.stat(actual).mtime
83
+ @actual = File.stat(actual).mtime.to_i
82
84
  @expected = expected
83
85
  values_match?(@expected, @actual)
84
86
  end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe SugarUtils::File::WriteOptions do
6
+ subject(:write_options) { described_class.new(filename, options) }
7
+
8
+ let(:filename) { nil }
9
+
10
+ before do
11
+ allow(File).to receive(:exist?).with('missing').and_return(false)
12
+ allow(File).to receive(:exist?).with('found').and_return(true)
13
+ allow(File::Stat).to receive(:new).with('found').and_return(
14
+ instance_double(File::Stat, uid: :uid, gid: :gid)
15
+ )
16
+ end
17
+
18
+ describe '#flush?' do
19
+ subject { write_options.flush? }
20
+
21
+ inputs :options
22
+ it_with Hash[], false
23
+ it_with Hash[flush: :flush], :flush
24
+ end
25
+
26
+ describe '#perm' do
27
+ subject { write_options.perm(*args) }
28
+
29
+ inputs :options, :args
30
+ it_with Hash[], [], 0o644
31
+ it_with Hash[], %i[default_value], :default_value
32
+ it_with Hash[mode: :mode], [], :mode
33
+ it_with Hash[mode: :mode], %i[default_value], :mode
34
+ it_with Hash[perm: :perm, mode: :mode], [], :mode
35
+ it_with Hash[perm: :perm, mode: :mode], %i[default_value], :mode
36
+ it_with Hash[perm: :perm], [], :perm
37
+ it_with Hash[perm: :perm], %i[default_value], :perm
38
+ end
39
+
40
+ describe '#owner' do
41
+ subject { write_options.owner }
42
+
43
+ inputs :filename, :options
44
+ it_with nil, Hash[], nil
45
+ it_with 'missing', Hash[], nil
46
+ it_with 'found', Hash[], :uid
47
+ it_with nil, Hash[owner: :owner], :owner
48
+ it_with 'missing', Hash[owner: :owner], :owner
49
+ it_with 'found', Hash[owner: :owner], :owner
50
+ end
51
+
52
+ describe '#group' do
53
+ subject { write_options.group }
54
+
55
+ inputs :filename, :options
56
+ it_with nil, Hash[], nil
57
+ it_with 'missing', Hash[], nil
58
+ it_with 'found', Hash[], :gid
59
+ it_with nil, Hash[group: :group], :group
60
+ it_with 'missing', Hash[group: :group], :group
61
+ it_with 'found', Hash[group: :group], :group
62
+ end
63
+
64
+ describe '#slice' do
65
+ subject { write_options.slice(*args) }
66
+
67
+ let(:options) { { key1: :value1, key2: :value2, key3: :value3 } }
68
+
69
+ inputs :args
70
+ it_with [], Hash[]
71
+ it_with %i[key1], Hash[key1: :value1]
72
+ it_with %i[key2], Hash[key2: :value2]
73
+ it_with %i[key3], Hash[key3: :value3]
74
+ it_with %i[key1 key3], Hash[key1: :value1, key3: :value3]
75
+ it_with [%i[key1], nil, %i[key3]], Hash[key1: :value1, key3: :value3]
76
+ end
77
+ end