simple_inflector 0.0.1
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/README.md +30 -0
- data/lib/simple_inflector.rb +36 -0
- data/lib/simple_inflector/core_ext.rb +2 -0
- data/lib/simple_inflector/core_ext/string.rb +16 -0
- data/lib/simple_inflector/version.rb +13 -0
- metadata +50 -0
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
A simple no-frills "inflector".
|
2
|
+
|
3
|
+
Provides methods to transform class names to file names.
|
4
|
+
|
5
|
+
Author: Philipp Kempgen, [http://kempgen.net](http://kempgen.net)
|
6
|
+
|
7
|
+
## Examples
|
8
|
+
"FooBar" .underscore #=> "foo_bar"
|
9
|
+
"FooBar::Baz" .underscore #=> "foo_bar/baz"
|
10
|
+
"::FooBar::Baz" .underscore #=> "foo_bar/baz"
|
11
|
+
"HTTPRequest" .underscore #=> "http_request"
|
12
|
+
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
If you want to use the extensions to core classes (`String`):
|
17
|
+
|
18
|
+
require 'simple_inflector/core_ext/string'
|
19
|
+
puts "FooBar".underscore #=> "foo_bar"
|
20
|
+
|
21
|
+
or
|
22
|
+
|
23
|
+
require 'simple_inflector/core_ext'
|
24
|
+
puts "FooBar".underscore #=> "foo_bar"
|
25
|
+
|
26
|
+
otherwise
|
27
|
+
|
28
|
+
require 'simple_inflector'
|
29
|
+
::SimpleInflector.underscore( "FooBar" ) #=> "foo_bar"
|
30
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# The inflector transforms class names etc.
|
2
|
+
#
|
3
|
+
module ::SimpleInflector
|
4
|
+
|
5
|
+
# Makes an underscored, lowercase form from a camel-case
|
6
|
+
# class name (that may include a namespace).
|
7
|
+
#
|
8
|
+
# Changes "`::`" to "`/`" to convert namespaces to (file
|
9
|
+
# system) paths (to be used for `require`).
|
10
|
+
#
|
11
|
+
# Examples:
|
12
|
+
# "FooBar" .underscore #=> "foo_bar"
|
13
|
+
# "FooBar::Baz" .underscore #=> "foo_bar/baz"
|
14
|
+
# "::FooBar::Baz" .underscore #=> "foo_bar/baz"
|
15
|
+
# "HTTPRequest" .underscore #=> "http_request"
|
16
|
+
#
|
17
|
+
# @param klassname [String] Class name (may include module name).
|
18
|
+
# @return [String] Underscored name.
|
19
|
+
#
|
20
|
+
def underscore( klassname )
|
21
|
+
s = klassname.to_s.dup
|
22
|
+
s.gsub!( /^::/, '' ) # get rid of leading "::"
|
23
|
+
s.gsub!( /::/, '/' ) # replace namespace separator by "/"
|
24
|
+
s.gsub!( /([A-Z0-9]+)([A-Z][a-z])/x ,'\\1_\\2' ) # un-camel-case
|
25
|
+
s.gsub!( /([a-z0-9] )([A-Z] )/x ,'\\1_\\2' ) # "
|
26
|
+
s.tr!( '-', '_' ) # replace hyphens by underscores
|
27
|
+
s.downcase! # lowercase. who would have thought?
|
28
|
+
s
|
29
|
+
end
|
30
|
+
|
31
|
+
extend self # make instance methods available as class methods
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'simple_inflector/version'
|
36
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'simple_inflector'
|
2
|
+
|
3
|
+
class ::String
|
4
|
+
|
5
|
+
# Makes an underscored, lowercase form from `self`.
|
6
|
+
#
|
7
|
+
# See {::SimpleInflector#underscore}.
|
8
|
+
#
|
9
|
+
# @return [String] Underscored name.
|
10
|
+
#
|
11
|
+
def underscore
|
12
|
+
::SimpleInflector.underscore( self )
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# This file should not require anything, as it's loaded in the
|
2
|
+
# `*.gemspec` file.
|
3
|
+
|
4
|
+
module ::SimpleInflector
|
5
|
+
|
6
|
+
# The version of this Gem.
|
7
|
+
VERSION = [ 0, 0, 1 ].freeze
|
8
|
+
|
9
|
+
# The version of this Gem as a string.
|
10
|
+
VERSION_STR = VERSION.map(& :to_s).join('.').freeze
|
11
|
+
|
12
|
+
end
|
13
|
+
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_inflector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Philipp Kempgen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-17 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Provides methods to transform class names to file names.
|
15
|
+
email:
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/simple_inflector/core_ext/string.rb
|
21
|
+
- lib/simple_inflector/core_ext.rb
|
22
|
+
- lib/simple_inflector/version.rb
|
23
|
+
- lib/simple_inflector.rb
|
24
|
+
- README.md
|
25
|
+
homepage: https://github.com/philipp-kempgen/simple_inflector
|
26
|
+
licenses: []
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 1.8.25
|
46
|
+
signing_key:
|
47
|
+
specification_version: 3
|
48
|
+
summary: A simple no-frills "inflector".
|
49
|
+
test_files: []
|
50
|
+
has_rdoc:
|