Skip to content

Commit

Permalink
Merge 1eb41f2 into 51e8f92
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Noack committed May 10, 2018
2 parents 51e8f92 + 1eb41f2 commit 3c2ee95
Show file tree
Hide file tree
Showing 37 changed files with 306 additions and 316 deletions.
2 changes: 2 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ruby:
config_file: .rubocop.yml
17 changes: 17 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
AllCops:
TargetRubyVersion: 2.4

Metrics/LineLength:
Max: 80

Style/Documentation:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false # Using Ruby 2.4 doesn't need it

Style/StringLiterals:
EnforcedStyle: single_quotes
SupportedStyles:
- single_quotes
- double_quotes
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby
rvm:
- 2.3.0
script: bundle exec rake spec
script: bin/test
gemfile:
- gemfiles/rails3.gemfile
- gemfiles/rails4.gemfile
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This changelog adheres to [Keep a CHANGELOG](http://keepachangelog.com/).

## Unreleased

### Added
- Correct various rubocop issues

## 0.0.1

### Added
Expand Down
8 changes: 4 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require "bundler/gem_tasks"
require 'bundler/gem_tasks'

desc 'Default: run specs.'
task :default => :spec
task default: :spec

require 'rspec/core/rake_task'

desc "Run specs"
desc 'Run specs'
RSpec::Core::RakeTask.new do |t|
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
t.pattern = './spec/**/*_spec.rb' # don't need this, it's default.
# Put spec opts in a file named .rspec in root
end
1 change: 1 addition & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bundle exec rubocop --only Layout lib spec && bundle exec rspec
2 changes: 1 addition & 1 deletion gemfiles/rails3.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source 'https://rubygems.org'
gemspec :path => '../'
gemspec path: '../'

group :development, :test do
gem 'rake'
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails4.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source 'https://rubygems.org'
gemspec :path => '../'
gemspec path: '../'

group :development, :test do
gem 'rake'
Expand Down
29 changes: 15 additions & 14 deletions lib/ruby_core_extensions/array.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
class Array

def to_param
self.collect { |element| element.respond_to?(:to_param) ? element.to_param : element }
collect { |element|
element.respond_to?(:to_param) ? element.to_param : element
}
end

def show_name
first.titleize
end

# Key should be unique, or latest element with that key will override previous ones.
# Key should be unique
# or latest element with that key will override previous ones.
def hash_by(key = nil, method = nil, &block)
self.inject({}) do |h, element|
each.with_object({}) do |element, hash|
if key
h[element.send(key)] = if block_given?
yield(element)
elsif method
element.send(method)
else
element
end
hash[element.send(key)] = if block_given?
yield(element)
elsif method
element.send(method)
else
element
end
else # key is block and value is element
h[yield(element)] = element
hash[yield(element)] = element
end
h
end
end

Expand All @@ -31,7 +32,7 @@ def hash_by_id(method = nil, &block)
end

def intersects?(other)
self.any?{|i| other.include?(i)}
any? { |i| other.include?(i) }
end

# Same effect as Array.wrap(object).first
Expand Down
5 changes: 1 addition & 4 deletions lib/ruby_core_extensions/class.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
class Class

def downcase_symbol
self.to_s.downcase.to_sym
to_s.downcase.to_sym
end

end

3 changes: 1 addition & 2 deletions lib/ruby_core_extensions/compact/array.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Array
def compact_blank!
delete_if{|v| v.blank?}
delete_if(&:blank?)
end

def recursive_compact_blank!
Expand All @@ -17,4 +17,3 @@ def recursive_compact_blank!
end
end
end

11 changes: 5 additions & 6 deletions lib/ruby_core_extensions/compact/hash.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
class Hash
# Remove nil values
def compact
self.dup.compact!
dup.compact!
end

# Remove nil values - !desctructively!
def compact!
delete_if{|k,v| v.nil?}
delete_if { |_k, v| v.nil? }
end

def compact_blank
self.dup.compact_blank!
dup.compact_blank!
end

def compact_blank!
delete_if{|k,v| v.blank?}
delete_if { |_k, v| v.blank? }
end

def recursive_compact_blank!
delete_if do |k,v|
delete_if do |_k, v|
if v.is_a?(Hash)
v.recursive_compact_blank!
v.recursive_blank?
Expand All @@ -31,4 +31,3 @@ def recursive_compact_blank!
end
end
end

11 changes: 2 additions & 9 deletions lib/ruby_core_extensions/enumerable.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
module Enumerable

def map_methods(*methods)
map do |object|
methods.inject({}) do |h, method|
h[method] = object.send(method)
h
methods.each.with_object({}) do |method, hash|
hash[method] = object.send(method)
end
end
end


def detect_and_return
detect do |e|
result = yield(e)
return result if result
end
end


def select_by_attr(attr, value)
select do |e|
e.send(attr) == value
end
end


if RUBY_VERSION < '1.9'
def with_object(obj, &block)
return to_enum :with_object, obj unless block_given?
Expand All @@ -34,6 +29,4 @@ def with_object(obj, &block)
obj
end
end

end

9 changes: 4 additions & 5 deletions lib/ruby_core_extensions/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ class File
SAFE_CHARS = /[^a-zA-Z0-9._\-]/

def self.safe_name(name)
name.gsub('&', 'and'). # prittify & to and
gsub(/\s/, '-'). # replace spaces with dashes
gsub(SAFE_CHARS, '-'). # replace with portable characters
gsub(/\-+/, '-') # limit - character to once
name.gsub('&', 'and') # prittify & to and
.gsub(/\s/, '-') # replace spaces with dashes
.gsub(SAFE_CHARS, '-') # replace with portable characters
.gsub(/\-+/, '-') # limit - character to once
end
end

15 changes: 5 additions & 10 deletions lib/ruby_core_extensions/hash.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
class Hash

unless self.method_defined?(:extract!)
#Imported from Rails 3
unless method_defined?(:extract!)
# Imported from Rails 3
def extract!(*keys)
result = {}
keys.each { |key| result[key] = delete(key) }
result
end
end


def map_key_value(key_method, value_method = nil)
value_method ||= key_method
each.with_object({}) do |(k,v), new_hash|
each.with_object({}) do |(k, v), new_hash|
new_hash[k.send(key_method)] = v.send(value_method)
end
end


def map_key(method)
each.with_object({}) do |(k,v), new_hash|
each.with_object({}) do |(k, v), new_hash|
new_hash[k.send(method)] = v
end
end


def map_value(method)
each.with_object({}) do |(k,v), new_hash|
each.with_object({}) do |(k, v), new_hash|
new_hash[k] = v.send(method)
end
end

end
2 changes: 0 additions & 2 deletions lib/ruby_core_extensions/kernel.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module Kernel

def require_dirs(dirs)
Array.wrap(dirs).each do |load_path|
Dir.glob("#{load_path}/**/*.rb").each do |file|
require file
end
end
end

end
2 changes: 0 additions & 2 deletions lib/ruby_core_extensions/numeric.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class Numeric

def to_bool
!zero?
end

end
32 changes: 13 additions & 19 deletions lib/ruby_core_extensions/object.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
class Object
# Does this object sound like another - phonetically?
def sounds_like?(other)
self.phonetic_code == other.phonetic_code
phonetic_code == other.phonetic_code
end

# Convert this object into a string, then convert that to phonetic code
def phonetic_code
self.to_s.phonetic_code
to_s.phonetic_code
end

def to_long_s
to_s
end

def virtual_belongs_to(*associations)
options = associations.extract_options!

for association in associations
class_eval <<-EVAL
associations.each do |association|
class_eval(<<-EVAL, __FILE__, __LINE__ + 1)
attr_accessor :#{association}, :#{association}_id
def #{association}=(#{association})
Expand All @@ -41,16 +41,13 @@ def #{association}_id
end
end


def booleanize(name, options)
raise ArgumentError, ":rescue option is required" if options[:rescue].blank?
if !options[:rescue].is_a?(Array)
options[:rescue] = [options[:rescue]]
end
raise ArgumentError, ':rescue option is required' if options[:rescue].blank?
options[:rescue] = [options[:rescue]] unless options[:rescue].is_a?(Array)

normal_name = name.to_s.gsub('!', '')
normal_name = name.to_s.delete('!')

class_eval <<-EVAL
class_eval(<<-EVAL, __FILE__, __LINE__ + 1)
attr_accessor :reason_not_#{normal_name}
def #{normal_name}?(*args)
#{name}(*args)
Expand All @@ -62,11 +59,8 @@ def #{normal_name}?(*args)
end
EVAL
end



def to_bool
self.to_s.to_bool
to_s.to_bool
end

end

Loading

0 comments on commit 3c2ee95

Please sign in to comment.