sleeping_king_studios-tools 0.8.0 → 1.0.0.rc.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,6 +11,8 @@ module SleepingKingStudios::Tools
11
11
  extend Forwardable
12
12
  end
13
13
 
14
+ # @return [SleepingKingStudios::Tools::Base] a memoized instance of the
15
+ # tools class.
14
16
  def self.instance
15
17
  @instance ||= new
16
18
  end
@@ -5,6 +5,8 @@ require 'sleeping_king_studios/tools'
5
5
  module SleepingKingStudios::Tools
6
6
  # Tools for working with an application or working environment.
7
7
  class CoreTools < Base
8
+ # Exception class used when deprecated code is called and the deprecation
9
+ # strategy is 'raise'.
8
10
  class DeprecationError < StandardError; end
9
11
 
10
12
  class << self
@@ -14,7 +16,11 @@ module SleepingKingStudios::Tools
14
16
  :require_each
15
17
  end
16
18
 
19
+ # @param deprecation_strategy [String] The name of the strategy used when
20
+ # deprecated code is called. Must be 'ignore', 'raise', or 'warn'.
17
21
  def initialize(deprecation_strategy: nil)
22
+ super()
23
+
18
24
  @deprecation_strategy =
19
25
  deprecation_strategy || ENV.fetch('DEPRECATION_STRATEGY', 'warn')
20
26
  end
@@ -46,17 +52,11 @@ module SleepingKingStudios::Tools
46
52
  )
47
53
  end
48
54
 
49
- # Generates an empty Binding object with a BasicObject as the receiver.
55
+ # Generates an empty Binding object with an Object as the receiver.
50
56
  #
51
57
  # @return [Binding] The empty binding object.
52
58
  def empty_binding
53
- context = Object.new
54
-
55
- def context.binding
56
- Kernel.instance_method(:binding).bind(self).call
57
- end
58
-
59
- context.binding
59
+ Object.new.instance_exec { binding }
60
60
  end
61
61
 
62
62
  # Expands each file pattern and requires each file.
@@ -5,6 +5,7 @@ require 'sleeping_king_studios/tools'
5
5
  module SleepingKingStudios::Tools
6
6
  # Tools for working with hash-like enumerable objects.
7
7
  class HashTools < SleepingKingStudios::Tools::Base
8
+ # Expected methods that a Hash-like object should implement.
8
9
  HASH_METHODS = %i[[] count each each_key each_pair].freeze
9
10
 
10
11
  class << self
@@ -16,10 +17,9 @@ module SleepingKingStudios::Tools
16
17
  :generate_binding,
17
18
  :hash?,
18
19
  :immutable?,
19
- :mutable?
20
-
21
- alias stringify_keys convert_keys_to_strings
22
- alias symbolize_keys convert_keys_to_symbols
20
+ :mutable?,
21
+ :stringify_keys,
22
+ :symbolize_keys
23
23
  end
24
24
 
25
25
  # Returns a copy of the hash with the keys converted to strings.
@@ -83,6 +83,10 @@ module SleepingKingStudios::Tools
83
83
  end
84
84
  end
85
85
 
86
+ # Generates a Binding object with an Object as the receiver and the hash
87
+ # key-value pairs set as local variables.
88
+ #
89
+ # @return [Binding] The binding object.
86
90
  def generate_binding(hsh)
87
91
  require_hash! hsh
88
92
 
@@ -168,7 +168,7 @@ module SleepingKingStudios::Tools
168
168
  romanize_digit(additive: additive, digit: digit.to_i, tens: index)
169
169
  end
170
170
  .reverse
171
- .join ''
171
+ .join
172
172
  end
173
173
 
174
174
  private
@@ -17,11 +17,10 @@ module SleepingKingStudios::Tools
17
17
  :dig,
18
18
  :eigenclass,
19
19
  :immutable?,
20
+ :metaclass,
20
21
  :mutable?,
21
22
  :object?,
22
23
  :try
23
-
24
- alias metaclass eigenclass
25
24
  end
26
25
 
27
26
  # Takes a proc or lambda and invokes it with the given object as
@@ -93,7 +92,7 @@ module SleepingKingStudios::Tools
93
92
  # method call. If the object does not respond to the method name, nil is
94
93
  # returned instead of calling the method.
95
94
  #
96
- # @param [Object] obj The object to dig.
95
+ # @param [Object] object The object to dig.
97
96
  # @param [Array] method_names The names of the methods to call.
98
97
  #
99
98
  # @return [Object] The result of the last method call, or nil if the last
@@ -104,11 +103,18 @@ module SleepingKingStudios::Tools
104
103
  end
105
104
  end
106
105
 
106
+ # @!method metaclass(object)
107
+ # Returns the object's singleton class.
108
+ #
109
+ # @param [Object] object The object for which an eigenclass is required.
110
+ #
111
+ # @return [Class] The object's eigenclass.
112
+
107
113
  # Returns the object's eigenclass.
108
114
  #
109
115
  # @param [Object] object The object for which an eigenclass is required.
110
116
  #
111
- # @return [Class] The object's eigenclass.
117
+ # @return [Class] The object's singleton class.
112
118
  def eigenclass(object)
113
119
  object.singleton_class
114
120
  end
@@ -24,7 +24,12 @@ module SleepingKingStudios::Tools
24
24
  :underscore
25
25
  end
26
26
 
27
+ # @param inflector [Object] An object that conforms to the interface used
28
+ # by SleepingKingStudios::Tools::Toolbox::Inflector, such as
29
+ # ActiveSupport::Inflector .
27
30
  def initialize(inflector: nil)
31
+ super()
32
+
28
33
  @inflector =
29
34
  inflector || SleepingKingStudios::Tools::Toolbox::Inflector.new
30
35
  end
@@ -57,34 +62,6 @@ module SleepingKingStudios::Tools
57
62
  commands.reduce(str) { |memo, command| send(command, memo) }
58
63
  end
59
64
 
60
- # (see PluralInflector#define_irregular_word)
61
- def define_irregular_word(singular, plural)
62
- CoreTools.deprecate 'StringTools#define_irregular_word'
63
-
64
- inflector.rules.define_irregular_word singular, plural
65
- end
66
-
67
- # (see PluralInflector#define_plural_rule)
68
- def define_plural_rule(match, replace)
69
- CoreTools.deprecate 'StringTools#define_plural_rule'
70
-
71
- inflector.rules.define_plural_rule match, replace
72
- end
73
-
74
- # (see PluralInflector#define_singular_rule)
75
- def define_singular_rule(match, replace)
76
- CoreTools.deprecate 'StringTools#define_singular_rule'
77
-
78
- inflector.rules.define_singular_rule match, replace
79
- end
80
-
81
- # (see PluralInflector#define_uncountable_word)
82
- def define_uncountable_word(word)
83
- CoreTools.deprecate 'StringTools#define_uncountable_word'
84
-
85
- inflector.rules.define_uncountable_word word
86
- end
87
-
88
65
  # Adds the specified number of spaces to the start of each line of the
89
66
  # string. Defaults to 2 spaces.
90
67
  #
@@ -133,33 +110,7 @@ module SleepingKingStudios::Tools
133
110
  # @param str [String] The word to pluralize.
134
111
  #
135
112
  # @return [String] The pluralized word.
136
- #
137
- # @overload pluralize(count, single, plural)
138
- # @deprecated This functionality is deprecated as of version 0.4.0 and
139
- # will be removed in a future version. Use IntegerTools#pluralize
140
- # instead.
141
- #
142
- # Returns the singular or the plural value, depending on the provided
143
- # item count.
144
- #
145
- # @example
146
- # "There are four #{StringTools.pluralize 4, 'light', 'lights'}!"
147
- # #=> 'There are four lights!'
148
- #
149
- # @param [Integer] count The number of items.
150
- # @param [String] single The singular form of the word or phrase.
151
- # @param [String] plural The plural form of the word or phrase.
152
- #
153
- # @return [String] The single form if count == 1; otherwise the plural
154
- # form.
155
113
  def pluralize(*args)
156
- if args.count == 3
157
- CoreTools.deprecate 'StringTools#pluralize with 3 arguments',
158
- message: 'Use IntegerTools#pluralize instead.'
159
-
160
- return IntegerTools.pluralize(*args)
161
- end
162
-
163
114
  str = require_string! args.first
164
115
 
165
116
  inflector.pluralize str
@@ -175,7 +126,11 @@ module SleepingKingStudios::Tools
175
126
  word == singularize(word)
176
127
  end
177
128
 
178
- # (see PluralInflector#singularize)
129
+ # Transforms the word to a singular, lowercase form.
130
+ #
131
+ # @param str [String] The word to transform.
132
+ #
133
+ # @return [String] The word in singular form.
179
134
  def singularize(str)
180
135
  require_string! str
181
136
 
@@ -5,10 +5,17 @@ require 'sleeping_king_studios/tools'
5
5
  module SleepingKingStudios::Tools
6
6
  # Helper object for quick access to all available tools.
7
7
  class Toolbelt < BasicObject
8
+ # @return [SleepingKingStudios::Tools::Toolbelt] a memoized instance of the
9
+ # toolbelt class.
8
10
  def self.instance
9
11
  @instance ||= new
10
12
  end
11
13
 
14
+ # @param deprecation_strategy [String] The name of the strategy used when
15
+ # deprecated code is called. Must be 'ignore', 'raise', or 'warn'.
16
+ # @param inflector [Object] An object that conforms to the interface used
17
+ # by SleepingKingStudios::Tools::Toolbox::Inflector, such as
18
+ # ActiveSupport::Inflector .
12
19
  def initialize(deprecation_strategy: nil, inflector: nil)
13
20
  @array_tools = ::SleepingKingStudios::Tools::ArrayTools.new
14
21
  @core_tools = ::SleepingKingStudios::Tools::CoreTools.new(
@@ -39,17 +46,7 @@ module SleepingKingStudios::Tools
39
46
  alias obj object_tools
40
47
  alias str string_tools
41
48
 
42
- %w[array core hash integer object string].each do |name|
43
- define_method(name) do
44
- ::SleepingKingStudios::Tools::CoreTools.deprecate(
45
- "SleepingKingStudios::Tools::Toolbelt##{name}",
46
- message: "Use ##{name}_tools instead."
47
- )
48
-
49
- ::SleepingKingStudios::Tools.const_get("#{name.capitalize}Tools")
50
- end
51
- end
52
-
49
+ # @return [String] a human-readable representation of the object.
53
50
  def inspect
54
51
  "#<#{::Object.instance_method(:class).bind(self).call.name}>"
55
52
  end
@@ -1,15 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'sleeping_king_studios/tools/string_tools'
3
+ require 'forwardable'
4
+
5
+ require 'sleeping_king_studios/tools/toolbelt'
4
6
  require 'sleeping_king_studios/tools/toolbox'
5
7
 
6
8
  module SleepingKingStudios::Tools::Toolbox
7
9
  # Provides an enumerable interface for defining a group of constants.
8
10
  class ConstantMap < Module
11
+ extend Forwardable
12
+ include Enumerable
13
+
9
14
  # @param constants [Hash] The constants to define.
10
15
  def initialize(constants)
11
16
  super()
12
17
 
18
+ @to_h = constants.dup
19
+
13
20
  constants.each do |const_name, const_value|
14
21
  const_set(const_name, const_value)
15
22
 
@@ -17,51 +24,72 @@ module SleepingKingStudios::Tools::Toolbox
17
24
  end
18
25
  end
19
26
 
20
- # Returns a hash with the names and values of the defined constants.
27
+ def_delegators :@to_h,
28
+ :each,
29
+ :each_key,
30
+ :each_pair,
31
+ :each_value,
32
+ :keys,
33
+ :values
34
+
35
+ # @!method each
36
+ # Iterates through the defined constants, yielding the name and value of
37
+ # each constant to the block.
21
38
  #
22
- # @return [Hash] The defined constants.
23
- def all
24
- constants.each.with_object({}) do |const_name, hsh|
25
- hsh[const_name] = const_get(const_name)
26
- end
27
- end
39
+ # @yieldparam key [Symbol] The name of the constant.
40
+ # @yieldparam value [Object] The value of the constant.
28
41
 
29
- # Iterates through the defined constants, yielding the name and value of
30
- # each constant to the block.
42
+ # @!method each_key
43
+ # Iterates through the defined constants, yielding the name of each
44
+ # constant to the block.
31
45
  #
32
- # @yieldparam key [Symbol] The name of the symbol.
33
- # @yieldparam value [Object] The value of the symbol.
34
- def each(&block)
35
- all.each(&block)
36
- end
46
+ # @yieldparam key [Symbol] The name of the constant.
47
+
48
+ # @!method each_pair
49
+ # Iterates through the defined constants, yielding the name and value of
50
+ # each constant to the block.
51
+ #
52
+ # @yieldparam key [Symbol] The name of the constant.
53
+ # @yieldparam value [Object] The value of the constant.
54
+
55
+ # @!method each_value
56
+ # Iterates through the defined constants, yielding the value of each
57
+ # constant to the block.
58
+ #
59
+ # @yieldparam value [Object] The value of the constant.
60
+
61
+ # @!method keys
62
+ # @return [Array] the names of the defined constants.
63
+
64
+ # @!method values
65
+ # @return [Array] the values of the defined constants.
66
+
67
+ # @return [Hash] The defined constants.
68
+ attr_reader :to_h
69
+ alias all to_h
37
70
 
38
71
  # Freezes the constant map and recursively freezes every constant value
39
- # using ObjectTools#deep_freeze. Also pre-emptively defines any reader
40
- # methods that are not already undefined.
72
+ # using ObjectTools#deep_freeze.
41
73
  #
42
74
  # @see ObjectTools#deep_freeze
43
75
  def freeze
44
- constants.each do |const_name|
45
- object_tools.deep_freeze const_get(const_name)
46
- end
47
-
48
76
  super
77
+
78
+ tools.hsh.deep_freeze(@to_h)
79
+
80
+ self
49
81
  end
50
82
 
51
83
  private
52
84
 
53
- def define_reader(const_name, reader_name = nil)
54
- reader_name ||= string_tools.underscore(const_name.to_s).intern
85
+ def define_reader(const_name)
86
+ reader_name ||= tools.str.underscore(const_name.to_s).intern
55
87
 
56
88
  define_singleton_method(reader_name) { const_get const_name }
57
89
  end
58
90
 
59
- def object_tools
60
- ::SleepingKingStudios::Tools::ObjectTools
61
- end
62
-
63
- def string_tools
64
- ::SleepingKingStudios::Tools::StringTools
91
+ def tools
92
+ ::SleepingKingStudios::Tools::Toolbelt.instance
65
93
  end
66
94
  end
67
95
  end
@@ -42,7 +42,7 @@ module SleepingKingStudios::Tools::Toolbox
42
42
  # capitalized. Defaults to true.
43
43
  #
44
44
  # @return [String] The word in CamelCase.
45
- def camelize(word, uppercase_first_letter = true)
45
+ def camelize(word, uppercase_first_letter = true) # rubocop:disable Style/OptionalBooleanParameter
46
46
  return '' if word.nil? || word.empty?
47
47
 
48
48
  word = word.to_s.gsub(/(\b|[_-])([a-z])/) { Regexp.last_match(2).upcase }
@@ -80,15 +80,12 @@ module SleepingKingStudios::Tools::Toolbox
80
80
  # rubocop:enable Metrics/AbcSize
81
81
  # rubocop:enable Metrics/CyclomaticComplexity
82
82
 
83
- # rubocop:disable Metrics/AbcSize
84
- # rubocop:disable Metrics/MethodLength
85
-
86
83
  # Transforms the word to a singular, lowercase form.
87
84
  #
88
85
  # @param word [String] The word to transform.
89
86
  #
90
87
  # @return [String] The word in singular form.
91
- def singularize(word)
88
+ def singularize(word) # rubocop:disable Metrics/MethodLength
92
89
  return '' if word.nil? || word.empty?
93
90
 
94
91
  normalized = word.to_s.strip.downcase
@@ -108,12 +105,9 @@ module SleepingKingStudios::Tools::Toolbox
108
105
  word
109
106
  end
110
107
 
111
- # rubocop:enable Metrics/AbcSize
112
- # rubocop:enable Metrics/MethodLength
113
-
114
108
  # Transforms the word to a lowercase, underscore-separated form.
115
109
  #
116
- # @params word [String] the word to transform.
110
+ # @param word [String] the word to transform.
117
111
  #
118
112
  # @return [String] The word in underscored form.
119
113
  def underscore(word)
@@ -12,7 +12,7 @@ module SleepingKingStudios::Tools::Toolbox
12
12
  mod.singleton_class.include?(self)
13
13
  end
14
14
 
15
- # @api private
15
+ # @private
16
16
  def included(other)
17
17
  return super unless defined?(self::ClassMethods)
18
18
 
@@ -10,13 +10,19 @@ module SleepingKingStudios
10
10
  module Version
11
11
  extend SleepingKingStudios::Tools::Toolbox::SemanticVersion
12
12
 
13
- MAJOR = 0
14
- MINOR = 8
15
- PATCH = 0
16
- PRERELEASE = nil
17
- BUILD = nil
13
+ # Major version.
14
+ MAJOR = 1
15
+ # Minor version.
16
+ MINOR = 0
17
+ # Patch version.
18
+ PATCH = 0
19
+ # Prerelease version.
20
+ PRERELEASE = :rc
21
+ # Build metadata.
22
+ BUILD = 0
18
23
  end
19
24
 
25
+ # The current version of the gem.
20
26
  VERSION = Version.to_gem_version
21
27
  end
22
28
  end