hugger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b4d808260564a2c9d055b8c0b0ff4cc445addd2a
4
+ data.tar.gz: 95b76893782f82463a78fc18f45e140f725af7f3
5
+ SHA512:
6
+ metadata.gz: 63fd2524e857f327599fed9858e8e291d5e9adb8a41f41fae93a666b424fc4ddd249fa92b371d206b9a59fbdbc6a2e3e755c3ee0aa88c42aee8bebc69f4284ce
7
+ data.tar.gz: 9c11490bee6a55356e1ca3b83828c58ada1ee1423f2ecc25e6672b3b5eec66351f92b71b06b62685545be8c266f771fe5132995784f8dd6199fe6221ba3affa2
data/.document ADDED
@@ -0,0 +1,3 @@
1
+ -
2
+ ChangeLog.md
3
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
11
+ test/coverage/
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'vendor/**/*'
4
+ - 'test/fixtures/**/*'
5
+ - 'tmp/**/*'
6
+ Metrics/BlockLength:
7
+ Enabled: false
8
+
9
+ Metrics/LineLength:
10
+ Enabled: false
11
+
12
+ Metrics/ClassLength:
13
+ Enabled: false
14
+
15
+ Metrics/LineLength:
16
+ Max: 150
17
+
18
+ Style/CaseEquality:
19
+ Exclude:
20
+ - 'test/**/*'
21
+
22
+ Naming/UncommunicativeMethodParamName:
23
+ Exclude:
24
+ - 'test/**/*'
25
+
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.7
4
+ - 2.4.2
5
+ - 2.5
6
+ before_install:
7
+ - gem install bundler -v 1.15.1
8
+ - gem update --system
9
+ env:
10
+ - CI=true
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown --title "hugger Documentation" --protected
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/ChangeLog.md ADDED
@@ -0,0 +1,4 @@
1
+ ### 0.1.0 / 2018-05-09
2
+
3
+ * Initial release:
4
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hugger.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2014-2017 [Luca Guidi](https://github.com/hanami/hanami)
4
+ Copyright (c) 2018 Akira Takahashi
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Hugger [![Gem Version](https://badge.fury.io/rb/hugger.svg)](https://badge.fury.io/rb/hugger) [![Build Status](https://travis-ci.org/rike422/hugger.svg?branch=master)](https://travis-ci.org/rike422/hugger) [![Code Climate](https://codeclimate.com/github/rike422/hugger/badges/gpa.svg)](https://codeclimate.com/github/rike422/hugger) [![Coverage Status](https://coveralls.io/repos/github/rike422/hugger/badge.svg?branch=master)](https://coveralls.io/github/rike422/hugger?branch=master)
2
+
3
+
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'hugger'
12
+ ```
13
+
14
+ ## Usage
15
+
16
+
17
+
18
+ ### Hugger::String
19
+
20
+ ```ruby
21
+ require 'hugger'
22
+
23
+ class SampleClass
24
+ include Hugger::Decoratable
25
+
26
+ hug_string :wrap
27
+ def wrap(a)
28
+ "#{a}_is_huged_by_hugger"
29
+ end
30
+
31
+ def dynamic_wrap(b)
32
+ hug "#{b}_is_hugged_by_hugger"
33
+ end
34
+ end
35
+
36
+ c = SampleClass.new
37
+ c.wrap('rike422').class => Hugger::String
38
+ c.dynamic_wrap('rike422').class => Hugger::String
39
+
40
+ c.dynamic_wrap('rike422').camelize # => "rike422_is_hugged_by_hugger"
41
+ c.dynamic_wrap('rike422').camelize.underscore # > "rike422_is_hugged_by_hugger"
42
+ c.dynamic_wrap('rike422').classify # => "Rike422IsHugedByHugger"
43
+ c.dynamic_wrap('rike422').dasherize # => "rike422-is-hugged-by-hugger"
44
+ c.dynamic_wrap('rike422').titleize # => "Rike422 Is Hugged By Hugger"
45
+ c.dynamic_wrap('rike422').pluralize # => "rike422_is_hugged_by_huggers"
46
+ c.dynamic_wrap('rike422').pluralize.singularize # => "rike422_is_hugged_by_hugger"
47
+
48
+
49
+ Hugger::String.titleize('hugger utils') # => "Hugger Utils"
50
+ Hugger::String.camelize('hugger utils') # => "Hugger utils"
51
+ Hugger::String.classify('hugger_utils') # => 'HuggerUtils'
52
+ Hugger::String.underscore('HuggerUtils') # => 'hugger_utils'
53
+ Hugger::String.dasherize('Hugger Utils') # => 'hugger-utils'
54
+ Hugger::String.dasherize('hugger_utils') # => 'hugger-utils'
55
+ Hugger::String.dasherize('HuggerUtils') # => "hugger-utils"
56
+ Hugger::String.demodulize(Hugger::String.name) # => "String"
57
+ Hugger::String.namespace(Hugger::String.name) # => "Hugger"
58
+ Hugger::String.pluralize('book') # => 'books'
59
+ Hugger::String.singularize('books') # => 'book'
60
+
61
+ ```
62
+
63
+ ## Thanks
64
+
65
+ This project borrow codes from following projects:
66
+
67
+
68
+ - [Hanami](https://github.com/hanami/utils)
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
73
+
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ begin
7
+ require 'bundler/setup'
8
+ rescue LoadError => e
9
+ abort e.message
10
+ end
11
+
12
+ Rake::TestTask.new do |test|
13
+ test.libs << 'test'
14
+ test.pattern = 'test/**/test_*.rb'
15
+ test.verbose = true
16
+ end
17
+
18
+ require 'yard'
19
+ YARD::Rake::YardocTask.new
20
+ task doc: :yard
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'hugger'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/hagoromo.gemspec ADDED
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path('lib', __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hugger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'hugger'
8
+ spec.version = Hugger::VERSION
9
+ spec.authors = ['Akira Takahashi']
10
+ spec.email = ['rike422@gmail.com']
11
+
12
+ spec.summary = 'Hugger is utility wrapper classes'
13
+ spec.description = 'Hugger is utility wrapper classes'
14
+ spec.homepage = 'https://github.com/rike422/hugger'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'bin'
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'dry-inflector'
23
+ spec.add_development_dependency 'bundler'
24
+ spec.add_development_dependency 'codeclimate-test-reporter'
25
+ spec.add_development_dependency 'coveralls'
26
+ spec.add_development_dependency 'pry'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rubocop'
29
+ spec.add_development_dependency 'simplecov'
30
+ spec.add_development_dependency 'test-unit-full'
31
+ spec.add_development_dependency 'yard'
32
+ end
@@ -0,0 +1,37 @@
1
+ module Hugger
2
+ # String
3
+ # @since 0.0.1
4
+ module Decoratable
5
+ def self.included(klass)
6
+ super
7
+ klass.extend(ClassMethods)
8
+ end
9
+
10
+ # Decorates the object using the Hugger classes
11
+ # @param [Object]
12
+ def hug(value)
13
+ case value
14
+ when ::String, String
15
+ Hugger::String.new(value)
16
+ end
17
+ end
18
+ end
19
+
20
+ # Decoratable class methods
21
+ # @since 0.0.1
22
+ module ClassMethods
23
+ # Decorates object from given method using the Hugger classes
24
+ # @param [Object]
25
+ def hug_string(*args)
26
+ m = Module.new
27
+ args.each do |method|
28
+ m.class_eval <<-STRING_WRAPPER, __FILE__, __LINE__ + 1
29
+ def #{method}(*args)
30
+ Hugger::String.new(super(*args))
31
+ end
32
+ STRING_WRAPPER
33
+ end
34
+ prepend(m)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ module Hugger
2
+ # Object
3
+ # @since 0.0.1
4
+ # @api private
5
+ module Object
6
+ STRING_MATCHER = /\A[[:space:]]*\z/
7
+
8
+ # Checks object is blank
9
+ # @return [TrueClass,FalseClass]
10
+ # @since 0.0.1
11
+ def blank?
12
+ case object
13
+ when String, ::String
14
+ STRING_MATCHER === object # rubocop:disable Style/CaseEquality
15
+ # when Hash, ::Hash, ::Array
16
+ # object.empty?
17
+ # when TrueClass, Numeric
18
+ # false
19
+ # when FalseClass, NilClass
20
+ # true
21
+ # else
22
+ # object.respond_to?(:empty?) ? object.empty? : !self
23
+ end
24
+ end
25
+
26
+ # Checks object is present
27
+ # @return [TrueClass,FalseClass]
28
+ # @since 0.0.1
29
+ def present?
30
+ !blank?
31
+ end
32
+
33
+ #
34
+ # @return [TrueClass,FalseClass]
35
+ # @since 0.0.1
36
+ def object
37
+ raise 'Not'
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,606 @@
1
+ module Hugger
2
+ # String
3
+ # @since 0.0.1
4
+ class String
5
+ include Hugger::Object
6
+
7
+ # Empty string for #classify
8
+ #
9
+ # @since 0.0.1
10
+ # @api private
11
+ EMPTY_STRING = ''.freeze
12
+
13
+ # Separator between Ruby namespaces
14
+ #
15
+ # @since 0.0.1
16
+ # @api private
17
+ NAMESPACE_SEPARATOR = '::'.freeze
18
+
19
+ # Regexp for #tokenize
20
+ #
21
+ # @since 0.0.1
22
+ # @api private
23
+ TOKENIZE_REGEXP = /\((.*)\)/
24
+
25
+ # Separator for #tokenize
26
+ #
27
+ # @since 0.0.1
28
+ # @api private
29
+ TOKENIZE_SEPARATOR = '|'.freeze
30
+
31
+ # Separator for #underscore
32
+ #
33
+ # @since 0.0.1
34
+ # @api private
35
+ UNDERSCORE_SEPARATOR = '/'.freeze
36
+
37
+ # gsub second parameter used in #underscore
38
+ #
39
+ # @since 0.0.1
40
+ # @api private
41
+ UNDERSCORE_DIVISION_TARGET = '\1_\2'.freeze
42
+
43
+ # Separator for #titleize
44
+ #
45
+ # @since 0.0.1
46
+ # @api private
47
+ TITLEIZE_SEPARATOR = ' '.freeze
48
+
49
+ # Separator for Ruby file name
50
+ #
51
+ # @since 0.0.1
52
+ # @api private
53
+ CLASSIFY_SEPARATOR = '_'.freeze
54
+
55
+ # Separator for #capitalize
56
+ #
57
+ # @since 0.0.1
58
+ # @api private
59
+ CAPITALIZE_SEPARATOR = ' '.freeze
60
+
61
+ # Separator for #dasherize
62
+ #
63
+ # @since 0.0.1
64
+ # @api private
65
+ DASHERIZE_SEPARATOR = '-'.freeze
66
+
67
+ class << self
68
+ # Return a titleized version of the string
69
+ #
70
+ # @param input [::String] the input
71
+ #
72
+ # @return [::String] the transformed string
73
+ #
74
+ # @since 0.0.1
75
+ #
76
+ # @example
77
+ # require 'hugger/string'
78
+ #
79
+ # Hugger::String.titleize('hugger utils') # => "Hugger Utils"
80
+
81
+ def titleize(input)
82
+ string = new(input.to_s).titlize
83
+ new(string)
84
+ end
85
+
86
+ # Return a camelize version of the string
87
+ #
88
+ # @param input [::String] the input
89
+ #
90
+ # @return [::String] the transformed string
91
+ #
92
+ # @since 0.0.1
93
+ #
94
+ # @example
95
+ # require 'hugger/string'
96
+ #
97
+ # Hugger::String.camelize('hugger utils') # => "Hugger Utils"
98
+ def camelize(input)
99
+ string = ::String.new(input.to_s)
100
+ new(string).camelize
101
+ end
102
+
103
+ # Return a CamelCase version of the string
104
+ #
105
+ # @param input [::String] the input
106
+ #
107
+ # @return [::String] the transformed string
108
+ #
109
+ # @since 0.0.1
110
+ #
111
+ # @example
112
+ # require 'hugger/string'
113
+ #
114
+ # Hugger::String.classify('hugger_utils') # => 'HuggerUtils'
115
+ def classify(input)
116
+ string = ::String.new(input.to_s)
117
+ new(string).classify
118
+ end
119
+
120
+ # Return a downcased and underscore separated version of the string
121
+ #
122
+ # Revised version of `ActiveSupport::Inflector.underscore` implementation
123
+ # @see https://github.com/rails/rails/blob/feaa6e2048fe86bcf07e967d6e47b865e42e055b/activesupport/lib/active_support/inflector/methods.rb#L90
124
+ #
125
+ # @param input [::String] the input
126
+ #
127
+ # @return [::String] the transformed string
128
+ #
129
+ # @since 0.0.1
130
+ #
131
+ # @example
132
+ # require 'hugger/string'
133
+ #
134
+ # Hugger::String.underscore('HuggerUtils') # => 'hugger_utils'
135
+ def underscore(input)
136
+ string = ::String.new(input.to_s)
137
+ new(string).underscore
138
+ end
139
+
140
+ # Return a downcased and dash separated version of the string
141
+ #
142
+ # @param input [::String] the input
143
+ #
144
+ # @return [::String] the transformed string
145
+ #
146
+ # @since 0.0.1
147
+ #
148
+ # @example
149
+ # require 'hugger/string'
150
+ #
151
+ # Hugger::String.dasherize('Hugger Utils') # => 'hugger-utils'
152
+ #
153
+ # Hugger::String.dasherize('hugger_utils') # => 'hugger-utils'
154
+ #
155
+ # Hugger::String.dasherize('HuggerUtils') # => "hugger-utils"
156
+ def dasherize(input)
157
+ string = ::String.new(input.to_s)
158
+ new(string).dasherize
159
+ end
160
+
161
+ # Return the string without the Ruby namespace of the class
162
+ #
163
+ # @param input [::String] the input
164
+ #
165
+ # @return [::String] the transformed string
166
+ #
167
+ # @since 0.0.1
168
+ #
169
+ # @example
170
+ # require 'hugger/string'
171
+ #
172
+ # Hugger::String.demodulize('Hugger::String') # => 'String'
173
+ #
174
+ # Hugger::String.demodulize('String') # => 'String'
175
+ def demodulize(input)
176
+ string = ::String.new(input.to_s)
177
+ new(string).demodulize
178
+ end
179
+
180
+ # Return the top level namespace name
181
+ #
182
+ # @param input [::String] the input
183
+ #
184
+ # @return [::String] the transformed string
185
+ #
186
+ # @since 0.0.1
187
+ #
188
+ # @example
189
+ # require 'hugger/string'
190
+ #
191
+ # Hugger::String.namespace('Hugger::String') # => 'Hugger'
192
+ #
193
+ # Hugger::String.namespace('String') # => 'String'
194
+ def namespace(input)
195
+ ::String.new(input.to_s).split(NAMESPACE_SEPARATOR).first
196
+ end
197
+
198
+ # Return a pluralized version of self.
199
+ #
200
+ # @param input [::String] the input
201
+ #
202
+ # @return [::String] the pluralized string.
203
+ #
204
+ # @since 0.0.1
205
+ #
206
+ # @see Hugger::Inflector
207
+ #
208
+ # @example
209
+ # require 'hugger/string'
210
+ #
211
+ # Hugger::String.pluralize('book') # => 'books'
212
+ def pluralize(input)
213
+ string = ::String.new(input.to_s)
214
+ new(string).pluralize
215
+ end
216
+
217
+ # Return a singularized version of self.
218
+ #
219
+ # @param input [::String] the input
220
+ #
221
+ # @return [::String] the singularized string.
222
+ #
223
+ # @since 0.0.1
224
+ #
225
+ # @see Hugger::Inflector
226
+ #
227
+ # @example
228
+ # require 'hugger/string'
229
+ #
230
+ # Hugger::String.singularize('books') # => 'book'
231
+ def singularize(input)
232
+ string = ::String.new(input.to_s)
233
+ new(string).singularize
234
+ end
235
+
236
+ # Replace the rightmost match of `pattern` with `replacement`
237
+ #
238
+ # If the pattern cannot be matched, it returns the original string.
239
+ #
240
+ # This method does NOT mutate the original string.
241
+ #
242
+ # @param input [::String] the input
243
+ # @param pattern [Regexp, ::String] the pattern to find
244
+ # @param replacement [String] the string to replace
245
+ #
246
+ # @return [::String] the replaced string
247
+ #
248
+ # @since 0.0.1
249
+ #
250
+ # @example
251
+ # require 'hugger/string'
252
+ #
253
+ # Hugger::String.rsub('authors/books/index', %r{/}, '#')
254
+ # # => 'authors/books#index'
255
+ def rsub(input, pattern, replacement)
256
+ string = ::String.new(input.to_s)
257
+ if i = string.rindex(pattern) # rubocop:disable Lint/AssignmentInCondition
258
+ s = string.dup
259
+ s[i] = replacement
260
+ s
261
+ else
262
+ string
263
+ end
264
+ end
265
+ end
266
+
267
+ # Initialize the string
268
+ #
269
+ # @param string [::String, Symbol] the value we want to initialize
270
+ #
271
+ # @return [Hugger::String] self
272
+ #
273
+ # @since 0.0.1
274
+ def initialize(string)
275
+ @string = string.to_s
276
+ end
277
+
278
+ # Return a titleized version of the string
279
+ #
280
+ # @return [Hugger::String] the transformed string
281
+ #
282
+ # @since 0.0.1
283
+ #
284
+ # @example
285
+ # require 'hugger/string'
286
+ #
287
+ # string = Hugger::String.new 'hugger utils'
288
+ # string.titleize # => "Hugger Utils"
289
+ def titleize
290
+ self.class.new underscore.split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR)
291
+ end
292
+
293
+ # Return a camelize version of the string
294
+ #
295
+ # @param input [::String] the input
296
+ #
297
+ # @return [::String] the transformed string
298
+ #
299
+ # @since 0.0.1
300
+ #
301
+ # @example
302
+ # require 'hugger/string'
303
+ #
304
+ # Hugger::String.new('hugger utils').camelize # => "Hugger Utils"
305
+
306
+ def camelize
307
+ self.class.new(Hugger.inflector.camelize(@string))
308
+ end
309
+
310
+ # Return a CamelCase version of the string
311
+ #
312
+ # @return [Hugger::String] the transformed string
313
+ #
314
+ # @since 0.0.1
315
+ #
316
+ # @example
317
+ # require 'hugger/string'
318
+ #
319
+ # string = Hugger::String.new 'hugger_utils'
320
+ # string.classify # => 'HuggerUtils'
321
+ def classify
322
+ self.class.new(Hugger.inflector.classify(@string))
323
+ end
324
+
325
+ # Return a downcased and underscore separated version of the string
326
+ #
327
+ # Revised version of `ActiveSupport::Inflector.underscore` implementation
328
+ # @see https://github.com/rails/rails/blob/feaa6e2048fe86bcf07e967d6e47b865e42e055b/activesupport/lib/active_support/inflector/methods.rb#L90
329
+ #
330
+ # @return [Hugger::String] the transformed string
331
+ #
332
+ # @since 0.0.1
333
+ #
334
+ # @example
335
+ # require 'hugger/string'
336
+ #
337
+ # string = Hugger::String.new 'HuggerUtils'
338
+ # string.underscore # => 'hugger_utils'
339
+ def underscore
340
+ self.class.new Hugger.inflector.underscore(@string)
341
+ end
342
+
343
+ # Return a downcased and dash separated version of the string
344
+ #
345
+ # @return [Hugger::String] the transformed string
346
+ #
347
+ # @since 0.0.1
348
+ #
349
+ # @example
350
+ # require 'hugger/string'
351
+ #
352
+ # string = Hugger::String.new 'Hugger Utils'
353
+ # string.dasherize # => 'hugger-utils'
354
+ #
355
+ # string = Hugger::String.new 'hugger_utils'
356
+ # string.dasherize # => 'hugger-utils'
357
+ #
358
+ # string = Hugger::String.new 'HuggerUtils'
359
+ # string.dasherize # => "hugger-utils"
360
+ def dasherize
361
+ self.class.new(Hugger.inflector.dasherize(@string))
362
+ end
363
+
364
+ # Return the string without the Ruby namespace of the class
365
+ #
366
+ # @return [Hugger::String] the transformed string
367
+ #
368
+ # @since 0.0.1
369
+ #
370
+ # @example
371
+ # require 'hugger/string'
372
+ #
373
+ # string = Hugger::String.new 'Hugger::String'
374
+ # string.demodulize # => 'String'
375
+ #
376
+ # string = Hugger::String.new 'String'
377
+ # string.demodulize # => 'String'
378
+ def demodulize
379
+ self.class.new(Hugger.inflector.demodulize(@string))
380
+ end
381
+
382
+ # Return the top level namespace name
383
+ #
384
+ # @return [Hugger::String] the transformed string
385
+ #
386
+ # @since 0.0.1
387
+ #
388
+ # @example
389
+ # require 'hugger/string'
390
+ #
391
+ # string = Hugger::String.new 'Hugger::String'
392
+ # string.namespace # => 'Hugger'
393
+ #
394
+ # string = Hugger::String.new 'String'
395
+ # string.namespace # => 'String'
396
+ def namespace
397
+ self.class.new split(NAMESPACE_SEPARATOR).first
398
+ end
399
+
400
+ # It iterates through the tokens and calls the given block.
401
+ # A token is a substring wrapped by `()` and separated by `|`.
402
+ #
403
+ # @yield the block that is called for each token.
404
+ #
405
+ # @return [void]
406
+ #
407
+ # @since 0.0.1
408
+ #
409
+ # @example
410
+ # require 'hugger/string'
411
+ #
412
+ # string = Hugger::String.new 'Hugger::(Utils|App)'
413
+ # string.tokenize do |token|
414
+ # puts token
415
+ # end
416
+ #
417
+ # # =>
418
+ # 'Hugger'
419
+ # 'Hugger::App'
420
+ def tokenize # rubocop:disable Metrics/MethodLength
421
+ if match = TOKENIZE_REGEXP.match(@string) # rubocop:disable Lint/AssignmentInCondition
422
+ pre = match.pre_match
423
+ post = match.post_match
424
+ tokens = match[1].split(TOKENIZE_SEPARATOR)
425
+ tokens.each do |token|
426
+ yield(self.class.new("#{pre}#{token}#{post}"))
427
+ end
428
+ else
429
+ yield(self.class.new(@string))
430
+ end
431
+
432
+ nil
433
+ end
434
+
435
+ # Return a pluralized version of self.
436
+ #
437
+ # @return [Hugger::String] the pluralized string.
438
+ #
439
+ # @api private
440
+ # @since 0.0.1
441
+ def pluralize
442
+ self.class.new Hugger.inflector.pluralize(self)
443
+ end
444
+
445
+ # Return a singularized version of self.
446
+ #
447
+ # @return [Hugger::String] the singularized string.
448
+ #
449
+ # @api private
450
+ # @since 0.0.1
451
+ def singularize
452
+ self.class.new Hugger.inflector.singularize(self)
453
+ end
454
+
455
+ # Returns the hash of the internal string
456
+ #
457
+ # @return [Integer]
458
+ #
459
+ # @since 0.0.1
460
+ def hash
461
+ @string.hash
462
+ end
463
+
464
+ # Returns a string representation
465
+ #
466
+ # @return [::String]
467
+ #
468
+ # @since 0.0.1
469
+ def to_s
470
+ @string
471
+ end
472
+
473
+ alias to_str to_s
474
+
475
+ # Equality
476
+ #
477
+ # @return [TrueClass,FalseClass]
478
+ #
479
+ # @since 0.0.1
480
+ def ==(other)
481
+ to_s == other
482
+ end
483
+
484
+ alias eql? ==
485
+
486
+ # Split the string with the given pattern
487
+ #
488
+ # @return [Array<Hugger::String>]
489
+ #
490
+ # @see http://www.ruby-doc.org/core/String.html#method-i-split
491
+ #
492
+ # @since 0.0.1
493
+ def split(pattern, limit = 0)
494
+ @string.split(pattern, limit).map { |s| self.class.new(s) }
495
+ end
496
+
497
+ # Replace the given pattern with the given replacement
498
+ #
499
+ # @return [Hugger::String]
500
+ #
501
+ # @see http://www.ruby-doc.org/core/String.html#method-i-gsub
502
+ #
503
+ # @since 0.0.1
504
+ def gsub(pattern, replacement = nil, &blk)
505
+ s = if block_given?
506
+ @string.gsub(pattern, &blk)
507
+ else
508
+ @string.gsub(pattern, replacement)
509
+ end
510
+ self.class.new(s)
511
+ end
512
+
513
+ # Iterate through the string, matching the pattern.
514
+ # Either return all those patterns, or pass them to the block.
515
+ #
516
+ # @return [Array<Hugger::String>]
517
+ #
518
+ # @see http://www.ruby-doc.org/core/String.html#method-i-scan
519
+ #
520
+ # @since 0.0.1
521
+ def scan(pattern, &blk)
522
+ @string.scan(pattern, &blk).map { |s| self.class.new(s) }
523
+ end
524
+
525
+ # Replace the rightmost match of `pattern` with `replacement`
526
+ #
527
+ # If the pattern cannot be matched, it returns the original string.
528
+ #
529
+ # This method does NOT mutate the original string.
530
+ #
531
+ # @param pattern [Regexp, String] the pattern to find
532
+ # @param replacement [String, Hugger::String] the string to replace
533
+ #
534
+ # @return [Hugger::String] the replaced string
535
+ #
536
+ # @since 0.0.1
537
+ #
538
+ # @example
539
+ # require 'hugger/string'
540
+ #
541
+ # string = Hugger::String.new('authors/books/index')
542
+ # result = string.rsub(/\//, '#')
543
+ #
544
+ # puts string
545
+ # # => #<Hugger::String:0x007fdb41233ad8 @string="authors/books/index">
546
+ #
547
+ # puts result
548
+ # # => #<Hugger::String:0x007fdb41232ed0 @string="authors/books#index">
549
+ def rsub(pattern, replacement)
550
+ if i = rindex(pattern) # rubocop:disable Lint/AssignmentInCondition
551
+ s = @string.dup
552
+ s[i] = replacement
553
+ self.class.new s
554
+ else
555
+ self
556
+ end
557
+ end
558
+
559
+ # Override Ruby's method_missing in order to provide ::String interface
560
+ #
561
+ # @api private
562
+ # @since 0.0.1
563
+ #
564
+ # @raise [NoMethodError] If doesn't respond to the given method
565
+ # rubocop:disable Style/MethodMissing
566
+ def method_missing(method_name, *args, &blk)
567
+ raise NoMethodError, %(undefined method `#{method_name}' for "#{@string}":#{self.class}) unless respond_to_missing?(method_name)
568
+
569
+ s = @string.__send__(method_name, *args, &blk)
570
+ case s
571
+ when ::String
572
+ self.class.new(s)
573
+ when Array
574
+ s.map { |p| self.class.new(p) }
575
+ else
576
+ s
577
+ end
578
+ end
579
+
580
+ # rubocop:enable Style/MethodMissing
581
+
582
+ # Override Ruby's respond_to_missing? in order to support ::String interface
583
+ #
584
+ # @api private
585
+ # @since 0.0.1
586
+ def respond_to_missing?(method_name, include_private = false)
587
+ @string.respond_to?(method_name, include_private)
588
+ end
589
+
590
+ # Private internal interface for Hugger::Object
591
+ #
592
+ # @api private
593
+ # @since 0.0.1
594
+ def object
595
+ @string
596
+ end
597
+
598
+ # inspect
599
+ #
600
+ # @api private
601
+ # @since 0.0.1
602
+ def inspect
603
+ object.inspect
604
+ end
605
+ end
606
+ end
@@ -0,0 +1,3 @@
1
+ module Hugger
2
+ VERSION = '0.0.1'.freeze
3
+ end
data/lib/hugger.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'hugger/version'
2
+
3
+ require 'dry/inflector'
4
+ require 'hugger/object'
5
+ require 'hugger/string'
6
+ require 'hugger/decoratable'
7
+
8
+ # Module
9
+ # @since 0.0.1
10
+ module Hugger
11
+ class << self
12
+ def inflector
13
+ @inflector ||= Dry::Inflector.new
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,205 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hugger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Akira Takahashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-inflector
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: codeclimate-test-reporter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: test-unit-full
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: yard
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Hugger is utility wrapper classes
154
+ email:
155
+ - rike422@gmail.com
156
+ executables:
157
+ - console
158
+ - setup
159
+ extensions: []
160
+ extra_rdoc_files: []
161
+ files:
162
+ - ".document"
163
+ - ".gitignore"
164
+ - ".rubocop.yml"
165
+ - ".travis.yml"
166
+ - ".yardopts"
167
+ - CODE_OF_CONDUCT.md
168
+ - ChangeLog.md
169
+ - Gemfile
170
+ - LICENSE.txt
171
+ - README.md
172
+ - Rakefile
173
+ - bin/console
174
+ - bin/setup
175
+ - hagoromo.gemspec
176
+ - lib/hugger.rb
177
+ - lib/hugger/decoratable.rb
178
+ - lib/hugger/object.rb
179
+ - lib/hugger/string.rb
180
+ - lib/hugger/version.rb
181
+ homepage: https://github.com/rike422/hugger
182
+ licenses:
183
+ - MIT
184
+ metadata: {}
185
+ post_install_message:
186
+ rdoc_options: []
187
+ require_paths:
188
+ - lib
189
+ required_ruby_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ required_rubygems_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ requirements: []
200
+ rubyforge_project:
201
+ rubygems_version: 2.6.11
202
+ signing_key:
203
+ specification_version: 4
204
+ summary: Hugger is utility wrapper classes
205
+ test_files: []