Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
add hash join, remove after_*_commit callbacks, minor refractor
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Dec 30, 2016
1 parent c938d28 commit 0f504fc
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 265 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pkg/
spec/reports/
tmp/
test/*.sqlite3.db
.ruby-version
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
---------

- **1.2.2 - Dec 30, 2016**
- Add Hash `join`
- Remove Rails Callbacks `after_create_commit` and `after_update_commit` as I think they were just a novelty item
- Some minor refractoring
- **1.2.1 - Oct 5, 2016**
- Add Rails Callbacks `after_create_commit` and `after_update_commit`
- Fix `find_duplicates`
Expand Down
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,39 @@ Run `rails g rearmed:setup` to create a settings files in `config/initializers/r

Rearmed.enabled_patches = {
rails_4: {
or: false,
link_to_confirm: false
link_to_confirm: false,
or: false
},
rails_3: {
all: false,
pluck: false,
update_columns: false,
all: false
update_columns: false
},
rails: {
pluck_to_hash: false,
pluck_to_struct: false,
find_or_create: false,
find_unused_columns:
find_duplicates: false,
find_in_relation_batches: false,
find_or_create: false,
find_relation_each: false,
newest: false,
reset_table: false,
pluck_to_hash: false,
pluck_to_struct: false,
reset_auto_increment: false,
find_relation_each: false,
find_in_relation_batches: false,
callbacks: {
after_create_commit: false,
after_update_commit: false
}
reset_table: false
},
string: {
valid_integer: false,
valid_float: false,
to_bool: false,
starts_with: false,
begins_with: false,
ends_with: false
ends_with: false,
starts_with: false,
to_bool: false,
valid_float: false,
valid_integer: false
},
hash: {
only: false,
compact: false,
dig: false,
compact: false
join: false,
only: false,
},
array: {
dig: false,
Expand Down Expand Up @@ -144,14 +142,16 @@ array.not_empty? # => true

### Hash Methods
```ruby
my_hash.compact
my_hash.compact!

hash.join{|k,v| "#{k}: #{v}\n"}

hash = {foo: 'foo', bar: 'bar', other: 'other'}
hash.only(:foo, :bar) # => {foo: 'foo'}
# or without monkey patch: Rearmed.only(hash, :foo, :bar)

hash.only!(:foo, :bar)

my_hash.compact
my_hash.compact!
```

### Rails
Expand Down Expand Up @@ -218,7 +218,7 @@ my_hash.compact # See Hash methods above
my_hash.compact!
```
### Minitest Method
### Minitest Methods
```ruby
assert_changed 'user.name' do
user.name = "Bob"
Expand Down
20 changes: 20 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,24 @@ task :test do
end
end

task :console do
require 'rearmed'
Rearmed.enabled_patches = {
array: true,
hash: true,
object: true,
string: true,
date: true,
enumerable: true,
rails_3: true,
rails_4: true,
rails: true,
minitest: true
}
require 'rearmed/apply_patches'

require 'irb'
binding.irb
end

task default: :test
60 changes: 1 addition & 59 deletions lib/generators/rearmed/setup_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,7 @@ class SetupGenerator < Rails::Generators::Base

def setup
create_file "config/initializers/rearmed.rb", <<eos
Rearmed.enabled_patches = {
rails_4: {
or: false,
link_to_confirm: false
},
rails_3: {
pluck: false,
update_columns: false,
all: false
},
rails: {
pluck_to_hash: false,
pluck_to_struct: false,
find_or_create: false,
find_duplicates: false,
newest: false,
reset_table: false,
reset_auto_increment: false,
find_relation_each: false,
find_in_relation_batches: false,
callbacks: {
after_create_commit: false,
after_update_commit: false
}
},
string: {
valid_integer: false,
valid_float: false,
to_bool: false,
starts_with: false,
begins_with: false,
ends_with: false,
},
hash: {
only: false,
dig: false,
compact: false,
},
array: {
dig: false,
delete_first: false,
not_empty: false
},
enumerable: {
natural_sort: false,
natural_sort_by: false
},
object: {
in: false,
not_nil: false
},
date: {
now: false
},
minitest: {
assert_changed: false,
assert_not_changed: false
}
}
Rearmed.enabled_patches = #{File.read(File.join(File.dirname(__FILE__), '../../rearmed/default_enabled_patches.hash'))}
require 'rearmed/apply_patches'
eos
Expand Down
159 changes: 4 additions & 155 deletions lib/rearmed.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,9 @@
require 'rearmed/methods'
require 'rearmed/exceptions'

module Rearmed

@enabled_patches = {
rails_4: {
or: false,
link_to_confirm: false
},
rails_3: {
pluck: false,
update_columns: false,
all: false
},
rails: {
pluck_to_hash: false,
pluck_to_struct: false,
find_or_create: false,
find_duplicates: false,
reset_table: false,
reset_auto_increment: false,
find_relation_each: false,
find_in_relation_batches: false,
},
string: {
valid_integer: false,
valid_float: false,
to_bool: false,
starts_with: false,
begins_with: false,
ends_with: false
},
hash: {
only: false,
dig: false,
compact: false
},
array: {
dig: false,
delete_first: false,
not_empty: false
},
enumerable: {
natural_sort: false,
natural_sort_by: false
},
object: {
in: false,
not_nil: false
},
date: {
now: false
},
minitest: {
assert_changed: false,
assert_not_changed: false
}
}
@enabled_patches = eval(File.read(File.join(File.dirname(__FILE__), 'rearmed/default_enabled_patches.hash')))

def self.enabled_patches=(val)
@enabled_patches = val
Expand All @@ -63,105 +13,4 @@ def self.enabled_patches
@enabled_patches
end

def self.valid_integer?(str)
str =~ /^\d*$/ ? true : false
end

def self.valid_float?(str)
str =~ /(^(\d+)(\.)?(\d+)?$)|(^(\d+)?(\.)(\d+)$)/ ? true : false
end

def self.to_bool(str)
if str =~ /^true$/
true
elsif str =~ /^false$/
false
else
#raise(ArgumentError.new "incorrect element #{str}")
nil
end
end

def self.natural_sort_by(array)
array.sort_by{|x| self.naturalize_str(yield(x))}
end

def self.natural_sort(array, options={})
if block_given?
BlockFoundError
else
array.sort do |a,b|
if options[:reverse] == true
self.naturalize_str(b.to_s) <=> self.naturalize_str(a.to_s)
else
self.naturalize_str(a.to_s) <=> self.naturalize_str(b.to_s)
end
end
end
end

def self.only(hash, *keys)
keys.map!{|key| hash.convert_key(key)} if hash.respond_to?(:convert_key, true)
keys.each_with_object(hash.class.new){|k, new_hash| new_hash[k] = hash[k] if hash.has_key?(k)}
end

def self.dig(collection, *values)
current_val = nil
current_collection = collection
values.each_with_index do |val,i|
if i+1 == values.length
if (current_collection.is_a?(Array) && val.is_a?(Integer)) || (current_collection.is_a?(Hash) && ['String','Symbol'].include?(val.class.name))
current_val = current_collection[val]
else
current_val = nil
end
elsif current_collection.is_a?(Array)
if val.is_a?(Integer)
current_collection = current_collection[val]
next
else
current_val = nil
break
end
elsif current_collection.is_a?(Hash)
if ['Symbol','String'].include?(val.class.name)
current_collection = current_collection[val]
next
else
current_val = nil
break
end
else
current_val = nil
break
end
end

return current_val
end

class BlockFoundError < StandardError
def initialize(klass=nil)
super("Rearmed doesn't yet support a block on this method.")
end
end

class NoArgOrBlockGivenError < StandardError
def initialize(klass=nil)
super("Must pass an argument or a block.")
end
end

class BothArgAndBlockError < StandardError
def initialize(klass=nil)
super("Arguments and blocks must be used seperately.")
end
end

private

def self.naturalize_str(str)
str.to_s.split(/(\d+)/).map{|a| a =~ /\d+/ ? a.to_i : a}
end

end
Loading

0 comments on commit 0f504fc

Please sign in to comment.