Skip to content

Commit

Permalink
extract features from Steam which could benefit to other Locomotive gems
Browse files Browse the repository at this point in the history
  • Loading branch information
did committed Mar 31, 2015
1 parent 4327a33 commit 868f628
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--colour
--backtrace
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in locomotivecms_common.gemspec
gemspec

group :test do
gem 'rspec', '~> 3.2.0'
end
38 changes: 29 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@ PATH
remote: .
specs:
locomotivecms_common (0.0.3)
activesupport (~> 4.2.1)
colorize
stringex (~> 2.5.2)

GEM
remote: https://rubygems.org/
specs:
activesupport (4.2.1)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
colorize (0.7.5)
diff-lcs (1.2.5)
i18n (0.7.0)
json (1.8.2)
minitest (5.5.1)
rake (10.2.2)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.8)
rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.6)
rspec (3.2.0)
rspec-core (~> 3.2.0)
rspec-expectations (~> 3.2.0)
rspec-mocks (~> 3.2.0)
rspec-core (3.2.2)
rspec-support (~> 3.2.0)
rspec-expectations (3.2.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.2.0)
rspec-mocks (3.2.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.2.0)
rspec-support (3.2.2)
stringex (2.5.2)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)

PLATFORMS
ruby
Expand All @@ -26,4 +46,4 @@ DEPENDENCIES
bundler (~> 1.5)
locomotivecms_common!
rake (~> 10.1)
rspec (~> 2.14)
rspec (~> 3.2.0)
6 changes: 6 additions & 0 deletions lib/locomotive/common.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
require_relative 'common/version'
require_relative 'common/core_ext'
require_relative 'common/logger'
require_relative 'common/exception'
require_relative 'common/notifier'
require_relative 'common/configuration'

require 'active_support'
require 'active_support/concern'
require 'active_support/deprecation'
require 'active_support/core_ext'

module Locomotive
module Common
class << self
Expand Down
5 changes: 5 additions & 0 deletions lib/locomotive/common/core_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require_relative 'core_ext/hash'
require_relative 'core_ext/string'
require_relative 'core_ext/boolean/true'
require_relative 'core_ext/boolean/false'
require_relative 'core_ext/kernel'
3 changes: 3 additions & 0 deletions lib/locomotive/common/core_ext/boolean/false.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class FalseClass
def to_bool() self ; end
end
3 changes: 3 additions & 0 deletions lib/locomotive/common/core_ext/boolean/true.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class TrueClass
def to_bool() self ; end
end
39 changes: 39 additions & 0 deletions lib/locomotive/common/core_ext/hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Big thanks to Tim Ruffles (https://github.com/timruffles)
# https://gist.github.com/timruffles/2780508
module HashConverter
class << self

def to_underscore(hash)
convert(hash, :underscore)
end

def to_string(hash)
convert(hash, :to_s)
end

def to_sym(hash)
convert(hash, :to_sym)
end

# FIXME: not sure it will be ever needed
# def to_camel_case hash
# convert hash, :camelize, :lower
# end

def convert(obj, *method)
case obj
when Hash
obj.inject({}) do |h, (k,v)|
v = convert(v, *method)
h[k.send(*method)] = v
h
end
when Array
obj.map { |m| convert(m, *method) }
else
obj
end
end

end
end
14 changes: 14 additions & 0 deletions lib/locomotive/common/core_ext/kernel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Kernel

def require_relative_all(paths, sub = nil)
main_path = File.dirname(caller.first.sub(/:\d+$/, ''))
main_path = File.join(main_path, sub) if sub

[*paths].each do |path|
Dir[File.join(main_path, path, '*.rb')].each { |file| require file }
end
end

end


37 changes: 37 additions & 0 deletions lib/locomotive/common/core_ext/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
unless String.public_instance_methods.include?(:to_bool)
class String

def to_bool
return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
return false if self == false || self.blank? || self =~ (/(false|f|no|n|0)$/i)

raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
end

end
end

unless String.public_instance_methods.include?(:permalink)
require 'stringex'

class String

def permalink(underscore = false)
# if the slug includes one "_" at least, we consider that the "_" is used instead of "-".
_permalink = if !self.index('_').nil?
self.to_url(replace_whitespace_with: '_')
else
self.to_url
end

underscore ? _permalink.underscore : _permalink
end

def permalink!(underscore = false)
replace(self.permalink(underscore))
end

alias :parameterize! :permalink!

end
end
8 changes: 5 additions & 3 deletions locomotivecms_common.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_development_dependency 'bundler', '~> 1.5'
spec.add_development_dependency 'rake', '~> 10.1'
spec.add_development_dependency 'rspec', '~> 2.14'
spec.add_development_dependency 'bundler', '~> 1.5'
spec.add_development_dependency 'rake', '~> 10.1'

spec.add_dependency 'activesupport', '~> 4.2.1'
spec.add_dependency 'stringex', '~> 2.5.2'
spec.add_dependency 'colorize'

spec.required_ruby_version = '~> 2.0'
end
44 changes: 44 additions & 0 deletions spec/unit/core_ext/string_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'spec_helper'

describe String do

describe '#permalink!' do

let(:string) { 'foo bar' }

before { string.permalink! }

it { expect(string).to eq 'foo-bar' }

end

describe '#to_bool' do

subject { string.to_bool }

describe 'true values' do

%w(true t yes y 1).each do |val|
let(:string) { val }
it { is_expected.to eq true }
end

end

describe 'false values' do

(%w(false f no n 0) + ['']).each do |val|
let(:string) { val }
it { is_expected.to eq false }
end

end

describe 'no truthy or falsy' do
let(:string) { 'foo' }
it { expect { subject }.to raise_error(%(invalid value for Boolean: "foo")) }
end

end

end

0 comments on commit 868f628

Please sign in to comment.