underglow 0.0.5

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/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in underglow.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ Underglow
2
+ =========
3
+ Provides some luxury methods and extensions. I personally use this on all my projects to make my life easier.
4
+
5
+ Installation
6
+ ============
7
+
8
+ gem install underglow
9
+
10
+ Testing
11
+ =======
12
+
13
+ rspec spec/
14
+
15
+ Contributing
16
+ ============
17
+ If you find this useful, sweet! If you'd like to improve upon it, that's legit bro.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ # Load underglow tasks for development and testing purposes
4
+ Dir[File.join(File.dirname(__FILE__), 'lib/tasks/*.rake')].each { |f| load f }
data/lib/tasks/db.rake ADDED
@@ -0,0 +1,50 @@
1
+ namespace :db do
2
+ # This works on sqlite, mysql and postgresql databases
3
+ desc "Drop all database connections"
4
+ task drop_connections: :environment do
5
+ if ENV['RAILS_ENV'].present?
6
+ environments = [ENV['RAILS_ENV']]
7
+ else
8
+ environments = ["development", "test"]
9
+ end
10
+
11
+ environments.each do |environment|
12
+ config = Rails.application.config.database_configuration[environment]
13
+
14
+ case config['adapter']
15
+ when /mysql/
16
+ ActiveRecord::Base.establish_connection(config)
17
+ ActiveRecord::Base.connection.drop_database config['database']
18
+ when /^sqlite/
19
+ require 'pathname'
20
+ path = Pathname.new(config['database'])
21
+ file = path.absolute? ? path.to_s : File.join(Rails.root, path)
22
+
23
+ FileUtils.rm(file)
24
+ when 'postgresql'
25
+ ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by procpid;").each do |x|
26
+ if config['database'] == x['datname'] && x['current_query'] == '<IDLE>'
27
+ ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['procpid']})")
28
+ end
29
+ end
30
+ ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
31
+ ActiveRecord::Base.connection.drop_database config['database']
32
+ end
33
+ end
34
+ end
35
+
36
+ desc "Recreates and migrates db useful for development"
37
+ task :fresh do
38
+ # We need to keep track of initial RAILS_ENV because db:test:load changes RAILS_ENV to test
39
+ RAILS_ENV = ENV['RAILS_ENV'] || "development"
40
+
41
+ Rake::Task['db:drop_connections'].invoke
42
+ Rake::Task['db:drop'].invoke
43
+ Rake::Task['db:create'].invoke
44
+ Rake::Task['db:migrate'].invoke
45
+ Rake::Task['db:test:load'].invoke if RAILS_ENV == "development"
46
+
47
+ # set the RAILS_ENV back to initial
48
+ ENV['RAILS_ENV'] = RAILS_ENV
49
+ end
50
+ end
@@ -0,0 +1,19 @@
1
+ namespace :system do
2
+ desc "Kill process with pid in *.pid file"
3
+ task :kill_pid_from_file, :path do |t, args|
4
+ pid_path = args[:path]
5
+
6
+ if File.exists?(pid_path) # if the process is running at all
7
+ pid = File.read(pid_path).to_i # Get pid from file
8
+
9
+ begin
10
+ Process.kill("TERM", pid) # then kill it
11
+ rescue
12
+ # If process not found, do nothing
13
+ end
14
+
15
+ # remove the pid file
16
+ File.delete(pid_path)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails'
2
+
3
+ # All thin operations
4
+ namespace :thin do
5
+ desc "Stop thin server"
6
+ task :stop => :environment do
7
+ puts "Stopping thin server..."
8
+ Rake::Task['system:kill_pid_from_file'].invoke(Rails.root.join('tmp', 'pids', 'thin.pid'))
9
+ end
10
+
11
+ desc "Start thin server"
12
+ task :start do
13
+ puts "Starting thin server..."
14
+ puts `bundle exec thin start -d --pid tmp/pids/thin.pid` # daemonize
15
+ end
16
+
17
+ desc "Restart thin server"
18
+ task :restart => ['thin:stop', 'thin:start']
19
+ end
File without changes
@@ -0,0 +1,118 @@
1
+ class String
2
+ def numeric?
3
+ true if Float(self) rescue false
4
+ end
5
+
6
+ def url?
7
+ return true if %r{(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))}.match(self)
8
+
9
+ false
10
+ end
11
+
12
+ # Coerces to Float or Fixnum otherwise String
13
+ # If no type is given, it will determine the type to coerce to
14
+ # If type is given (the standard type symbols like :integer, :string, etc), it will coerce to that type
15
+ def coerce(type = nil)
16
+ if type.nil?
17
+ if numeric?
18
+ self.strip.match(/^\d+$/) ? self.to_i : self.to_f
19
+ elsif self.match(/true/i)
20
+ true
21
+ elsif self.match(/false/i)
22
+ false
23
+ else
24
+ self
25
+ end
26
+ else
27
+ type = type.to_sym
28
+
29
+ case type
30
+ when :string
31
+ self
32
+ when :integer
33
+ self.to_i
34
+ when :float
35
+ self.to_f
36
+ when :boolean # only true matches to true, else false for everything
37
+ if self.match(/true/i)
38
+ true
39
+ else
40
+ false
41
+ end
42
+ else # unknown type, just return string
43
+ self
44
+ end
45
+ end
46
+ end
47
+
48
+ # only capitalize initial letter and leave the rest alone
49
+ def initial_capitalize
50
+ str = self
51
+ str[0] = str[0].chr.capitalize
52
+
53
+ str
54
+ end
55
+
56
+ def urlize
57
+ downcase.gsub("_", "-")
58
+ end
59
+
60
+ def deurlize
61
+ gsub("-", "_")
62
+ end
63
+
64
+ # deurlizes to symbol
65
+ def deurlize_to_sym
66
+ deurlize.downcase.to_sym
67
+ end
68
+
69
+ # make it suitable for html attributes
70
+ def html_attributify
71
+ downcase.gsub(/[_\/\s]/, "-").gsub(/[^0-9a-z\-]+/, "")
72
+ end
73
+
74
+ # Concat another string and overlap it if it does
75
+ def overlap(b)
76
+ a = self
77
+ a_len = self.length
78
+ b_len = b.length
79
+ n = nil
80
+
81
+ (0..a_len-1).each do |i|
82
+ j = i
83
+ k = 0
84
+
85
+ while j < a_len and k < b_len and a[j] == b[k]
86
+ j += 1
87
+ k += 1
88
+ end
89
+
90
+ n = k and break if j == a_len
91
+ end
92
+
93
+ n ||= 0
94
+
95
+ a + b[n..b_len-1]
96
+ end
97
+
98
+ # Sanitizes a string and leaves behind only ascii characters, and gets rid of non-ascii and does not change original encoding
99
+ def ascii_only!
100
+ original_encoding = self.encoding
101
+ encode!("US-ASCII", invalid: :replace, undef: :replace, replace: "")
102
+ encode!(original_encoding.name)
103
+ end
104
+
105
+ # Removes matched portion from string and returns matched data object
106
+ def extract!(regexp)
107
+ raise ArgumentError, "Must pass in a Regexp object!" unless regexp.is_a? Regexp
108
+
109
+ match = regexp.match(self)
110
+
111
+ if match
112
+ sub!(regexp, "")
113
+ return match
114
+ end
115
+
116
+ nil
117
+ end
118
+ end
@@ -0,0 +1,5 @@
1
+ class Symbol
2
+ def urlize
3
+ to_s.urlize
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Underglow
2
+ VERSION = "0.0.5"
3
+ end
data/lib/underglow.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "underglow/version"
2
+ require 'underglow/extensions/string'
3
+ require 'underglow/extensions/symbol'
4
+
5
+ module Underglow
6
+ # Make gem's rake tasks available to Rails app
7
+ if defined? Rails
8
+ module Rails
9
+ class Railtie < ::Rails::Railtie
10
+ rake_tasks do
11
+ # load all rake tasks, need to load them by full path so they don't get confused with any tasks of the same name in Rails app
12
+ Dir[File.join(File.dirname(__FILE__), 'tasks/*.rake')].each { |f| load f }
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe "String" do
4
+ it "should coerce a string to proper data type if no argument is given" do
5
+ "hello world".coerce.class.name.should == "String"
6
+ "5".coerce.class.name.should == "Fixnum"
7
+ "3.14".coerce.class.name.should == "Float"
8
+ "true".coerce.should == true
9
+ "false".coerce.should == false
10
+ end
11
+
12
+ it "should coerce to the data type if given an argument" do
13
+ "5".coerce(:string).class.name.should == "String"
14
+ "5".coerce(:integer).class.name.should == "Fixnum"
15
+ "5".coerce(:float).class.name.should == "Float"
16
+ end
17
+
18
+ it "should coerce to false if given a boolean to coerce to and is anything but true" do
19
+ "true".coerce(:boolean).should == true
20
+ "false".coerce(:boolean).should == false
21
+ "5".coerce(:boolean).should == false
22
+ end
23
+
24
+
25
+ it "should be able to deurlize to symbol" do
26
+ "solid-state-drive".deurlize_to_sym.should == :solid_state_drive
27
+ end
28
+
29
+ it "should be able to tell if the string is numeric" do
30
+ "1".should be_numeric
31
+ "1.22".should be_numeric
32
+ end
33
+
34
+ it "should be able to tell if string is a url" do
35
+ "http://google.com".url?.should == true
36
+ "https://192.168.1.200:3000/fucky".url?.should == true
37
+ "hahahah".url?.should == false
38
+ end
39
+
40
+ it "should initial captalize" do
41
+ "what the fuck".initial_capitalize.should == "What the fuck"
42
+ "what the FUCK".initial_capitalize.should == "What the FUCK"
43
+ end
44
+
45
+ it "should convert to something proper for html attributes" do
46
+ "Sandbox".html_attributify.should == "sandbox"
47
+ "not sure WHAT you mean".html_attributify.should == "not-sure-what-you-mean"
48
+ "what_the_fuck!!!!".html_attributify.should == "what-the-fuck"
49
+ "builds/blah".html_attributify.should == "builds-blah"
50
+ end
51
+
52
+ context "overlapping" do
53
+ it "should append if it doesn't overlap at the end" do
54
+ "Core i6".overlap("i5-2500k").should == "Core i6i5-2500k"
55
+ "The day is good".overlap("was good").should == "The day is goodwas good"
56
+ end
57
+
58
+ it "should overlap if it overlaps at the end" do
59
+ "FX".overlap("FX-2500").should == "FX-2500"
60
+ "Core i5".overlap("i5-2500k 3.2 GHz").should == "Core i5-2500k 3.2 GHz"
61
+ "Today is really beautiful".overlap("is really beautiful!").should == "Today is really beautiful!"
62
+ "Just fuck off".overlap("fuck off").should == "Just fuck off"
63
+ "Oh whilly nil".overlap("whilly nilly").should == "Oh whilly nilly"
64
+ end
65
+ end
66
+
67
+ context "sanitizing ascii only" do
68
+ it "should be able to get rid of non-ascii characters" do
69
+ str = "ATI Radeon\u2122 HD 4250 GPU"
70
+ str.ascii_only!
71
+ str.should == "ATI Radeon HD 4250 GPU"
72
+ end
73
+
74
+ it "should not change the encoding" do
75
+ str = "ATI Radeon\u2122 HD 4250 GPU"
76
+ original_encoding = str.encoding.name
77
+
78
+ str.ascii_only!
79
+ str.encoding.name.should == original_encoding
80
+ end
81
+ end
82
+
83
+ describe '#extract!' do
84
+ it "removes the match and returns match object" do
85
+ str = "What the fuck"
86
+ match = str.extract!(/the (fuck)/)
87
+ match.should be_a MatchData
88
+ match[0].should == "the fuck"
89
+ match[1].should == "fuck"
90
+
91
+ str.should == "What "
92
+ end
93
+
94
+ it "only removes the first match" do
95
+ str = "12345"
96
+ match = str.extract!(/\d/)
97
+ str.should == "2345"
98
+ end
99
+
100
+ it "removes nothing and returns nil if nothing is matched" do
101
+ str = "12345"
102
+ match = str.extract!(/[A-Z]+/)
103
+ match.should be_nil
104
+ str.should == "12345"
105
+ end
106
+
107
+ it "raises an argument error if not passed in a regexp" do
108
+ expect { "oh boy".extract!("1234") }.to raise_error ArgumentError
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Symbol do
4
+ it "should urlize a symbol properly" do
5
+ :fuck_you_bitch.urlize.should == "fuck-you-bitch"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'underglow' # and any other gems you need
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.filter_run focus: true
11
+ config.run_all_when_everything_filtered = true
12
+ end
data/underglow.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "underglow/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "underglow"
7
+ gem.version = Underglow::VERSION
8
+ gem.authors = ["James Hu"]
9
+ gem.email = ["axsuul@gmail.com"]
10
+ gem.homepage = "http://www.github.com/axsuul/underglow"
11
+ gem.summary = gem.description = %q{Makes life worth living}
12
+
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {spec}/*`.split("\n")
15
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ gem.require_paths = ["lib"]
17
+
18
+ gem.add_development_dependency "rspec"
19
+ gem.add_development_dependency "rake"
20
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: underglow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - James Hu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Makes life worth living
47
+ email:
48
+ - axsuul@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - README.md
57
+ - Rakefile
58
+ - lib/tasks/db.rake
59
+ - lib/tasks/system.rake
60
+ - lib/tasks/thin.rake
61
+ - lib/underglow.rb
62
+ - lib/underglow/config.rb
63
+ - lib/underglow/extensions/string.rb
64
+ - lib/underglow/extensions/symbol.rb
65
+ - lib/underglow/version.rb
66
+ - spec/extensions/string_spec.rb
67
+ - spec/extensions/symbol_spec.rb
68
+ - spec/spec_helper.rb
69
+ - underglow.gemspec
70
+ homepage: http://www.github.com/axsuul/underglow
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.24
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Makes life worth living
94
+ test_files: []