corelib_ruby 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: 81f43b9d5997a0a97fca28b30170a3daa80cf85e
4
+ data.tar.gz: 03b01a6f4b0bc6945b778c9f6ffe1cca7533e648
5
+ SHA512:
6
+ metadata.gz: d91a73b06d6927c3505d9edfc1b8d26eb8f07ad38fe63b795856a2e67e973270aaa59fd51aeafd9e226f7f9291f5d36fb2eb776c9d54df8a9a5446d397bd9852
7
+ data.tar.gz: c0c99f36a9ca23451a6ccabde0437ffb8958eb27330092e0653a21c82b6e814a92dd8b143ce768c0d5624ff7b41d55fe489cb567333139690b0bf12302dee6f0
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /rubocop
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,38 @@
1
+ AllCops:
2
+ Exclude: # skip all of the following....
3
+ - db/**/* # schema files are auto generated
4
+ - db/seeds/**/* # skip seed files
5
+ - db/seeds/seed_data/**/* # skip seed data
6
+ - bin/**/* # bin files are auto generated
7
+ - /**/*.gemspec
8
+ RunRailsCops: true
9
+ Documentation:
10
+ Enabled: false # don't require docs @ top level of class
11
+ Style/StringLiterals:
12
+ Enabled: false # TODO (Bobby): Disabled by Bobby. Need to decide if we care about enforcing single or double quoted strings.
13
+ Style/HashSyntax:
14
+ Enabled: false
15
+ Style/SpaceAroundEqualsInParameterDefault:
16
+ Enabled: false
17
+ Metrics/LineLength:
18
+ Max: 130
19
+ # Ignore the "Use nested module/class definitions instead of compact style" cop
20
+ # because Rails, by default, generates the compact style.
21
+ #
22
+ # TODO: (Jay) Consider adopting the nested at some point
23
+ Style/ClassAndModuleChildren:
24
+ Enabled: false
25
+ # Ignore the annotation keywords in comments. This allows use of something like
26
+ # `TODO (Foo):` which we would use, but breaks rubocop. Need to replace with
27
+ # `TODO: (Jay)` to make compatibile.
28
+ #
29
+ # TODO: (Jay) Consider re-enabling this to comply with the standard ruby
30
+ # conventions
31
+ Style/CommentAnnotation:
32
+ Enabled: false
33
+
34
+ Style/MultilineOperationIndentation:
35
+ EnforcedStyle: indented
36
+
37
+ Metrics/AbcSize:
38
+ Max: 17
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.0.1 (Oct 9, 2015)
4
+
5
+ 1. (New) Added **String** methods: `#first, #first_or_nil, #last, #last_or_nil, #first_words, #force_leading_space, #all_spaces?`
6
+ 2. (New) Added **Hash** methods: `#extract`
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in corelib_ruby.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # corelib_ruby
2
+
3
+ Many languages provide a vast library of methods or functions for developers to use, whereas Ruby only provides basic (albeit powerful) building blocks. At best, this leads to more complex applications and time wasted writing code another developer has probably already written; at worst it results in core class extensions being placed in helper methods or on objects that have no business owning the methods. The lack of a good strategy for managing Ruby extentions really becomes apparent when a developer needs to share his extensions across multiple projects.
4
+
5
+ Corelib aims to solve this problem by providing a central gem for developers to share extensions & additions to the Ruby core. Corelib focuses on:
6
+
7
+ 1. Reducing Code Duplication
8
+ 2. Improving Code Readability
9
+ 3. Sharing Developer Knowledge
10
+ 4. Reducing Errors
11
+ 5. Saving Developers Time
12
+ 6. Improving Code Quality & Performance
13
+
14
+ We invite all like minded developers to join us in growing the corelib library of extensions.
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'corelib_ruby'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install corelib_ruby
31
+
32
+ ## Usage
33
+
34
+ Browse the `/lib/corelib_ruby/` directory to find new methods; each method provides extensive document. All of these methods are now available inside your application without any additional configuration.
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ Open a console with either command
43
+
44
+ $ bin/console
45
+ $ rake console
46
+
47
+ Run rspec tests
48
+
49
+ $ rspec
50
+
51
+ Run rubocop
52
+
53
+ $ bin/rubocop
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/corlewsolutions/corelib_ruby.
58
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ # Adds the ability to run 'rake console' in the terminal and have a console
4
+ # that already knows about the gem
5
+ task :console do
6
+ exec "pry -r corelib_ruby -I ./lib"
7
+ # exec "irb -r corelib_ruby -I ./lib"
8
+ end
data/bin/console ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "corelib_ruby"
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
+ # require "irb"
10
+ # IRB.start
11
+
12
+ require "pry"
13
+ Pry.start
data/bin/rubocop ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+
5
+ # path to your application root.
6
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
7
+
8
+ Dir.chdir APP_ROOT do
9
+ print "Running rubocop ... "
10
+
11
+ # Style Checks
12
+
13
+ command = "rubocop"
14
+ command += " --format simple --out rubocop/style-issues.txt"
15
+ command += " --format html --out rubocop/style-issues.html"
16
+ command += " --format offenses --out rubocop/style-counts.txt"
17
+ system command
18
+
19
+ # Lint Checks
20
+
21
+ command = "rubocop -l"
22
+ command += " --format simple --out rubocop/lint-issues.txt"
23
+ command += " --format html --out rubocop/lint-issues.html"
24
+ command += " --format offenses --out rubocop/lint-counts.txt"
25
+ system command
26
+
27
+ puts "done (results can be found in the 'rubocop' folder)"
28
+ end
data/bin/setup ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+
5
+ # path to your application root.
6
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
7
+
8
+ Dir.chdir APP_ROOT do
9
+ # Remove Gemfile.lock and suppress output if it doesn't exist
10
+ system "rm Gemfile.lock 2> /dev/null"
11
+ system "bundle install"
12
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'corelib_ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "corelib_ruby"
8
+ spec.version = CorelibRuby::VERSION
9
+ spec.authors = ["roberts1000"]
10
+ spec.email = ["roberts@corlewsolutions.com"]
11
+
12
+ spec.summary = %q{Useful extensions to ruby core.}
13
+ spec.description = %q{Useful extensions to ruby core.}
14
+ spec.homepage = "https://github.com/corlewsolutions/corelib_ruby"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2.0"
24
+ spec.add_development_dependency "pry", "~> 0.10.1"
25
+ spec.add_development_dependency "rubocop", "~> 0.34.2"
26
+ end
@@ -0,0 +1 @@
1
+ require "corelib_ruby/core_ext/hash/extract"
@@ -0,0 +1,24 @@
1
+ class Hash
2
+ # This method mimics the fetch method except it also deletes the key from the
3
+ # hash once it has been fetched.
4
+ #
5
+ # If key exists in the hash, it's value will be returned.
6
+ # If key doesn't exist, and optional default value will be returned
7
+ # If key doesn't exist and not default is specified, a KeyError is raised
8
+ # If key doesn't exists, there is no default, but a block is given, then
9
+ # that block will be executed with the key.
10
+ def extract(*args)
11
+ arg_length = args.length
12
+ fail ArgumentError, "wrong number of arguments #{arg_length} for 1..2" if arg_length == 0 || arg_length > 2
13
+ key = args[0]
14
+
15
+ if key?(key)
16
+ val = self[key]
17
+ delete(key)
18
+ return val
19
+ end
20
+
21
+ return yield key if block_given?
22
+ (args.length == 2) ? args[1] : fail(KeyError, "key not found #{key.inspect}", caller)
23
+ end
24
+ end
@@ -0,0 +1,41 @@
1
+ class String
2
+ def first
3
+ empty? ? "" : self[0, 1]
4
+ end
5
+
6
+ def first_or_nil
7
+ empty? ? nil : self[0, 1]
8
+ end
9
+
10
+ def last
11
+ empty? ? "" : self[-1, 1]
12
+ end
13
+
14
+ def last_or_nil
15
+ empty? ? nil : self[-1, 1]
16
+ end
17
+
18
+ # Retreive the fist n words of a string as a new string. Attach an optional
19
+ # ellipses what will display smartly
20
+ #
21
+ # Examples
22
+ # "This is a test".first_words(2)
23
+ # # => "This is"
24
+ # "This is a test".first_words(2, ellipses: true)
25
+ # # => "This is ..."
26
+ # "This is a test".first_words(2, ellipses: " +++")
27
+ # # => "This is +++"
28
+ def first_words(n=nil, ellipses: false)
29
+ new_str = n.nil? ? self : split[0...n].join(' ')
30
+ elip = determine_ellipses(new_str, ellipses)
31
+ new_str + "#{elip}"
32
+ end
33
+
34
+ private
35
+
36
+ def determine_ellipses(str, ellipses)
37
+ return "" if empty? || !ellipses
38
+ return "" if str == self
39
+ ellipses === true ? " ..." : ellipses.to_s
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ require "corelib_ruby/core_ext/string/accessors"
2
+ require "corelib_ruby/core_ext/string/questions"
3
+ require "corelib_ruby/core_ext/string/conversions"
@@ -0,0 +1,6 @@
1
+ class String
2
+ def force_leading_space(even_when_empty: false)
3
+ (return even_when_empty ? " " : "") if empty?
4
+ all_spaces? ? dup : " #{lstrip}"
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ class String
2
+ # Returns true if a non empty string contains all strict space characters.
3
+ # Tabs and new lines are not considered spaces.
4
+ # Returns false for empty strings unless the when_empty: option is set to true.
5
+ def all_spaces?(when_empty: false)
6
+ return when_empty if empty?
7
+ each_byte { |x| return false unless x == 32 }
8
+ true
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module CorelibRuby
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "corelib_ruby/version"
2
+ require "corelib_ruby/core_ext/string/base"
3
+ require "corelib_ruby/core_ext/hash/base"
4
+
5
+ # module CorelibRuby
6
+ # end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: corelib_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - roberts1000
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.34.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.34.2
83
+ description: Useful extensions to ruby core.
84
+ email:
85
+ - roberts@corlewsolutions.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - CHANGELOG.md
94
+ - Gemfile
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/rubocop
99
+ - bin/setup
100
+ - corelib_ruby.gemspec
101
+ - lib/corelib_ruby.rb
102
+ - lib/corelib_ruby/core_ext/hash/base.rb
103
+ - lib/corelib_ruby/core_ext/hash/extract.rb
104
+ - lib/corelib_ruby/core_ext/string/accessors.rb
105
+ - lib/corelib_ruby/core_ext/string/base.rb
106
+ - lib/corelib_ruby/core_ext/string/conversions.rb
107
+ - lib/corelib_ruby/core_ext/string/questions.rb
108
+ - lib/corelib_ruby/version.rb
109
+ homepage: https://github.com/corlewsolutions/corelib_ruby
110
+ licenses: []
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.8
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Useful extensions to ruby core.
132
+ test_files: []