betterobject 1.0.0 → 1.1.0

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: 9f0fe8e69cf1f96c5d5150fca67a2b1ac82ebc54
4
- data.tar.gz: dbed0f676c842b23bbcc562410a4931090ba82d9
3
+ metadata.gz: 8c0a48f6ca647b2e04d0da3ae2684874d64053e1
4
+ data.tar.gz: 8f47dec33142fc3c804e5f2577744668d2e68b63
5
5
  SHA512:
6
- metadata.gz: 3e7baa787819509439a2fec0c2cf44f7a1c78aed1aed3b26717454a05ca484f4a01c5494c7bc795ca4f05f19335421f06a5b301acaaf9f7098bf15471a71d613
7
- data.tar.gz: 82c0a7b11eb65ce5f5423e4eb5e5310d10d186c0748aa63937a5b1f9e6944bf92e22a21cfdeca35229901d0c4edf6a91d7be02171524a1e95765c61c6791bcae
6
+ metadata.gz: 6d572278169c31ae29c5ffc5e2f270ed7c74cbd6a8b3c404dd913bf5b75a5a4c3d77dd1ae484eca241a9cda56b8906d12b6a5d28455a56f8a1ec33b29c54fdec
7
+ data.tar.gz: b6be73de85b765e1b7d1ecc6e3cdb9b5b0d9fe8fa35270313810240616b4c33996e057489327c6b4ce6bbef782499fbe6ba148578a8da2ba1ad584ded42372e1
data/README.md CHANGED
@@ -4,11 +4,8 @@ This gem installs several class methods to Object which in turn generates both c
4
4
  In order to prevent name pollution, you have the ability to manage the generators to pick alternate names if you prefer.
5
5
  After scouring the RubyGems site, some of the better Class upgrades are included here as well as some of my own.
6
6
  The gem creates the backbone upon which future upgrades should be forthcoming.
7
- As a teaser, some of the generators presently include: obj.local_methods, obj.inherited_methods, obj.in?, COBJ.comes_from?, COBJ.derives_from?, COBJ.define_presence_of, obj.find_def. This first release installs 14 generators.
8
- Calling `Object.better_install_all` will install all of the generators.
9
- You can also generate a subset by calling `Object.better_install :generator_name` or `Object.better_install array_of_gen_names`.
10
- The generator names are also the method names which can be renamed by calling `Object.better_rename(old_name, new_name)`;
11
- this must be done before you generate the method.
7
+ Generators are code in waiting. Only when installed do they become methods (either instance or class methods depending on the generator).
8
+ The generator names are also the method names which can be renamed in the event that you get a name conflict.
12
9
 
13
10
  ## Installation
14
11
 
@@ -85,6 +82,7 @@ You can control which methods names get generated. Now let's dive into the other
85
82
  * `Object.better_source_code`
86
83
  * `Object.better_list`
87
84
  * `Object.better_install`
85
+ * `Object.better_install_as!`
88
86
  * `Object.better_install_all`
89
87
  * `Object.better_uninstall`
90
88
  * `Object.better_uninstall_all`
@@ -92,6 +90,7 @@ You can control which methods names get generated. Now let's dive into the other
92
90
  * `Object.better_unskip`
93
91
  * `Object.better_skipped`
94
92
  * `Object.better_rename`
93
+ * `Object.include_many?`
95
94
 
96
95
 
97
96
  Note that there are no instance methods created by this gem until you install a generator.
@@ -156,6 +155,25 @@ Object.better_install :methods_by_me
156
155
  "a string".methods_by_me # [list of local methods]
157
156
  ```
158
157
 
158
+ ### Object.better_install_as!(:generator_library_name, :permanent_clone_name)
159
+
160
+ This forces permanent installation of a generator to an alternate target name.
161
+ Note that once done, this alternate generator is permanently installed and cannot be removed.
162
+ If you are creating a `gem` that requires `betterobject`, this type of installation will prevent someone else's code from deleting what you require.
163
+ Alternately, you can capture the source code, and physically place the code in Object.
164
+ This call leaves no trace once it is called other than the methods appearing from the generator.
165
+ If you are using `betterobject` as part of a library `gem` project or a component then you should only install with this method and only install the minimum of what you need.
166
+ A good naming convention could be prefixing the generator name with your gem name.
167
+ As an example, this was used in the `setfu` gem as follows:
168
+
169
+ ```ruby
170
+ class Object
171
+ better_install_as!(:push_unique, :setfu_push_unique)
172
+ better_install_as!(:tag, :setfu_tag)
173
+ better_install_as!(:tag, :setfu_count)
174
+ end
175
+ ```
176
+
159
177
  ### Object.better_list(include_skip=false)
160
178
 
161
179
  This returns an array of generators that can be installed. Skipped generators can also be listed if you call this method with the value `true`.
@@ -296,13 +314,21 @@ you must provide code to remove your methods.
296
314
  * `:comes_from?`
297
315
  * `:define_presence_of`
298
316
  * `:in?`
317
+ * `:include_many?`
299
318
  * `:to_literal`
300
319
  * `:pluralize`
301
320
  * `:parent`
302
- * `:sort!`
303
321
  * `:find_def`
304
322
  * `:tag`
323
+ * `:create_tag`
305
324
  * `:functionize`
325
+ * `:push_unique`
326
+ * `:sort!`
327
+ * `:many_at`
328
+ * `:delete_many_at`
329
+ * `:tripleize`
330
+
331
+
306
332
 
307
333
 
308
334
  ### Generator :local_methods(include_singleton_methods=true)
@@ -452,8 +478,8 @@ This class method can be called from Object, String, or any class you wish.
452
478
  The pluralization is pretty smart, but if it does not quite work, you can provide the altername name in the second parameter. The new method name is returned. See the example below
453
479
 
454
480
  ```ruby
455
-
456
481
  Object.better_install :pluralize # installs the first-level generator
482
+
457
483
  Object.pluralize :include? # returns :includes?
458
484
  [1,2,3].includes? 2 # true
459
485
 
@@ -468,7 +494,18 @@ Object.pluralize :respond_to? # :responds_to?
468
494
 
469
495
  This generator finds the first non-module ancestor of a class.
470
496
  The class Object's parent is always Object.
471
- This is a class method installed on Object.
497
+ This is a class method installed on Object. See the example below:
498
+
499
+ ```ruby
500
+ Object.better_install :parent
501
+
502
+ 6.class.ancestors # [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
503
+ 6.class # Fixnum
504
+ 6.class.parent # Integer
505
+ 6.class.parent.parent # Numeric
506
+ 6.class.parent.parent.parent # Object ... Note: Comparable is a Module
507
+ 6.class.parent.parent.parent.parent.parent.parent.parent.parent.parent # Object
508
+ ```
472
509
 
473
510
  ### Generators :to_literal
474
511
 
@@ -529,7 +566,7 @@ See the example below:
529
566
  str = "Tag me!"
530
567
  str.tag1!
531
568
  str.tag2 = "forget me not"
532
- str.ta3 = 17
569
+ str.tag3 = 17
533
570
  str.tag1? # true
534
571
  str.tag2? # true
535
572
  str.flag? # false
@@ -617,11 +654,94 @@ ary.push_unique "your boat"
617
654
  ary.join " " # "row row row your boat"
618
655
  ```
619
656
 
657
+ ### Generators :many_at(*list)
658
+
659
+ This targets the Array class with an improved access method. You can pass an array of items, or a comma delimited list of items;
660
+ items include integer indexes, ranges, and flat arrays of integers. See the example below:
661
+
662
+ ```ruby
663
+ Object.better_install :many_at
664
+
665
+ ary = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight"]
666
+ stuff = ary.many_at 1, 3..5, 8 # stuff == ["one", "three", "four", "five", "eight"]
667
+ stuff = ary.many_at [1, 3..5, 8] # same as above
668
+ stuff = ary.many_at 1, [3,4,5], 8 # same as above
669
+ ```
670
+
671
+ ### Generators :delete_many_at(*list)
672
+ This targets the Array class with an improved `#delete_at` method. You can pass an array of items, or a comma delimited list of items;
673
+ items include integer indexes, ranges, and flat arrays of integers. The method returns the deleted items as an array of elements
674
+ in the reverse order in which they were deleted. Note that deletion indexes are internally sorted in reverse order in order to preserve the index integrity.
675
+ If the list of deletion indexes is ascending, then we should see the same data as if we called `#many_at`. See the example below:
676
+
677
+ ```ruby
678
+ Object.better_install :delete_many_at
679
+
680
+ ary = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight"]
681
+ stuff = ary.delete_many_at 1, 3..5, 8 # stuff == ["one", "three", "four", "five", "eight"]
682
+ # ary == ["zero", "two", "six", "seven"]
683
+ # random delete order:
684
+ ary = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight"]
685
+ stuff = ary.delete_many_at 8, 3..5, 1 # stuff == ["one", "three", "four", "five", "eight"]
686
+ # ary == ["zero", "two", "six", "seven"]
687
+ ```
688
+
689
+ ### Generators :tripleize
690
+ This installs a generator that defines the `===` operator on a particular existing class such as Array or Hash.
691
+ Note that the `===` is used in conjunction with the `case`-`when` statement.
692
+ The installed generator can be called with or without a block.
693
+ The default behavior is to mimic what a Range does with the `===` operator.
694
+ This is best explained by example:
695
+ ```ruby
696
+ (1..9) === 5 # true ... Range defines === as: self.include? (right_expr)
697
+ [1,2,3,4,5,6,7,8,9] === 5 # false ... Kernel defines === as: self == (right_expr)
698
+
699
+ Object.better_install :tripleize
700
+
701
+ Array.tripleize # install default behavior
702
+ [1,2,3,4,5,6,7,8,9] === 5 # true
703
+
704
+ Array.tripleize { |me , other| me.first == other }
705
+ [1,2,3,4,5,6,7,8,9] === 5 # false
706
+ [1,2,3,4,5,6,7,8,9] === 1 # true
707
+ ```
708
+
709
+ ### Generators :include_many?
710
+ This is an instance generator on Object that creates a generalized instance method called `#include_many?` or some other name
711
+ you think is better. If an Object implements `#include`, then `#include_many?` should work fine.
712
+ Note that the `#include` method only looks at a single item, where `#include_many?` requires that all items are included.
713
+ You can pass a list of items, or pass an `Array` of items. See the example below:
714
+
715
+ ```ruby
716
+ Object.better_install :include_many?
717
+
718
+ [1,2,3,4,5,6,7,8].include? [3,4,7] # false
719
+ [1,2,3,4,5,6,7,8].include_many? [3,4,7] # true
720
+ [1,2,3,4,5,6,7,8].include_many? 3,4,7 # true
721
+ [1,2,3,4,5,6,7,8].include_many? 3 # true
722
+ [1,2,3,4,5,6,7,8].include_many? 1,9 # false
723
+ [1,2,3,[4,5],[6,7],8].include_many? [[4,5],[6,7],1] # true ... not: [4,5],[6,7],1 see note below:
724
+ [1,2,3,[4,5],[6,7],8].include_many? [4,5],[6,7],1 # false ... same as: .include_many? 4,5 ... see note below:
725
+ [1,2,3,[4,5],[6,7],8].include_many? 1,[4,5],[6,7] # true ... see note below:
726
+ "This is a test string".include_many? "test", "string" # true
727
+ ```
728
+
729
+ Note that it is best to encapsulate the list inside an array, particularly if your elements are arrays.
730
+ If the first item in the list is an array, then its contents will be the only thing tested.
731
+
732
+
620
733
  ## Change Log
621
734
 
735
+ ### Version 1.1.0
736
+ 1. Update documentation
737
+ 2. Added generator `:delete_many_at`
738
+ 3. Added generator `:many_at`
739
+ 4. Added generator `:tripleize`
740
+ 5. Added generator `:include_many?`
741
+
622
742
  ### Version 1.0.0
623
- 1. Added new generator :push_unique
624
- 2. Added new generator :create_tag
743
+ 1. Added new generator `:push_unique`
744
+ 2. Added new generator `:create_tag`
625
745
 
626
746
  ## Development
627
747
 
@@ -631,7 +751,7 @@ To install this gem onto your local machine, run `bundle exec rake install`.
631
751
 
632
752
  ## Contributing
633
753
 
634
- I need to control this for the time being.
754
+ I need to control this for the time being. You you need a generator, shoot me an email and I will consider it on the next release.
635
755
 
636
756
  ## License
637
757
 
@@ -14,11 +14,11 @@ Gem::Specification.new do |spec|
14
14
  In order to prevent name pollution, you have the ability to manage the generators to pick alternate names if you prefer.
15
15
  After scouring the RubyGems site, some of the better Class upgrades are included here as well as some of my own.
16
16
  The gem creates the backbone upon which future upgrades should be forthcoming.
17
- As a teaser, some of the generators presently include: obj.local_methods, obj.inherited_methods, obj.in?, COBJ.comes_from?, COBJ.derives_from?, COBJ.define_presence_of, obj.find_def. This first release installs 14 generators.
18
- Calling `Object.better_install_all` will install all of the generators.
19
- You can also generate a subset by calling `Object.better_install :generator_name` or `Object.better_install array_of_gen_names`.
20
- The generator names are also the method names which can be renamed by calling `Object.better_rename(old_name, new_name)`;
21
- this must be done before you generate the method.}
17
+ As a teaser, some of the generators presently include: obj.local_methods, obj.inherited_methods, obj.replaced_methods, obj.in?, COBJ.comes_from?, COBJ.derives_from?, obj.find_def.
18
+ There are currently 20 generators and counting.
19
+ Calling Object.better_install_all will install all of the generators.
20
+ You can also generate a subset by calling Object.better_install(:generator_name).
21
+ The generator names are also the method names which can be renamed by calling Object.better_rename(old_name, new_name);}
22
22
 
23
23
  spec.license = "MIT"
24
24
 
@@ -1,8 +1,14 @@
1
1
  require "betterobject/version"
2
+ require "generators.rb"
3
+
4
+ ### ::TODO:: :required ... prevent uninstall forever
5
+ ### ::: Array ... needs a delete_many_at(*prm) ... 1,5,7,3 or an array setfu need this!!!
6
+ ### ::: also, a delete_many(*prm) ... deletes a bunch of stuff based on value
7
+ ### ::: Generator clone ??? another way to tag things .... or provide alternate names
8
+ ### ::: Generator drop ??? removes generator completely
9
+ ### ::: drop after permanent install? nah ... not sure if I want drop either
10
+ ### ::: Object.better_install! :generator ... cannot be uninstalled **** I like this!!!
2
11
 
3
- unless require "generators.rb"
4
- require_relative "generators.rb"
5
- end
6
12
 
7
13
  class Object
8
14
  ###
@@ -17,6 +23,11 @@ class Object
17
23
  raise "unknown generator #{sym.inspect}" unless @@better_object_valid_methods.include? sym
18
24
  end
19
25
 
26
+ def self.better_be_different(old_sym, new_sym)
27
+ raise "unrecognize #{old_sym.inspect}" unless @@better_object_valid_methods.include? old_sym
28
+ raise "new name must be differenct that existing generator" if @@better_object_valid_methods.include? new_sym
29
+ end
30
+
20
31
  def self.better_list(include_skip=false)
21
32
  better_be_object
22
33
  rtn = []
@@ -234,9 +245,19 @@ class Object
234
245
  true
235
246
  end
236
247
 
248
+ def better_install_as!(old_sym, new_sym)
249
+ better_be_object
250
+ better_be_defined(old_sym)
251
+ better_be_different(old_sym, new_sym)
252
+ code = @@better_object_valid_methods[old_sym][:code].gsub(/BO_METH_NAME/,new_sym.to_s)
253
+ class_eval code
254
+ true
255
+ end
256
+
237
257
  def self.better_rename(old_sym, new_sym)
238
258
  better_be_object
239
259
  better_be_defined(old_sym)
260
+ better_be_different(old_sym, new_sym)
240
261
  raise "cannot rename installed generator" if @@better_installed_methods.keys.include? old_sym
241
262
  rec = @@better_object_valid_methods[old_sym]
242
263
  @@better_object_valid_methods[new_sym] = rec
@@ -1,3 +1,3 @@
1
1
  module Betterobject
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -277,7 +277,7 @@ CODE
277
277
  #
278
278
  hash = {}
279
279
  hash[:type] = :class
280
- hash[:explain] = "creates MyClass() on Kernel level as an alternate constructor or some other method"
280
+ hash[:explain] = "creates MyClass() on Kernel level as an alternate constructor or class method"
281
281
  hash[:code] = <<'CODE'
282
282
  def BO_METH_NAME(meth=:new)
283
283
  raise "can't redefine #{self}" if Kernel.methods.include? self.to_s.to_sym
@@ -294,7 +294,7 @@ CODE
294
294
  #
295
295
  hash = {}
296
296
  hash[:type] = :instance
297
- hash[:explain] = "creates :tag, :tag=, :tag?, untag ..."
297
+ hash[:explain] = "creates :tag, :tag=, :tag?, untag, :tag!"
298
298
  hash[:code] = <<'CODE'
299
299
  def BO_METH_NAME
300
300
  nil || @bo_BO_METH_NAME
@@ -381,4 +381,117 @@ hash[:rm] = <<'CODE'
381
381
  CODE
382
382
  @@better_object_valid_methods[:push_unique] = hash
383
383
 
384
+
385
+ #
386
+ # Generator :delete_many_at
387
+ #
388
+ hash = {}
389
+ hash[:type] = :instance
390
+ hash[:explain] = "creates #delete_many_at method on Array. Same as #delete_at, but also allows a list of arguments"
391
+ hash[:code] = <<'CODE'
392
+ Array.class_eval do
393
+ def BO_METH_NAME(*list)
394
+ rtn = []
395
+ list = list.first if list.first.kind_of? Array # must delete right-to_left
396
+ ran = []
397
+ ary = []
398
+ num = []
399
+ list.each do |elm|
400
+ if elm.kind_of? Range
401
+ ran.concat elm.to_a
402
+ elsif elm.kind_of? Array
403
+ ary.concat elm
404
+ else
405
+ num.push elm
406
+ end
407
+ end
408
+ grp = (ran|ary|num).sort.reverse
409
+ grp.each do |idx|
410
+ rtn.push(delete_at(idx))
411
+ end
412
+ return rtn.reverse
413
+ end
414
+ end
415
+ CODE
416
+ hash[:rm] = <<'CODE'
417
+ Array.class_eval do
418
+ undef BO_METH_NAME
419
+ end
420
+ CODE
421
+ @@better_object_valid_methods[:delete_many_at] = hash
422
+
423
+
424
+ #
425
+ # Generator :many_at
426
+ #
427
+ hash = {}
428
+ hash[:type] = :instance
429
+ hash[:explain] = "creates #many_at method on Array. Same as #at but allows a list of arguments"
430
+ hash[:code] = <<'CODE'
431
+ Array.class_eval do
432
+ def BO_METH_NAME(*list)
433
+ rtn = []
434
+ list = list.first if list.first.kind_of? Array # must delete right-to_left
435
+ list.each do |elm|
436
+ if elm.kind_of? Range
437
+ rtn.concat elm.to_a
438
+ elsif elm.kind_of? Array
439
+ rtn.concat elm
440
+ else
441
+ rtn.push elm
442
+ end
443
+ end
444
+ return rtn
445
+ end
446
+ end
447
+ CODE
448
+ hash[:rm] = <<'CODE'
449
+ Array.class_eval do
450
+ undef BO_METH_NAME
451
+ end
452
+ CODE
453
+ @@better_object_valid_methods[:many_at] = hash
454
+
455
+
456
+ #
457
+ # Generator tripleize
458
+ #
459
+ hash = {}
460
+ hash[:type] = :class
461
+ hash[:explain] = "defines === to mean: left.include? right or your block result"
462
+ hash[:code] = <<'CODE'
463
+ def self.BO_METH_NAME(&proc)
464
+ if proc.nil?
465
+ def === (elm)
466
+ self.include? elm
467
+ end
468
+ else
469
+ @proc_tripleize = proc
470
+ def === (elm)
471
+ self.class.instance_variable_get(:@proc_tripleize).call(self,elm)
472
+ end
473
+ end
474
+ end
475
+ CODE
476
+ @@better_object_valid_methods[:tripleize] = hash
477
+
478
+
479
+ #
480
+ # Generator include_many?
481
+ #
482
+ hash = {}
483
+ hash[:type] = :instance
484
+ hash[:explain] = "calls include? to check for multiple items"
485
+ hash[:code] = <<'CODE'
486
+ def BO_METH_NAME(*items)
487
+ items = items.first if items.first.kind_of? Array
488
+ items.each do |item|
489
+ return false unless self.include? item
490
+ end
491
+ true
492
+ end
493
+ CODE
494
+ @@better_object_valid_methods[:include_many?] = hash
495
+
496
+
384
497
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: betterobject
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Colvin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-01 00:00:00.000000000 Z
11
+ date: 2018-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,11 +71,11 @@ description: |-
71
71
  In order to prevent name pollution, you have the ability to manage the generators to pick alternate names if you prefer.
72
72
  After scouring the RubyGems site, some of the better Class upgrades are included here as well as some of my own.
73
73
  The gem creates the backbone upon which future upgrades should be forthcoming.
74
- As a teaser, some of the generators presently include: obj.local_methods, obj.inherited_methods, obj.in?, COBJ.comes_from?, COBJ.derives_from?, COBJ.define_presence_of, obj.find_def. This first release installs 14 generators.
75
- Calling `Object.better_install_all` will install all of the generators.
76
- You can also generate a subset by calling `Object.better_install :generator_name` or `Object.better_install array_of_gen_names`.
77
- The generator names are also the method names which can be renamed by calling `Object.better_rename(old_name, new_name)`;
78
- this must be done before you generate the method.
74
+ As a teaser, some of the generators presently include: obj.local_methods, obj.inherited_methods, obj.replaced_methods, obj.in?, COBJ.comes_from?, COBJ.derives_from?, obj.find_def.
75
+ There are currently 20 generators and counting.
76
+ Calling Object.better_install_all will install all of the generators.
77
+ You can also generate a subset by calling Object.better_install(:generator_name).
78
+ The generator names are also the method names which can be renamed by calling Object.better_rename(old_name, new_name);
79
79
  email:
80
80
  - bryan@bdlsys.com
81
81
  executables: []