gemmyrb 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 959c5dce4ee3611f0e6a9a1ee91a8da5ed128b3b
4
- data.tar.gz: 2a2349bd342e424e17246d58d951b6e99dd74538
3
+ metadata.gz: '0597ccc5d58c800b09db4032d7f3b4c9417a200c'
4
+ data.tar.gz: d8cf7ba24fd1e4b423d2a8bdc2accd984b441d21
5
5
  SHA512:
6
- metadata.gz: 7115fb4d4a8f4a152be9e24ac51bfd120101cd73d69d3356579aeda8526a83e7f77b99362571fdd2d1646849b0b6bbe703d34619f1234077b51245b61efcacd5
7
- data.tar.gz: dc1d6dafb2a9e55032622f6aebbd38e540a6ba55998f1d3aa3048a71248fd50dfe9ef228e394c5072290ae8de90afadbc350d350e26ca14a596f549bef402207
6
+ metadata.gz: 61bd6b8d5e1ecb3735cf5da9b664aa0de07006402d386c86492c8b1ff763119895fce69758fcb04fc86cdd6b10a9e47ec805eb797ff420f21554cebf92966930
7
+ data.tar.gz: 0ce7ac19a0e282bd31f2644d032d5f334c89192af8a8f0f2765137066d1fb60f90f2de371e02fd1795bca328c69993c3b2b7e3639cb90d4fa7b4950c273993bc
data/README.md CHANGED
@@ -22,9 +22,8 @@ documents:
22
22
  - _Shell commands and other one-off processes_
23
23
  - [examples/03_shell_commands.rb](./examples/03_shell_commands.rb)
24
24
 
25
- **To see a full list of the patched methods (there are a lot since I've
26
- included many from facets), see the [rubydoc](http://www.rubydoc.info/gems/gemmyrb)**
25
+ **To see a full list of the patched methods, see the [rubydoc](http://www.rubydoc.info/gems/gemmyrb)**
27
26
 
28
27
  Specifically, look at the constants defined on Gemmy::Patches. There is one
29
28
  module for each of the core classes being patched. Each individual method
30
- is also contained in its own module (to make modular inclusion possible)
29
+ is also contained in its own module (for the sake of, well, modularity)
@@ -4,12 +4,24 @@ module Gemmy::Patches::ArrayPatch
4
4
 
5
5
  module ClassMethods
6
6
 
7
+ module Wrap
8
+ using CF::Array[:wrap]
9
+ def self.autotest
10
+ Array.wrap(nil).eql?([]) &&\
11
+ Array.wrap([]).eql?([]) &&\
12
+ Array.wrap(1).eql?([1])
13
+ end
14
+ end
15
+
7
16
  module Zip
8
17
  # facets
9
18
  def zip(*arrays)
10
19
  return [] if arrays.empty?
11
20
  return arrays[0].zip(*arrays[1..-1])
12
21
  end
22
+ def self.autotest
23
+ Array.zip([1], [2]) == [[1,2]]
24
+ end
13
25
  end
14
26
 
15
27
  end
@@ -22,18 +34,24 @@ module Gemmy::Patches::ArrayPatch
22
34
  def exclude?(x)
23
35
  ! include? x
24
36
  end
37
+ def self.autotest
38
+ [1].exclude?(2) && ![2].exclude?(2)
39
+ end
25
40
  end
26
41
 
27
42
  module KeyBy
28
43
  # facets
29
44
  def key_by
30
45
  return to_enum(:key_by) unless block_given?
31
- h = {}
46
+ h = Hash.new { |h,k| h[k] = [] }
32
47
  each do |v|
33
- h[yield(v)] = v
48
+ h[yield(v)] << v
34
49
  end
35
50
  return h
36
51
  end
52
+ def self.autotest
53
+ [1,2,3].key_by { |v| v % 2 } == { 1 => [1, 3], 0 => [2] }
54
+ end
37
55
  end
38
56
 
39
57
  module After
@@ -42,6 +60,9 @@ module Gemmy::Patches::ArrayPatch
42
60
  return nil unless include? value
43
61
  self[(index(value).to_i + 1) % length]
44
62
  end
63
+ def self.autotest
64
+ [1,2].after(1).eql?(2)
65
+ end
45
66
  end
46
67
 
47
68
  module Before
@@ -50,6 +71,9 @@ module Gemmy::Patches::ArrayPatch
50
71
  return nil unless include? value
51
72
  self[(index(value).to_i - 1) % length]
52
73
  end
74
+ def self.autotest
75
+ [1,2].before(2) == 1
76
+ end
53
77
  end
54
78
 
55
79
  module Duplicates
@@ -61,16 +85,8 @@ module Gemmy::Patches::ArrayPatch
61
85
  }
62
86
  h.delete_if{|_,v| v < min}.keys
63
87
  end
64
- end
65
-
66
- module ExtractOptions
67
- # facets
68
- def extract_options!
69
- if Hash === last && last.extractable_options?
70
- pop
71
- else
72
- {}
73
- end
88
+ def self.autotest
89
+ [1,1,2,2,3].duplicates == [1,2]
74
90
  end
75
91
  end
76
92
 
@@ -0,0 +1,23 @@
1
+ module Gemmy::Patches::EnumeratorPatch
2
+ module ClassMethods
3
+ end
4
+ module InstanceMethods
5
+ module MapBy
6
+ using CF::Enumerable::MapBy
7
+ # maps to hash
8
+ # Each iteration returns array [hash_key, hash_val]
9
+ # hash values are stored in arrays
10
+ def map_by(&blk)
11
+ clone.map_by &blk
12
+ end
13
+ end
14
+
15
+ module MapTo
16
+ using CF::Enumerable::MapTo
17
+ # map to <klass> instances, passing self as arg to klass constructor
18
+ def map_to(klass)
19
+ clone.map_to klass
20
+ end
21
+ end
22
+ end
23
+ end
@@ -5,7 +5,17 @@ module Gemmy::Patches::SymbolPatch
5
5
  module ClassMethods
6
6
  end
7
7
 
8
- module InstanceMethods
8
+ module InstanceMethods
9
+
10
+ module Call
11
+ using CF::Symbol[:call]
12
+ def call(*args, &blk)
13
+ Gemmy.patch("symbol/i/call")._call self, *args, &blk
14
+ end
15
+ def self._call(sym, *args, &blk)
16
+ sym.call *args, &blk
17
+ end
18
+ end
9
19
 
10
20
  module Variablize
11
21
  # facets
@@ -30,5 +40,4 @@ module InstanceMethods
30
40
 
31
41
  end
32
42
 
33
-
34
43
  end
data/lib/gemmy/patches.rb CHANGED
@@ -13,6 +13,17 @@
13
13
  #
14
14
  module Gemmy::Patches
15
15
 
16
+ # searches for the 'autotest' method on any patch modules
17
+ # and runs it, checking that the return value is true
18
+ # raises an error if it's not
19
+ def self.autotest
20
+ class_refinements.each do |patch_klass|
21
+ if patch_klass.respond_to?(:autotest)
22
+ patch_klass.autotest || raise(RuntimeError, "#{patch_klass} failed")
23
+ end
24
+ end
25
+ end
26
+
16
27
  # The usage of this method is to load all the patches for some core classes.
17
28
  # With no arguments it will include all patches
18
29
  # There are two optional params (keyword arguments).
@@ -81,9 +92,7 @@ module Gemmy::Patches
81
92
  end
82
93
 
83
94
  def self.patch_as_instance_method(core_klass, patch_klass)
84
- patch_klass.send(:refine, core_klass) do
85
- include patch_klass
86
- end
95
+ patch_klass.send(:refine, core_klass) { include patch_klass }
87
96
  end
88
97
 
89
98
  def self.core_patches
@@ -99,7 +108,8 @@ module Gemmy::Patches
99
108
  Class: Gemmy::Patches::ClassPatch,
100
109
  Exception: Gemmy::Patches::ExceptionPatch,
101
110
  Float: Gemmy::Patches::FloatPatch,
102
- Proc: Gemmy::Patches::ProcPatch
111
+ Proc: Gemmy::Patches::ProcPatch,
112
+ Enumerator: Gemmy::Patches::EnumeratorPatch
103
113
  }.with_indifferent_access
104
114
  end
105
115
 
data/lib/gemmy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Gemmy
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
data/lib/gemmy.rb CHANGED
@@ -3,11 +3,43 @@ require 'colored'
3
3
  require 'active_support/all'
4
4
  require 'pry'
5
5
  require 'colored'
6
+ require 'corefines'
6
7
 
7
8
  # Container class for all the functionality.
8
9
  #
9
10
  class Gemmy
10
11
 
12
+ # Used by patches to get a reference to static patch classes
13
+ # Without this there would be long, unqualified constant names such as
14
+ # Gemmy::Patches::SymbolPatch::InstanceMethods::Call
15
+ #
16
+ # Usage is to pass a string like so: "<CoreClass>/<Context>/<MethodName>"
17
+ # Core class could be "symbol" for example
18
+ # Context is either "i" for instance or "c" for class
19
+ # Method name is underscored, i.e. "call" in this example:
20
+ #
21
+ # klass = Gemmy.patch "symbol/i/call"
22
+ #
23
+ # Now I can call any class methods on the klass.
24
+ #
25
+ # The utility of this might not be obvious, but it is useful
26
+ # when using another library's refinements in Gemmy's own
27
+ def self.patch(string)
28
+ parts = string.split("/")
29
+ raise ArgumentError unless parts.length == 3
30
+ core_class, context, method_name = parts
31
+ context_classname = if context.eql?("i")
32
+ "InstanceMethods"
33
+ elsif context.eql?("c")
34
+ "ClassMethods"
35
+ else
36
+ raise ArgumentError
37
+ end
38
+ Gemmy::Patches.const_get("#{core_class.capitalize}Patch")
39
+ .const_get(context_classname)
40
+ .const_get method_name.camelcase
41
+ end
42
+
11
43
  # There are two main usages:
12
44
  # - namespaced (using refinements and explicit include/extend calls)
13
45
  # - global
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gemmyrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - max pleaner
@@ -86,6 +86,20 @@ dependencies:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
88
  version: 0.19.4
89
+ - !ruby/object:Gem::Dependency
90
+ name: corefines
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 1.9.0
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 1.9.0
89
103
  description: ''
90
104
  email: maxpleaner@gmail.com
91
105
  executables:
@@ -102,6 +116,7 @@ files:
102
116
  - lib/gemmy/patches.rb
103
117
  - lib/gemmy/patches/array_patch.rb
104
118
  - lib/gemmy/patches/class_patch.rb
119
+ - lib/gemmy/patches/enumerator_patch.rb
105
120
  - lib/gemmy/patches/exception_patch.rb
106
121
  - lib/gemmy/patches/float_patch.rb
107
122
  - lib/gemmy/patches/hash_patch.rb