token_string 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +86 -0
  4. data/lib/token_string.rb +216 -0
  5. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 53556486561d1dffead617ffce1d46ed8f529ead
4
+ data.tar.gz: 8240e2bbd64b9d12e477cab348326edd8bcaf51e
5
+ SHA512:
6
+ metadata.gz: 54875ab90d4fd9c5c142c91161ffff930ff92bcf36b1536d44a6516744db38cb4da1d860f9f67dd36951ffc16c49076992361dd54f956fc7f205e1a0279677fc
7
+ data.tar.gz: 4dfae4665483113ef0a26d366b00a7d31f8a64cbd76273f99e7ec779a859fd7e473b2ff0959951a6eab9ad3eb95c6246955ed111ca2d23f1ad278660d1a479ac
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Gyula László
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ token_string
2
+ ============
3
+
4
+ Provides a way to deal with CamelCase, snake_case and more in a more elegant way then ActiveSupport::Inflector.
5
+
6
+
7
+ examples of usage:
8
+ ```ruby
9
+
10
+ irb(main):001:0> require 'token_string'
11
+ => true
12
+
13
+ # Use TokenString.from <format>, <string> to create a tokenized version
14
+ # of a string.
15
+
16
+ irb(main):002:0> a = TokenString.from :camel, 'PropertyDb'
17
+ => #<TokenString::Impl:0x007fce5383a7b0 @format=:camel, @tokens=["Property", "Db"]>
18
+ irb(main):003:0> b = TokenString.make :snake, 'mkz','monkeyplug'
19
+ => #<TokenString::Impl:0x007fce5309ef38 @format=:snake, @tokens=["mkz", "monkeyplug"]>
20
+
21
+ # Add a prefix to the string. The ! on the end indicates that this
22
+ # version of prefix will clone the array of tokens then alters and returns the clone.
23
+
24
+ irb(main):004:0> r = a.prefix!(b)
25
+ => #<TokenString::Impl:0x007fce5308f240 @format=:camel, @tokens=["mkz", "monkeyplug", "Property", "Db"]>
26
+
27
+ # The format of the clone stays the same
28
+ irb(main):005:0> r.format
29
+ => :camel
30
+
31
+ # We can access the formatted version using to_s
32
+
33
+ irb(main):006:0> r.to_s
34
+ => "MkzMonkeyplugPropertyDb"
35
+
36
+ # derive(<prefix>, <postfixes>...) creates a clone and adds <prefix> to
37
+ # the beginning and <postifixes to the end.
38
+
39
+ irb(main):007:0> r.derive("make", "object", "controller")
40
+ => #<TokenString::Impl:0x007fce53064838 @format=:camel, @tokens=["make", "mkz", "monkeyplug", "Property", "Db", "object", "controller"]>
41
+
42
+ # Calling #snake #human #const or #camel on the object returns a copy of
43
+ # the object with the new format.
44
+ #
45
+ # WARNING: adding prefixes and postfixes without cloning the objct first
46
+ # modifies the existing token list in that object.
47
+
48
+ irb(main):008:0> r.snake
49
+ # => #<TokenString::Impl:0x007fce53055180 @format=:snake, @tokens=["mkz", "monkeyplug", "Property", "Db"]>
50
+ irb(main):009:0> t = r.derive("make", "object", "controller")
51
+ # => #<TokenString::Impl:0x007fce53036a00 @format=:camel, @tokens=["make", "mkz", "monkeyplug", "Property", "Db", "object", "controller"]>
52
+ irb(main):010:0> t.format
53
+ # => :camel
54
+ irb(main):011:0> ts = t.snake
55
+ # => #<TokenString::Impl:0x007fce530258e0 @format=:snake, @tokens=["make", "mkz", "monkeyplug", "Property", "Db", "object", "controller"]>
56
+ irb(main):013:0> ts.to_s
57
+ # => "make_mkz_monkeyplug_property_db_object_controller"
58
+ irb(main):014:0> r.to_s
59
+ # => "MkzMonkeyplugPropertyDb"
60
+ irb(main):015:0> r.force(:snake)
61
+ # => #<TokenString::Impl:0x007fce5308f240 @format=:snake, @tokens=["mkz", "monkeyplug", "Property", "Db"]>
62
+ irb(main):016:0> r.to_s
63
+ # => "mkz_monkeyplug_property_db"
64
+ irb(main):017:0> v = r.const
65
+ # => #<TokenString::Impl:0x007fce53aeb518 @format=:const, @tokens=["mkz", "monkeyplug", "Property", "Db"]>
66
+ irb(main):018:0> v.to_s
67
+ # => "MKZ_MONKEYPLUG_PROPERTY_DB"
68
+ irb(main):019:0> r.to_s
69
+ # => "mkz_monkeyplug_property_db"
70
+ irb(main):020:0> v == r
71
+ # => true
72
+
73
+ ```
74
+
75
+ Built-in formats
76
+ ================
77
+
78
+ Currently the built in formats:
79
+ * :camel => CamelCase
80
+ * :snake => snake_case
81
+ * :human => Human Case
82
+ * :const => CONST_CASE
83
+
84
+
85
+ But declaring new formats is easy (look at the end of token_string.rb).
86
+
@@ -0,0 +1,216 @@
1
+ class TokenString
2
+
3
+ class Format
4
+
5
+ # Convert an input string in this format to a token list and validate it
6
+ def convert_from str
7
+ t = tokenize( str ).reject {|tok| tok == nil || tok.empty?}
8
+ unless t.all?{|tok| validate(tok)}
9
+ raise ArgumentError.new("Bad token in input: #{str.inspect} (tokens: #{t.inspect})")
10
+ end
11
+ t
12
+ end
13
+
14
+ # Converts the tokens to this format.
15
+ def convert_to tokens
16
+ join( tokens.map{|t| process(t.to_s) })
17
+ end
18
+
19
+ # Validate each token. For now its just to check if they are an identifier.
20
+ def validate token
21
+ token =~ /[A-Za-z0-9]/
22
+ end
23
+ end
24
+
25
+
26
+ # Register a Format.
27
+ #
28
+ # Registering a format adds that format to the available formats and
29
+ # creates two accessors on Impl instances: the name of the #<format>
30
+ # which creates a copy of the Impl with the requested format,
31
+ # and #<format>! which clones then sets the format.
32
+ def self.add name, format
33
+ @converters = {} unless @converters
34
+ @converters[name] = format
35
+ # Register a converter for Impl
36
+ Impl.class_eval([
37
+ "def #{name}; Impl.new(:#{name}, @tokens); end",
38
+ "def #{name}!; clone.force(:#{name}); end",
39
+ ].join("\n") )
40
+ end
41
+
42
+ # Helper to convert a list of tokens to a specific format
43
+ def self.convert_tokens_to( stream, format )
44
+ c_to = @converters[format]
45
+ return str unless c_to
46
+ c_to.convert_to( stream )
47
+ end
48
+
49
+ # Gets a list of all the known formats.
50
+ def self.known_formats
51
+ @converters.keys
52
+ end
53
+
54
+ # Creates a new TokenString from the given string using the
55
+ # supplied format.
56
+ #
57
+ # If no such format is registered, the method throws
58
+ # an NoSuchConverterError.
59
+ #
60
+ # If the converter finds the string invalid, it throws
61
+ # an ArgumentError.
62
+ def self.from( format, string )
63
+ converter = @converters[format]
64
+ unless converter
65
+ raise NoSuchConverterError.new(
66
+ "Unknown format for input: #{format.inspect}." +
67
+ "Available formats: #{@converters.keys.inspect}")
68
+ end
69
+ Impl.new( format, converter.convert_from( string ) )
70
+ end
71
+
72
+
73
+ # Construct a new TokenString from the format and tokens.
74
+ def self.make( format, *tokens )
75
+ Impl.new( format, linearize_tokens(tokens.flatten) )
76
+ end
77
+
78
+
79
+ # The value type that implements the whole shebang.
80
+ class Impl
81
+ attr_reader :format, :tokens
82
+
83
+ #:nodoc:
84
+ def initialize( format, tokens)
85
+ # this should be called only from TokenString.from & TokenString.make
86
+ @format, @tokens = format, tokens
87
+ end
88
+
89
+ # "cast" this Imple to a different type.
90
+ # These casts use the same underlying buffer to be cheap.
91
+ def to(new_format)
92
+ Impl.new( new_format, tokens )
93
+ end
94
+
95
+
96
+ # Make this Impl a different type.
97
+ #
98
+ # This method simply sets format to the given new
99
+ # format.
100
+ def force(new_format)
101
+ @format = new_format
102
+ self
103
+ end
104
+
105
+ # Convert to string
106
+ def to_s
107
+ TokenString.convert_tokens_to( @tokens, @format )
108
+ end
109
+
110
+ # Two such strings should be equal independent of the representation
111
+ def ==(other)
112
+ return false unless other
113
+ other.tokens == tokens
114
+ end
115
+
116
+ # Clones
117
+ def clone
118
+ Impl.new( format, tokens.map{|t|t})
119
+ end
120
+
121
+ # Editing the contents of the string
122
+ # ----------------------------------
123
+
124
+ # Appends a token to the beginning to the tokens
125
+ def prefix( *pf )
126
+ tokens.unshift(*TokenString.linearize_tokens(pf))
127
+ self
128
+ end
129
+
130
+ # Equal to calling .clone.prefix(...)
131
+ def prefix!( *pf )
132
+ clone.prefix(*pf)
133
+ end
134
+
135
+ # Appends a token to the end of the tokens
136
+ def postfix( *pf )
137
+ tokens.push(*TokenString.linearize_tokens(pf))
138
+ self
139
+ end
140
+
141
+ # Equal to calling .clone.postfix(...)
142
+ def postfix!( *pf )
143
+ clone.postfix(*pf)
144
+ end
145
+
146
+ # Equal to calling prefix(<prefix>).postfix(<postfix>)
147
+ def surround( prefix_, *postfix_ )
148
+ prefix( prefix_ )
149
+ postfix( *postfix_ ) unless postfix_.empty?
150
+ self
151
+ end
152
+
153
+ # Equal to calling clone.surround()
154
+ def derive!(*args)
155
+ clone.surround( *args)
156
+ end
157
+
158
+ alias :derive :derive!
159
+
160
+
161
+ # Some string methods that may get called on us
162
+ def lines; [to_s]; end
163
+
164
+ end
165
+
166
+ class NoSuchConverterError < ArgumentError
167
+ def initialize msg, *args
168
+ super(msg, *args)
169
+ end
170
+ end
171
+
172
+ private
173
+ #
174
+ # :nodoc
175
+ def self.linearize_tokens toks
176
+ o = []
177
+ return o unless toks
178
+ toks.each do |tok|
179
+ case
180
+ when !tok then # nothing...
181
+ when tok.is_a?(Impl) then o.push(*linearize_tokens(tok.tokens))
182
+ else o << tok
183
+ end
184
+ end
185
+ o
186
+ end
187
+ end
188
+
189
+ class HumanFormat < TokenString::Format
190
+ def tokenize(str); str.split( /\s+/); end
191
+ def process(token); token.capitalize; end
192
+ def join(tokens); tokens.join(' '); end
193
+ end
194
+
195
+ class SnakeCase < TokenString::Format
196
+ def tokenize(str); str.split( /_+/); end
197
+ def process(token); token.downcase; end
198
+ def join(tokens); tokens.join('_'); end
199
+ end
200
+
201
+ class CamelCase < TokenString::Format
202
+ def tokenize(str); str.gsub( /[A-Z]+/, '_\0' ).split(/_+/); end
203
+ def process(token); token.capitalize; end
204
+ def join(tokens); tokens.join(''); end
205
+ end
206
+
207
+ class ConstCase < TokenString::Format
208
+ def tokenize(str); str.split(/_+/); end
209
+ def process(token); token.upcase; end
210
+ def join(tokens); tokens.join('_'); end
211
+ end
212
+
213
+ TokenString.add :human, HumanFormat.new
214
+ TokenString.add :const, ConstCase.new
215
+ TokenString.add :camel, CamelCase.new
216
+ TokenString.add :snake, SnakeCase.new
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: token_string
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Gyula Laszlo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Provides a way to deal with CamelCase, snake_case and more in a more
14
+ elegant way then ActiveSupport::Inflector
15
+ email: gyula.laszlo.gm@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/token_string.rb
21
+ - LICENSE
22
+ - README.md
23
+ homepage: https://github.com/gyulalaszlo/token_string
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.0.14
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: TokenString
47
+ test_files: []