classify 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +5 -0
- data/Gemfile +4 -0
- data/README.markdown +33 -0
- data/Rakefile +2 -0
- data/classify.gemspec +22 -0
- data/lib/classify.rb +18 -0
- data/spec/classify_spec.rb +33 -0
- data/spec/spec_helper.rb +6 -0
- metadata +76 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Classify
|
2
|
+
|
3
|
+
Converts strings into constants using Ruby.
|
4
|
+
|
5
|
+
Something similar to Rails' [classify](http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-classify) method.
|
6
|
+
|
7
|
+
## How do use
|
8
|
+
|
9
|
+
require 'rubygems'
|
10
|
+
require 'classify'
|
11
|
+
|
12
|
+
>> class MyRandomString; end
|
13
|
+
>> Classify.it!("my_random_string")
|
14
|
+
=> MyRandomString
|
15
|
+
|
16
|
+
## How do install
|
17
|
+
|
18
|
+
[sudo] gem install classify
|
19
|
+
|
20
|
+
## How to use it in a rails 3 project
|
21
|
+
|
22
|
+
Add `gem 'classify'` to your Gemfile and run `bundle`.
|
23
|
+
|
24
|
+
## How to help
|
25
|
+
|
26
|
+
- Start by copying the project or make your own branch.
|
27
|
+
- Navigate to the root path of the project and run `bundle`.
|
28
|
+
- Start by running all tests using rspec, `rspec spec/classify_spec.rb`.
|
29
|
+
- Implement your own code, write some tests, commit and do a pull request.
|
30
|
+
|
31
|
+
## Requirements
|
32
|
+
|
33
|
+
Classify is tested in OS X 10.6.6 using Ruby 1.8.7 and 1.9.2.
|
data/Rakefile
ADDED
data/classify.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "classify"
|
6
|
+
s.version = "0.0.2"
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Linus Oleander"]
|
9
|
+
s.email = ["linus@oleander.nu"]
|
10
|
+
s.homepage = "https://github.com/oleander/classify"
|
11
|
+
s.summary = %q{Converts strings into constants using Ruby}
|
12
|
+
s.description = %q{Converts strings into constants using Ruby. Something simular to Rails' classify method.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "classify"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency('rspec')
|
22
|
+
end
|
data/lib/classify.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
class Classify
|
2
|
+
def self.it!(string)
|
3
|
+
raise ArgumentError, "Wrong argument #{string.inspect}" if not string.class == String or string.match(/(^\_)|(\_$)|([^a-z0-9\_])/i)
|
4
|
+
Classify.new.camelize(string)
|
5
|
+
end
|
6
|
+
|
7
|
+
def try_converting(string)
|
8
|
+
unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ string
|
9
|
+
raise ArgumentError, "#{string} is not a valid constant name!"
|
10
|
+
end
|
11
|
+
|
12
|
+
Object.module_eval("::#{$1}", __FILE__, __LINE__)
|
13
|
+
end
|
14
|
+
|
15
|
+
def camelize(string)
|
16
|
+
self.try_converting(string.gsub(/^(.{1})|\_.{1}/) { |s| s.gsub(/[^a-z0-9]+/i, '').capitalize })
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
module Therandomstring; end
|
5
|
+
module MyRandomString; end
|
6
|
+
module A1; end
|
7
|
+
module My1A; end
|
8
|
+
|
9
|
+
describe Classify do
|
10
|
+
it "should return a constant" do
|
11
|
+
Classify.it!("my_random_string").should eq(MyRandomString)
|
12
|
+
Classify.it!("a_1").should eq(A1)
|
13
|
+
Classify.it!("my_1_a").should eq(My1A)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should raise an error if the param contains non valid data" do
|
17
|
+
["my::string", "_value_string", "my_string ", "string_ö_string", nil, Object, "", 1, "1_string", "string_", "_string_"].each do |string|
|
18
|
+
lambda {
|
19
|
+
Classify.it!(string)
|
20
|
+
}.should raise_error(ArgumentError)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should work without underscore" do
|
25
|
+
Classify.it!("therandomstring").should eq(Therandomstring)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should work on a half done string" do
|
29
|
+
["my_randomString", "myRandomString", "MyRandomString", "My_Random_String"].each do |string|
|
30
|
+
Classify.it!(string).should eq(MyRandomString)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: classify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Linus Oleander
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-02-18 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
description: Converts strings into constants using Ruby. Something simular to Rails' classify method.
|
28
|
+
email:
|
29
|
+
- linus@oleander.nu
|
30
|
+
executables: []
|
31
|
+
|
32
|
+
extensions: []
|
33
|
+
|
34
|
+
extra_rdoc_files: []
|
35
|
+
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rspec
|
39
|
+
- Gemfile
|
40
|
+
- README.markdown
|
41
|
+
- Rakefile
|
42
|
+
- classify.gemspec
|
43
|
+
- lib/classify.rb
|
44
|
+
- spec/classify_spec.rb
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: https://github.com/oleander/classify
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: classify
|
70
|
+
rubygems_version: 1.5.0
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Converts strings into constants using Ruby
|
74
|
+
test_files:
|
75
|
+
- spec/classify_spec.rb
|
76
|
+
- spec/spec_helper.rb
|