footing 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # Footing
2
2
 
3
- #### NOTE: this lib is experimental at the moment
4
-
5
3
  Footing provides some sanity for monkey patching practices.
6
4
  It's also a utility lib that contains additional functionality for core objects that you might find useful.
7
5
 
6
+ #### NOTE: this lib is experimental at the moment
7
+
8
8
  ## No implicit monkey patching
9
9
 
10
10
  **No surprises here.** You must explicitly patch.
@@ -59,10 +59,19 @@ For the lazy and brave, you can also patch everything at once.
59
59
  Footing.patch_all!
60
60
  ```
61
61
 
62
+ ## Patch free
63
+
64
+ Dont like monkey patches? Run patch free by setting up utility methods instead.
65
+
66
+ ```ruby
67
+ Footing.util! Footing::String
68
+ Footing::String.escape "foo", "o" # => "f\\o\\o"
69
+ ```
70
+
62
71
  ## Kick the tires
63
72
 
64
73
  * `git clone git://github.com/hopsoft/footing.git`
65
74
  * `cd /path/to/footing`
66
75
  * `bundle`
67
76
  * `./console`
68
- * `Footing.patch String, Footing::String`
77
+ * `Footing.patch! String, Footing::String`
@@ -0,0 +1,30 @@
1
+ module Footing
2
+ module Hash
3
+
4
+ def self.included(mod)
5
+ mod.send :include, InstanceMethods
6
+ end
7
+
8
+ module InstanceMethods
9
+
10
+ # Rekeys the Hash by invoking a method on the existing keys
11
+ # and uses the return value as the new key.
12
+ #
13
+ # NOTE: Creates and return a new Hash.
14
+ #
15
+ # Example:
16
+ # h = { [1] => "short", [1,2] => "medium", [1,2,3] => "long" }
17
+ # h.rekey(:length) # => { 1 => "short", 2 => "medium", 3 => "long" }
18
+ #
19
+ # @param [Symbol] name The method name to invoke on the existing keys.
20
+ # @return [Hash] A new Hash that has been re-keyed.
21
+ def rekey(method_name)
22
+ inject({}) do |new_hash, (key, value)|
23
+ new_hash[key.send(method_name)] = value
24
+ new_hash
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -1,5 +1,26 @@
1
1
  module Footing
2
2
  module Kernel
3
3
 
4
+ def self.included(mod)
5
+ mod.send :include, InstanceMethods
6
+ end
7
+
8
+ module InstanceMethods
9
+
10
+ # Safely evals text inside of a sandbox.
11
+ # @see http://phrogz.net/programmingruby/taint.html Ruby safe level description.
12
+ # @param [String] text The text to eval.
13
+ # @param [Integer] level The safe level to apply.
14
+ # @return [Object]
15
+ def safe_eval(text, level=4)
16
+ sandbox = lambda do
17
+ $SAFE = level
18
+ eval(text.to_s)
19
+ end
20
+ sandbox.call
21
+ end
22
+
23
+ end
24
+
4
25
  end
5
26
  end
@@ -7,6 +7,23 @@ module Footing
7
7
 
8
8
  module InstanceMethods
9
9
 
10
+ # Returns a positive representation of the number.
11
+ def positive
12
+ return self if self >= 0
13
+ flip_sign
14
+ end
15
+
16
+ # Returns a negative representation of the number.
17
+ def negative
18
+ return self if self < 0
19
+ flip_sign
20
+ end
21
+
22
+ # Flips the sign on the number making it either either positive or negative.
23
+ def flip_sign
24
+ self * -1
25
+ end
26
+
10
27
  # Returns the percentage that this number is of the passed number.
11
28
  # @example
12
29
  # 8.percent_of(10) # => 80.0
@@ -2,9 +2,21 @@ module Footing
2
2
  module String
3
3
 
4
4
  def self.included(mod)
5
+ mod.extend ClassMethods
5
6
  mod.send :include, InstanceMethods
6
7
  end
7
8
 
9
+ module ClassMethods
10
+
11
+ ## Generates a random string (upcase alpha-numeric)
12
+ ## Returns a string with the length provided, defaulting to 12 chars
13
+ def random(length=12)
14
+ chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
15
+ (0...length).map { chars.split('')[rand(chars.length)] }.join
16
+ end
17
+
18
+ end
19
+
8
20
  module InstanceMethods
9
21
 
10
22
  # Escapes a series of chars in the current string.
@@ -16,5 +28,6 @@ module Footing
16
28
  end
17
29
 
18
30
  end
31
+
19
32
  end
20
33
  end
data/lib/footing.rb CHANGED
@@ -6,7 +6,8 @@ module Footing
6
6
  "Kernel",
7
7
  "Object",
8
8
  "String",
9
- "Numeric"
9
+ "Numeric",
10
+ "Hash"
10
11
  ]
11
12
 
12
13
  list.each do |name|
@@ -34,6 +35,28 @@ module Footing
34
35
  context.send :include, extension
35
36
  end
36
37
 
38
+ # Creates class methods for all InstanceMethods in the module.
39
+ # This allows users to invoke utility methods rather than monkey patching if they so desire.
40
+ # @param [Module] mod The Module to setup util methods for.
41
+ def self.util!(mod)
42
+ proxy = ::Object.new
43
+ proxy_eigen = class << proxy
44
+ self
45
+ end
46
+
47
+ Footing.patch! proxy, mod
48
+
49
+ eigen = class << mod
50
+ self
51
+ end
52
+
53
+ mod.const_get("InstanceMethods").instance_methods(false).each do |method|
54
+ eigen.send :define_method, method do |value, *args|
55
+ proxy_eigen.instance_method(method).bind(value).call(*args)
56
+ end
57
+ end
58
+ end
59
+
37
60
  end
38
61
 
39
62
  Dir[File.expand_path(File.join(File.dirname(__FILE__), "**/*.rb"))].each do |file|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: footing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-23 00:00:00.000000000 Z
12
+ date: 2012-07-24 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! " Footing provides some sanity for monkey patching practices.\n
15
15
  \ It's also a utility lib that contains additional functionality for core objects
@@ -20,6 +20,7 @@ executables: []
20
20
  extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
+ - lib/extensions/hash.rb
23
24
  - lib/extensions/kernel.rb
24
25
  - lib/extensions/numeric.rb
25
26
  - lib/extensions/object.rb