ruby_core_extensions 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +14 -0
  5. data/Gemfile +2 -0
  6. data/LICENSE +21 -0
  7. data/README.md +37 -0
  8. data/Rakefile +12 -0
  9. data/gemfiles/rails3.gemfile +11 -0
  10. data/gemfiles/rails4.gemfile +11 -0
  11. data/lib/ruby_core_extensions.rb +20 -0
  12. data/lib/ruby_core_extensions/array.rb +50 -0
  13. data/lib/ruby_core_extensions/class.rb +8 -0
  14. data/lib/ruby_core_extensions/compact.rb +2 -0
  15. data/lib/ruby_core_extensions/compact/array.rb +20 -0
  16. data/lib/ruby_core_extensions/compact/hash.rb +34 -0
  17. data/lib/ruby_core_extensions/enumerable.rb +39 -0
  18. data/lib/ruby_core_extensions/file.rb +12 -0
  19. data/lib/ruby_core_extensions/hash.rb +34 -0
  20. data/lib/ruby_core_extensions/kernel.rb +11 -0
  21. data/lib/ruby_core_extensions/numeric.rb +7 -0
  22. data/lib/ruby_core_extensions/object.rb +72 -0
  23. data/lib/ruby_core_extensions/recursive.rb +8 -0
  24. data/lib/ruby_core_extensions/recursive/array.rb +45 -0
  25. data/lib/ruby_core_extensions/recursive/big_decimal.rb +5 -0
  26. data/lib/ruby_core_extensions/recursive/date.rb +8 -0
  27. data/lib/ruby_core_extensions/recursive/date_time.rb +8 -0
  28. data/lib/ruby_core_extensions/recursive/fixnum.rb +7 -0
  29. data/lib/ruby_core_extensions/recursive/hash.rb +86 -0
  30. data/lib/ruby_core_extensions/recursive/object.rb +17 -0
  31. data/lib/ruby_core_extensions/recursive/time.rb +8 -0
  32. data/lib/ruby_core_extensions/string.rb +78 -0
  33. data/lib/ruby_core_extensions/version.rb +3 -0
  34. data/ruby_core_extensions.gemspec +32 -0
  35. data/spec/array_spec.rb +67 -0
  36. data/spec/class_spec.rb +12 -0
  37. data/spec/compact_spec.rb +49 -0
  38. data/spec/enumerable_spec.rb +24 -0
  39. data/spec/filename_spec.rb +17 -0
  40. data/spec/hash_spec.rb +107 -0
  41. data/spec/numeric_spec.rb +12 -0
  42. data/spec/object_spec.rb +58 -0
  43. data/spec/spec_helper.rb +17 -0
  44. data/spec/string_spec.rb +62 -0
  45. data/spec/support/coverage.rb +29 -0
  46. metadata +225 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 73d409278a49f15845c54d39663e24a0942bd004
4
+ data.tar.gz: 61a669479de59c7b8e9105d674cc44d28824151a
5
+ SHA512:
6
+ metadata.gz: addb261d51cdd07ced187a5efefa83d474d712875133108585a255be62168add46445b532a7816292466110c8766720394f91012c2b38db2e584a70830b700de
7
+ data.tar.gz: 3e37676250b767f9d97808b353e25e187c386de97f99ec457ff8a6823a7c25fbb8c74195e3488e0ed367de7db71aabb5b18ed9e600d10b558b5d839a908cbe47
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg
2
+ coverage
3
+ Gemfile.lock
4
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ script: bundle exec rake spec
5
+ gemfile:
6
+ - gemfiles/rails3.gemfile
7
+ - gemfiles/rails4.gemfile
8
+ notifications:
9
+ email:
10
+ - support@travellink.com.au
11
+ flowdock:
12
+ secure: IK3CyMRa++LNdooemZJBZSNdwUiKpVdXZUz9exYIkj3La/EJDOy+kqV2NzYdqRht2BGHHiRsVn3hVqySDDuv9jxBHTZgM0v/pQ4kT8JZqi7fDb36axf3M6Y2VJyrJ0S/9BbHqLE95B4NXQ30ctLCSfQNbp9sOn2EwP3DZS9KJks=
13
+ sudo: false
14
+ cache: bundler
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Tom Preston-Werner
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ Ruby Core Extensions
2
+ ====================
3
+
4
+ [![Build Status](https://travis-ci.org/sealink/ruby_core_extensions.png?branch=master)](https://travis-ci.org/sealink/ruby_core_extensions)
5
+ [![Coverage Status](https://coveralls.io/repos/sealink/ruby_core_extensions/badge.png)](https://coveralls.io/r/sealink/ruby_core_extensions)
6
+ [![Dependency Status](https://gemnasium.com/sealink/ruby_core_extensions.png?travis)](https://gemnasium.com/sealink/ruby_core_extensions)
7
+ [![Code Climate](https://codeclimate.com/github/sealink/ruby_core_extensions.png)](https://codeclimate.com/github/sealink/ruby_core_extensions)
8
+
9
+ # DESCRIPTION
10
+
11
+ Extends the core ruby classes with helpful functions
12
+
13
+ # INSTALLATION
14
+
15
+ Add to your Gemfile:
16
+ gem 'ruby_core_extensions'
17
+
18
+ # Extensions
19
+
20
+ The following classes are extend in this gem:
21
+
22
+ * Array
23
+ * BigDecimal
24
+ * Class
25
+ * Date
26
+ * DateTime
27
+ * Enumerable
28
+ * File
29
+ * Fixnum
30
+ * Hash
31
+ * Kernel
32
+ * Numeric
33
+ * Object
34
+ * Range
35
+ * String
36
+ * Time
37
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc 'Default: run specs.'
4
+ task :default => :spec
5
+
6
+ require 'rspec/core/rake_task'
7
+
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
11
+ # Put spec opts in a file named .rspec in root
12
+ end
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+ gemspec :path => '../'
3
+
4
+ group :development, :test do
5
+ gem 'rake'
6
+ gem 'rdoc'
7
+ gem 'rspec'
8
+ gem 'simplecov'
9
+ gem 'simplecov-rcov'
10
+ gem 'activesupport', '~> 3.2.0'
11
+ end
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+ gemspec :path => '../'
3
+
4
+ group :development, :test do
5
+ gem 'rake'
6
+ gem 'rdoc'
7
+ gem 'rspec'
8
+ gem 'simplecov'
9
+ gem 'simplecov-rcov'
10
+ gem 'activesupport', '~> 4.0.0'
11
+ end
@@ -0,0 +1,20 @@
1
+ module RubyCoreExtensions
2
+ require 'active_support/version'
3
+ if ActiveSupport::VERSION::MAJOR > 2
4
+ require 'active_support/dependencies/autoload'
5
+ end
6
+ require 'active_support/core_ext'
7
+
8
+ require 'ruby_core_extensions/array'
9
+ require 'ruby_core_extensions/class'
10
+ require 'ruby_core_extensions/enumerable'
11
+ require 'ruby_core_extensions/file'
12
+ require 'ruby_core_extensions/hash'
13
+ require 'ruby_core_extensions/kernel'
14
+ require 'ruby_core_extensions/numeric'
15
+ require 'ruby_core_extensions/object'
16
+ require 'ruby_core_extensions/string'
17
+
18
+ require 'ruby_core_extensions/compact'
19
+ require 'ruby_core_extensions/recursive'
20
+ end
@@ -0,0 +1,50 @@
1
+ class Array
2
+
3
+ def to_param
4
+ self.collect { |element| element.respond_to?(:to_param) ? element.to_param : element }
5
+ end
6
+
7
+ def show_name
8
+ first.titleize
9
+ end
10
+
11
+ # Key should be unique, or latest element with that key will override previous ones.
12
+ def hash_by(key = nil, method = nil, &block)
13
+ self.inject({}) do |h, element|
14
+ if key
15
+ h[element.send(key)] = if block_given?
16
+ yield(element)
17
+ elsif method
18
+ element.send(method)
19
+ else
20
+ element
21
+ end
22
+ else # key is block and value is element
23
+ h[yield(element)] = element
24
+ end
25
+ h
26
+ end
27
+ end
28
+
29
+ def hash_by_id(method = nil, &block)
30
+ hash_by(:id, method, &block)
31
+ end
32
+
33
+ def intersects?(other)
34
+ self.any?{|i| other.include?(i)}
35
+ end
36
+
37
+ # Same effect as Array.wrap(object).first
38
+ # but no need to create new object in case of non-array
39
+ def self.first(object)
40
+ return object.first if object.is_a?(Array)
41
+ object
42
+ end
43
+
44
+ # Same effect as Array.wrap(object).last
45
+ # but no need to create new object in case of non-array
46
+ def self.last(object)
47
+ return object.last if object.is_a?(Array)
48
+ object
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ class Class
2
+
3
+ def downcase_symbol
4
+ self.to_s.downcase.to_sym
5
+ end
6
+
7
+ end
8
+
@@ -0,0 +1,2 @@
1
+ require 'ruby_core_extensions/compact/array'
2
+ require 'ruby_core_extensions/compact/hash'
@@ -0,0 +1,20 @@
1
+ class Array
2
+ def compact_blank!
3
+ delete_if{|v| v.blank?}
4
+ end
5
+
6
+ def recursive_compact_blank!
7
+ delete_if do |v|
8
+ if v.is_a?(Hash)
9
+ v.recursive_compact_blank!
10
+ v.recursive_blank?
11
+ elsif v.is_a?(Array)
12
+ v.recursive_compact_blank!
13
+ v.recursive_blank?
14
+ else
15
+ v.blank?
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,34 @@
1
+ class Hash
2
+ # Remove nil values
3
+ def compact
4
+ self.dup.compact!
5
+ end
6
+
7
+ # Remove nil values - !desctructively!
8
+ def compact!
9
+ delete_if{|k,v| v.nil?}
10
+ end
11
+
12
+ def compact_blank
13
+ self.dup.compact_blank!
14
+ end
15
+
16
+ def compact_blank!
17
+ delete_if{|k,v| v.blank?}
18
+ end
19
+
20
+ def recursive_compact_blank!
21
+ delete_if do |k,v|
22
+ if v.is_a?(Hash)
23
+ v.recursive_compact_blank!
24
+ v.recursive_blank?
25
+ elsif v.is_a?(Array)
26
+ v.recursive_compact_blank!
27
+ v.recursive_blank?
28
+ else
29
+ v.blank?
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,39 @@
1
+ module Enumerable
2
+
3
+ def map_methods(*methods)
4
+ map do |object|
5
+ methods.inject({}) do |h, method|
6
+ h[method] = object.send(method)
7
+ h
8
+ end
9
+ end
10
+ end
11
+
12
+
13
+ def detect_and_return
14
+ detect do |e|
15
+ result = yield(e)
16
+ return result if result
17
+ end
18
+ end
19
+
20
+
21
+ def select_by_attr(attr, value)
22
+ select do |e|
23
+ e.send(attr) == value
24
+ end
25
+ end
26
+
27
+
28
+ if RUBY_VERSION < '1.9'
29
+ def with_object(obj, &block)
30
+ return to_enum :with_object, obj unless block_given?
31
+ each do |element|
32
+ yield element, obj
33
+ end
34
+ obj
35
+ end
36
+ end
37
+
38
+ end
39
+
@@ -0,0 +1,12 @@
1
+ class File
2
+ # http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_276
3
+ SAFE_CHARS = /[^a-zA-Z0-9._\-]/
4
+
5
+ def self.safe_name(name)
6
+ name.gsub('&', 'and'). # prittify & to and
7
+ gsub(/\s/, '-'). # replace spaces with dashes
8
+ gsub(SAFE_CHARS, '-'). # replace with portable characters
9
+ gsub(/\-+/, '-') # limit - character to once
10
+ end
11
+ end
12
+
@@ -0,0 +1,34 @@
1
+ class Hash
2
+
3
+ unless self.method_defined?(:extract!)
4
+ #Imported from Rails 3
5
+ def extract!(*keys)
6
+ result = {}
7
+ keys.each { |key| result[key] = delete(key) }
8
+ result
9
+ end
10
+ end
11
+
12
+
13
+ def map_key_value(key_method, value_method = nil)
14
+ value_method ||= key_method
15
+ each.with_object({}) do |(k,v), new_hash|
16
+ new_hash[k.send(key_method)] = v.send(value_method)
17
+ end
18
+ end
19
+
20
+
21
+ def map_key(method)
22
+ each.with_object({}) do |(k,v), new_hash|
23
+ new_hash[k.send(method)] = v
24
+ end
25
+ end
26
+
27
+
28
+ def map_value(method)
29
+ each.with_object({}) do |(k,v), new_hash|
30
+ new_hash[k] = v.send(method)
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,11 @@
1
+ module Kernel
2
+
3
+ def require_dirs(dirs)
4
+ Array.wrap(dirs).each do |load_path|
5
+ Dir.glob("#{load_path}/**/*.rb").each do |file|
6
+ require file
7
+ end
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,7 @@
1
+ class Numeric
2
+
3
+ def to_bool
4
+ !zero?
5
+ end
6
+
7
+ end
@@ -0,0 +1,72 @@
1
+ class Object
2
+ # Does this object sound like another - phonetically?
3
+ def sounds_like?(other)
4
+ self.phonetic_code == other.phonetic_code
5
+ end
6
+
7
+ # Convert this object into a string, then convert that to phonetic code
8
+ def phonetic_code
9
+ self.to_s.phonetic_code
10
+ end
11
+
12
+ def to_long_s
13
+ to_s
14
+ end
15
+
16
+ def virtual_belongs_to(*associations)
17
+ options = associations.extract_options!
18
+
19
+ for association in associations
20
+ class_eval <<-EVAL
21
+ attr_accessor :#{association}, :#{association}_id
22
+
23
+ def #{association}=(#{association})
24
+ @#{association}_id = #{association}.id if #{association}
25
+ @#{association} = #{association}
26
+ end
27
+
28
+ def #{association}_id=(#{association}_id)
29
+ @#{association} = nil
30
+ @#{association}_id = #{association}_id
31
+ end
32
+
33
+ def #{association}
34
+ @#{association} ||= #{(options[:class_name] || association.to_s).classify}.find_by_id(@#{association}_id)
35
+ end
36
+
37
+ def #{association}_id
38
+ @#{association}.id if #{association}
39
+ end
40
+ EVAL
41
+ end
42
+ end
43
+
44
+
45
+ def booleanize(name, options)
46
+ raise ArgumentError, ":rescue option is required" if options[:rescue].blank?
47
+ if !options[:rescue].is_a?(Array)
48
+ options[:rescue] = [options[:rescue]]
49
+ end
50
+
51
+ normal_name = name.to_s.gsub('!', '')
52
+
53
+ class_eval <<-EVAL
54
+ attr_accessor :reason_not_#{normal_name}
55
+ def #{normal_name}?(*args)
56
+ #{name}(*args)
57
+ @reason_not_#{normal_name} = nil
58
+ true
59
+ rescue #{options[:rescue].map(&:to_s).join(', ')} => e
60
+ @reason_not_#{normal_name} = e.message
61
+ false
62
+ end
63
+ EVAL
64
+ end
65
+
66
+
67
+ def to_bool
68
+ self.to_s.to_bool
69
+ end
70
+
71
+ end
72
+