manic_monkey 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.md ADDED
@@ -0,0 +1,8 @@
1
+ # The MIT License (MIT) #
2
+ Copyright (c) 2012 David Southard
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # zookeeper #
2
+ Some simple monkeypatches to ruby core classes
3
+
4
+ ## String ##
5
+ The string class now has an unindent method. This is useful for things like HERE docs.
6
+
7
+ Sample usage
8
+
9
+ *This will produce a string with a two space indent*
10
+ ```ruby
11
+ def some_large_string
12
+ <<-HERE
13
+ ███████▄▄███████████▄
14
+ ▓▓▓▓▓▓█░░░░░░░░░░░░░░█
15
+ ▓▓▓▓▓▓█░░░░░░░░░░░░░░█
16
+ ▓▓▓▓▓▓█░░░░░░░░░░░░░░█
17
+ ▓▓▓▓▓▓█░░░░░░░░░░░░░░█
18
+ ▓▓▓▓▓▓█░░░░░░░░░░░░░░█
19
+ ▓▓▓▓▓▓███░░░░░░░░░░░░█
20
+ ██████▀▀▀█░░░░██████▀
21
+ ░░░░░░░░░█░░░░█
22
+ ░░░░░░░░░░█░░░█
23
+ ░░░░░░░░░░░█░░█
24
+ ░░░░░░░░░░░█░░█
25
+ ░░░░░░░░░░░░▀▀
26
+ HERE
27
+ end
28
+ ```
29
+
30
+ *With unindent String monkey patch... no spaces*
31
+ ```ruby
32
+ def some_large_string
33
+ <<-HERE.unindent
34
+ ░░░░░░░░░░░░░░░░░░░░░░░▄▄
35
+ ░░░░░░░░░░░░░░░░░░░░░░█░░█
36
+ ░░░░░░░░░░░░░░░░░░░░░░█░░█
37
+ ░░░░░░░░░░░░░░░░░░░░░█░░░█
38
+ ░░░░░░░░░░░░░░░░░░░░█░░░░█
39
+ ░░░░░░░░░░░███████▄▄█░░░░░██████▄
40
+ ░░░░░░░░░░░▓▓▓▓▓▓█░░░░░░░░░░░░░░█
41
+ ░░░░░░░░░░░▓▓▓▓▓▓█░░░░░░░░░░░░░░█
42
+ ░░░░░░░░░░░▓▓▓▓▓▓█░░░░░░░░░░░░░░█
43
+ ░░░░░░░░░░░▓▓▓▓▓▓█░░░░░░░░░░░░░░█
44
+ ░░░░░░░░░░░▓▓▓▓▓▓█░░░░░░░░░░░░░░█
45
+ ░░░░░░░░░░░▓▓▓▓▓▓█████░░░░░░░░░█
46
+ ░░░░░░░░░░░██████▀░░░░▀▀██████▀
47
+ ◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈
48
+ ░█░░░█░█░▄▀░█▀▀░░░░▀█▀░█░█░█░▄▀▀░
49
+ ░█░░░█░█▀░░░█▀░░▄▄░░█░░█▀█░█░░▀▄░
50
+ ░█▄▄░█░█░▀▄░█▄▄░░░░░█░░█░█░█░▄▄▀░
51
+ ◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈☻◈
52
+ HERE
53
+ end
54
+ ```
55
+ ## Hash ##
56
+
57
+ ### Hash#symbolize_keys! ###
58
+ Rails Hash method *symbolize_keys!* to turn keys of strings into symbols
59
+
60
+ ### Hash#deep_symbolize
61
+ Deep symbolize method to symbolize keys recursively
62
+ [See this gist for more information.](https://gist.github.com/998709)
63
+
64
+ Symbolizes all of hash's keys and subkeys.
65
+ Also allows for custom pre-processing of keys (e.g. downcasing, etc)
66
+ if the block is given:
67
+
68
+ ```ruby
69
+ somehash.deep_symbolize { |key| key.downcase }
70
+ ```
71
+
72
+ #### Usage ####
73
+ Either include it into global Hash class to make it available to all hashes, or extend only your own hash objects with this module.
74
+
75
+ Example 1:
76
+
77
+ ```ruby
78
+ class Hash
79
+ include DeepSymbolizable
80
+ end
81
+ ```
82
+ Example 2:
83
+
84
+ ```ruby
85
+ myhash.extend DeepSymbolizable
86
+ ```
@@ -0,0 +1,62 @@
1
+ # https://gist.github.com/998709
2
+
3
+ # Symbolizes all of hash's keys and subkeys.
4
+ # Also allows for custom pre-processing of keys (e.g. downcasing, etc)
5
+ # if the block is given:
6
+ #
7
+ # somehash.deep_symbolize { |key| key.downcase }
8
+ #
9
+ # Usage: either include it into global Hash class to make it available to
10
+ # to all hashes, or extend only your own hash objects with this
11
+ # module.
12
+ # E.g.:
13
+ # 1) class Hash; include DeepSymbolizable; end
14
+ # 2) myhash.extend DeepSymbolizable
15
+
16
+ module DeepSymbolizable
17
+ def deep_symbolize(&block)
18
+ method = self.class.to_s.downcase.to_sym
19
+ syms = DeepSymbolizable::Symbolizers
20
+ syms.respond_to?(method) ? syms.send(method, self, &block) : self
21
+ end
22
+
23
+ module Symbolizers
24
+ extend self
25
+
26
+ # the primary method - symbolizes keys of the given hash,
27
+ # preprocessing them with a block if one was given, and recursively
28
+ # going into all nested enumerables
29
+ def hash(hash, &block)
30
+ hash.inject({}) do |result, (key, value)|
31
+ # Recursively deep-symbolize subhashes
32
+ value = _recurse_(value, &block)
33
+
34
+ # Pre-process the key with a block if it was given
35
+ key = yield key if block_given?
36
+ # Symbolize the key string if it responds to to_sym
37
+ sym_key = key.to_sym rescue key
38
+
39
+ # write it back into the result and return the updated hash
40
+ result[sym_key] = value
41
+ result
42
+ end
43
+ end
44
+
45
+ # walking over arrays and symbolizing all nested elements
46
+ def array(ary, &block)
47
+ ary.map { |v| _recurse_(v, &block) }
48
+ end
49
+
50
+ # handling recursion - any Enumerable elements (except String)
51
+ # is being extended with the module, and then symbolized
52
+ def _recurse_(value, &block)
53
+ if value.is_a?(Enumerable) && !value.is_a?(String)
54
+ # support for a use case without extended core Hash
55
+ value.extend DeepSymbolizable unless value.class.include?(DeepSymbolizable)
56
+ value = value.deep_symbolize(&block)
57
+ end
58
+ value
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,13 @@
1
+ class Hash
2
+ include DeepSymbolizable
3
+
4
+ def symbolize_keys!
5
+ keys.each do |key|
6
+ self[(key.to_sym rescue key) || key] = delete(key)
7
+ end
8
+ self
9
+ end
10
+
11
+ end
12
+
13
+
@@ -0,0 +1,8 @@
1
+ class String
2
+ # Strip leading whitespace from each line that is the same as the
3
+ # amount of whitespace on the first line of the string.
4
+ # Leaves _additional_ indentation on later lines intact.
5
+ def unindent
6
+ gsub /^#{self[/\A\s*/]}/, ''
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'manic_monkey/string_patches'
2
+ require 'manic_monkey/deep_symbolizable'
3
+ require 'manic_monkey/hash_patches'
data/lib/version.rb ADDED
@@ -0,0 +1,4 @@
1
+ class ManicMonkey
2
+ VERSION = '0.0.1'
3
+ end
4
+
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: manic_monkey
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Southard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A few ruby core patches in a gem.
31
+ email:
32
+ - nacengineer@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/manic_monkey.rb
38
+ - lib/version.rb
39
+ - lib/manic_monkey/string_patches.rb
40
+ - lib/manic_monkey/hash_patches.rb
41
+ - lib/manic_monkey/deep_symbolizable.rb
42
+ - LICENSE.md
43
+ - README.md
44
+ homepage:
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: 1.9.1
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.24
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Monkey patches for ruby core classes
68
+ test_files: []