betterobject 0.1.0 → 1.0.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 +4 -4
- data/README.md +85 -30
- data/lib/betterobject/version.rb +1 -1
- data/lib/betterobject.rb +1 -1
- data/lib/generators.rb +70 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f0fe8e69cf1f96c5d5150fca67a2b1ac82ebc54
|
4
|
+
data.tar.gz: dbed0f676c842b23bbcc562410a4931090ba82d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e7baa787819509439a2fec0c2cf44f7a1c78aed1aed3b26717454a05ca484f4a01c5494c7bc795ca4f05f19335421f06a5b301acaaf9f7098bf15471a71d613
|
7
|
+
data.tar.gz: 82c0a7b11eb65ce5f5423e4eb5e5310d10d186c0748aa63937a5b1f9e6944bf92e22a21cfdeca35229901d0c4edf6a91d7be02171524a1e95765c61c6791bcae
|
data/README.md
CHANGED
@@ -77,7 +77,7 @@ Object.better_install [:in_the_thing_to_the_right?, :methods_by_me] # or better
|
|
77
77
|
|
78
78
|
You can control which methods names get generated. Now let's dive into the other class methods of `betterobject`.
|
79
79
|
|
80
|
-
|
80
|
+
### Object class methods created from the `betterobject` gem
|
81
81
|
* `Object.better_installed_instance_methods`
|
82
82
|
* `Object.better_installed_class_methods`
|
83
83
|
* `Object.better_installed`
|
@@ -97,28 +97,28 @@ You can control which methods names get generated. Now let's dive into the other
|
|
97
97
|
Note that there are no instance methods created by this gem until you install a generator.
|
98
98
|
Also, each and every method must be called from Object.
|
99
99
|
|
100
|
-
|
100
|
+
### Object.better_install_all
|
101
101
|
|
102
102
|
This will install every defined generator. In the event that a generator is renamed, the new name will be generated, while the old name
|
103
103
|
will no longer exist. This is similar to renaming a file. Note that the generator essence never goes away, as it will exist under some new name.
|
104
104
|
It is simply tucked away until needed. Also, generators marked as skipped will not be installed.
|
105
105
|
|
106
|
-
|
106
|
+
### Object.better_skip(gen or [gen1, gen2, ...] or gen1, gen2, ...)
|
107
107
|
|
108
108
|
This is to prevent a generator from installing by adding a skip attribute.
|
109
109
|
Additionally, skipped generators cannot be uninstalled until the skip flag is removed.
|
110
110
|
If a generator is first installed then given this skip attribute, it cannot be uninstalled.
|
111
111
|
This acts as a protection.
|
112
112
|
|
113
|
-
|
113
|
+
### Object.better_unskip(gen or [gen1, gen2, ...] or gen1, gen2, ...)
|
114
114
|
|
115
115
|
This removes the skip attribute enabling the generator to be installed or uninstalled.
|
116
116
|
|
117
|
-
|
117
|
+
### Object.better_skipped
|
118
118
|
|
119
119
|
This returns an array of skipped generators.
|
120
120
|
|
121
|
-
|
121
|
+
### Object.better_install(gen or [gen1, gen2, ...] or gen1, gen2, ...)
|
122
122
|
|
123
123
|
This will install a specific generator.
|
124
124
|
If you rename a generator, the old name no longer exists, so you must use the new name. See below:
|
@@ -133,13 +133,13 @@ str = "The cat in the hat is back"
|
|
133
133
|
'cat'.is_in? str # true
|
134
134
|
```
|
135
135
|
|
136
|
-
|
136
|
+
### Object.better_uninstall_all
|
137
137
|
|
138
138
|
This will remove all the generators from Object. Note that some generators spawn additional generators such as `:define_presence_of`.
|
139
139
|
If this generator is ever run, `Object.better_uninstall_all` will not remove these second generation methods.
|
140
140
|
Note that generators marked with the skip flag will not be uninstalled.
|
141
141
|
|
142
|
-
|
142
|
+
### Object.better_uninstall(gen or [gen1, gen2, ...] or gen1, gen2, ...)
|
143
143
|
|
144
144
|
This will uninstall a specific generator or a set of generators.
|
145
145
|
This method returns false if the generator is skipped, or already uninstalled; otherwise true is returned.
|
@@ -156,23 +156,23 @@ Object.better_install :methods_by_me
|
|
156
156
|
"a string".methods_by_me # [list of local methods]
|
157
157
|
```
|
158
158
|
|
159
|
-
|
159
|
+
### Object.better_list(include_skip=false)
|
160
160
|
|
161
161
|
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`.
|
162
162
|
|
163
|
-
|
163
|
+
### Object.better_installed
|
164
164
|
|
165
165
|
This returns a list of installed generators. This does not list what the generators generate.
|
166
166
|
|
167
|
-
|
167
|
+
### Object.better_installed_instance_methods
|
168
168
|
|
169
169
|
This gives us a list of generators that created instance methods. This does not list what the generators generate.
|
170
170
|
|
171
|
-
|
171
|
+
### Object.better_installed_class_methods
|
172
172
|
|
173
173
|
This give us a list of generators that created class methods. This does not list what the generators generate.
|
174
174
|
|
175
|
-
|
175
|
+
### Object.better_rename(old_name, new_name)
|
176
176
|
|
177
177
|
This allows us to rename a generator which in turn (most likely) renames what gets generated.
|
178
178
|
This is an important feature as placing methods at the Object level has the danger of bumping into potential name conflicts.
|
@@ -191,7 +191,7 @@ sorted_hash.bjc_tag? # true
|
|
191
191
|
:zoo.bjc_in? hash # true
|
192
192
|
```
|
193
193
|
|
194
|
-
|
194
|
+
### Object.better_explain(:generator_name, pts=true)
|
195
195
|
|
196
196
|
Calling this method without a generator name gives us a general list of the generators.
|
197
197
|
Further details are revealed when a generator name is provided. The last default parameter puts the string to STOUT when true; otherwise,
|
@@ -202,7 +202,7 @@ See example below:
|
|
202
202
|
Object.better_explain(:in?) # obj.in?(enum) # ex: 3.in? [3,4,5] == true
|
203
203
|
```
|
204
204
|
|
205
|
-
|
205
|
+
### Object.better_source_code(:generator_name, return_string = false, inner_only = false, type=:code)
|
206
206
|
|
207
207
|
This will revel the source code for the generator. You can then copy and paste this in `irb` and experiment with it. See below:
|
208
208
|
|
@@ -220,7 +220,7 @@ The parameter `return_string` will return a string instance when set true.
|
|
220
220
|
The parameter `inner_only` when set true will remove the `class Object` and enclosing `end` statements.
|
221
221
|
The final parameter is set to either `:code` or `:rm` depending on if you need the creation code or the deletion code.
|
222
222
|
|
223
|
-
|
223
|
+
### Object.better_define(meth_name, code, type, doc="*Undocumented*", rm_code=nil)
|
224
224
|
|
225
225
|
This installs a user-defined generator. As an example we will create a `whotheheckami` generator as follows.
|
226
226
|
```ruby
|
@@ -288,7 +288,7 @@ This method was used as a tool to create some of the generators.
|
|
288
288
|
Note that if you decouple the method name from the generator name, or target a foreign object,
|
289
289
|
you must provide code to remove your methods.
|
290
290
|
|
291
|
-
|
291
|
+
### Generator list
|
292
292
|
* `:local_methods`
|
293
293
|
* `:inherited_methods`
|
294
294
|
* `:replaced_methods`
|
@@ -305,7 +305,7 @@ you must provide code to remove your methods.
|
|
305
305
|
* `:functionize`
|
306
306
|
|
307
307
|
|
308
|
-
|
308
|
+
### Generator :local_methods(include_singleton_methods=true)
|
309
309
|
|
310
310
|
This is an instance generator on Object. It is used on all objects that derive from Object (which is pretty much everything).
|
311
311
|
The purpose is to view methods generated by the class of the instance and not the methods inherited by that class.
|
@@ -329,7 +329,7 @@ fred.local_methods(false) #[:a, :b]
|
|
329
329
|
fred.methods # [:a, :b, :dup, :clone, etc ...]
|
330
330
|
```
|
331
331
|
|
332
|
-
|
332
|
+
### Generator :inherited_methods
|
333
333
|
|
334
334
|
This is an instance generator on Object. It is similar to `#methods`, except all locally defined methods are excluded.
|
335
335
|
The combination of `#local_methods` and `#inherited_methods` is the same as `#methods`. See below:
|
@@ -348,7 +348,7 @@ fred = Fred.new
|
|
348
348
|
fred.methods.sort == [fred.local_methods + fred.inherited_methods].sort # true
|
349
349
|
```
|
350
350
|
|
351
|
-
|
351
|
+
### Generator :replaced_methods
|
352
352
|
|
353
353
|
This is an instance generator on Object. This lets us discover which methods have been replaced during inheritance, or replaced due to a singleton method using the same name as an instance method. See the example below:
|
354
354
|
|
@@ -367,7 +367,7 @@ end
|
|
367
367
|
fred.replaced_methods # [:length, :to_s]
|
368
368
|
```
|
369
369
|
|
370
|
-
|
370
|
+
### Generators :derives_from? and :comes_from?
|
371
371
|
|
372
372
|
These methods are both class methods on Object. The purpose is to test hierarchy of derived objects.
|
373
373
|
The two class methods once generated are identical except `comes_from?` is more inclusive.
|
@@ -391,7 +391,7 @@ Fred.comes_from? String # true
|
|
391
391
|
Fred.comes_from? Object # true
|
392
392
|
```
|
393
393
|
|
394
|
-
|
394
|
+
### Generators :define_presence_of(:target_method_name, target_default_value = true)
|
395
395
|
|
396
396
|
This generator generates a generator!
|
397
397
|
When installed, a class method is created on Object called `#define_presence_of`.
|
@@ -426,7 +426,7 @@ The generated generator defines an instance method on Object as well as the desc
|
|
426
426
|
Each method returns an opposite result.
|
427
427
|
There are several base class gems (not mine) that force a certain naming convention; this one let's you pick the name!
|
428
428
|
|
429
|
-
|
429
|
+
### Generators :in?
|
430
430
|
|
431
431
|
This generator allows us to swap the order of the standard `#include?` method with an improved comprehensive ordering.
|
432
432
|
I saw this in another gem called ("object-in" by Tim Rogers), so I give him credit for the idea. It is included here because
|
@@ -443,7 +443,7 @@ Object.better_install :in?
|
|
443
443
|
'i'.in? "team" # false
|
444
444
|
```
|
445
445
|
|
446
|
-
|
446
|
+
### Generators :pluralize(meth_name, alt=nil)
|
447
447
|
|
448
448
|
This generator installs a generator that mimics another method using a pluralized name.
|
449
449
|
One of my pet peves is poorly named methods that are grammatically incorrect.
|
@@ -464,17 +464,17 @@ Object.pluralize :respond_to? # :responds_to?
|
|
464
464
|
17.responds_to? :to_s # true
|
465
465
|
```
|
466
466
|
|
467
|
-
|
467
|
+
### Generators :parent
|
468
468
|
|
469
469
|
This generator finds the first non-module ancestor of a class.
|
470
470
|
The class Object's parent is always Object.
|
471
471
|
This is a class method installed on Object.
|
472
472
|
|
473
|
-
|
473
|
+
### Generators :to_literal
|
474
474
|
|
475
475
|
This is an alias for `#inspect`. In the event inspect fails, `#to_s` is then called.
|
476
476
|
|
477
|
-
|
477
|
+
### Generators :tag
|
478
478
|
|
479
479
|
This generator creates a family of instance methods. If unrenamed, you will get: `#tag`, `#tag!`, `#untag`, `#tag?` and `#tag=`.
|
480
480
|
If you rename this generator to :fred, you will get these: `#fred`, `#fred!`, `#unfred`, `#fred?`, `#fred=`.
|
@@ -511,7 +511,36 @@ See the example below:
|
|
511
511
|
var1.tag # false
|
512
512
|
```
|
513
513
|
|
514
|
-
|
514
|
+
### Generators :create_tag
|
515
|
+
This generator is a class method on Object that installs a named tag generating system.
|
516
|
+
Once a tag is generated, it will work just like tag in the previous section.
|
517
|
+
This allows an `Object` derived instance to have one or more tags attached.
|
518
|
+
If you only need one tag, then the `:tag` generator is a better choice as it can be uninstalled.
|
519
|
+
Tags created by #create_tag are 2nd level generators and cannot be uninstalled.
|
520
|
+
These two generators are independent so long as you avoid using `:tag` as a source name.
|
521
|
+
See the example below:
|
522
|
+
|
523
|
+
```ruby
|
524
|
+
Object.better_install :create_tag
|
525
|
+
Object.create_tag :tag1
|
526
|
+
Object.create_tag :tag2
|
527
|
+
Object.create_tag :tag3
|
528
|
+
Object.create_tag :flag
|
529
|
+
str = "Tag me!"
|
530
|
+
str.tag1!
|
531
|
+
str.tag2 = "forget me not"
|
532
|
+
str.ta3 = 17
|
533
|
+
str.tag1? # true
|
534
|
+
str.tag2? # true
|
535
|
+
str.flag? # false
|
536
|
+
str.tag2 # "forget me not"
|
537
|
+
str.flag # nil
|
538
|
+
str.unflag
|
539
|
+
str.flag # false
|
540
|
+
end
|
541
|
+
```
|
542
|
+
|
543
|
+
### Generators :sort!
|
515
544
|
|
516
545
|
This method targets only the Hash class and creates the `#sort!` method.
|
517
546
|
Hash has a method `#sort` that returns a sorted array of key value pairs.
|
@@ -527,7 +556,7 @@ hash.sort!
|
|
527
556
|
Hash.keys # [:better, :fruit, :zoo]
|
528
557
|
```
|
529
558
|
|
530
|
-
|
559
|
+
### Generators :find_def
|
531
560
|
|
532
561
|
This method locates the owner of either an instance method or a class method.
|
533
562
|
The method expects a symbol representation of the method.
|
@@ -545,7 +574,7 @@ tst = Fixnum.find_def :new # tst = nil
|
|
545
574
|
tst = "nope".find_def :foo # tst = nil
|
546
575
|
```
|
547
576
|
|
548
|
-
|
577
|
+
### Generators :functionize(meth = :new)
|
549
578
|
|
550
579
|
This method makes your class look like a method that you simply call on the class constant.
|
551
580
|
If you examine the `Rational` class, there is no `#new` constructor at all.
|
@@ -568,6 +597,32 @@ obj1 = Clown(:one, :two) # fast way
|
|
568
597
|
obj2 = Clown.new(:three, :four) # slow way
|
569
598
|
```
|
570
599
|
|
600
|
+
### Generators :push_unique(item)
|
601
|
+
|
602
|
+
This method extends Array with a new method `#push_unique`. This works just like `#push`,
|
603
|
+
but first checks to see if that item already exists; in that case no `#push` occurs.
|
604
|
+
See the example below:
|
605
|
+
|
606
|
+
```ruby
|
607
|
+
Object.better_install :push_unique
|
608
|
+
|
609
|
+
ary = []
|
610
|
+
ary.push "row"
|
611
|
+
ary.push "row"
|
612
|
+
ary.push_unique "row"
|
613
|
+
ary.push_unique "row"
|
614
|
+
ary.push_unique "row"
|
615
|
+
ary.push "row"
|
616
|
+
ary.push_unique "your boat"
|
617
|
+
ary.join " " # "row row row your boat"
|
618
|
+
```
|
619
|
+
|
620
|
+
## Change Log
|
621
|
+
|
622
|
+
### Version 1.0.0
|
623
|
+
1. Added new generator :push_unique
|
624
|
+
2. Added new generator :create_tag
|
625
|
+
|
571
626
|
## Development
|
572
627
|
|
573
628
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/betterobject/version.rb
CHANGED
data/lib/betterobject.rb
CHANGED
@@ -17,7 +17,7 @@ class Object
|
|
17
17
|
raise "unknown generator #{sym.inspect}" unless @@better_object_valid_methods.include? sym
|
18
18
|
end
|
19
19
|
|
20
|
-
def better_list(include_skip=false)
|
20
|
+
def self.better_list(include_skip=false)
|
21
21
|
better_be_object
|
22
22
|
rtn = []
|
23
23
|
# @@better_object_valid_methods.keys.sort
|
data/lib/generators.rb
CHANGED
@@ -17,7 +17,6 @@ class Object
|
|
17
17
|
@@better_installed_methods={}
|
18
18
|
@@better_object_valid_methods={}
|
19
19
|
|
20
|
-
|
21
20
|
#
|
22
21
|
# Generator :find_def
|
23
22
|
#
|
@@ -273,6 +272,23 @@ CODE
|
|
273
272
|
@@better_object_valid_methods[:parent] = hash
|
274
273
|
|
275
274
|
|
275
|
+
#
|
276
|
+
# Generator :functionize
|
277
|
+
#
|
278
|
+
hash = {}
|
279
|
+
hash[:type] = :class
|
280
|
+
hash[:explain] = "creates MyClass() on Kernel level as an alternate constructor or some other method"
|
281
|
+
hash[:code] = <<'CODE'
|
282
|
+
def BO_METH_NAME(meth=:new)
|
283
|
+
raise "can't redefine #{self}" if Kernel.methods.include? self.to_s.to_sym
|
284
|
+
raise "can't redefine #{self}" if self.respond_to? self.to_s.to_sym
|
285
|
+
cmd = "def #{self}(*prms,&block); return #{self}.#{meth.to_s}(*prms,&block);end"
|
286
|
+
Kernel.class_eval cmd
|
287
|
+
end
|
288
|
+
CODE
|
289
|
+
@@better_object_valid_methods[:functionize] = hash
|
290
|
+
|
291
|
+
|
276
292
|
#
|
277
293
|
# Generator :tag
|
278
294
|
#
|
@@ -307,19 +323,62 @@ CODE
|
|
307
323
|
|
308
324
|
|
309
325
|
#
|
310
|
-
# Generator :
|
311
|
-
#
|
326
|
+
# Generator :create_tag
|
327
|
+
#
|
312
328
|
hash = {}
|
313
329
|
hash[:type] = :class
|
314
|
-
hash[:explain] = "creates
|
330
|
+
hash[:explain] = "creates multiple named tags similar to :tag"
|
315
331
|
hash[:code] = <<'CODE'
|
316
|
-
def BO_METH_NAME(
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
332
|
+
def self.BO_METH_NAME(tag_name)
|
333
|
+
class_eval do
|
334
|
+
meth = tag_name.to_s
|
335
|
+
|
336
|
+
define_method meth.to_sym do
|
337
|
+
@boh_BO_METH_NAME ||= {}
|
338
|
+
nil || @boh_BO_METH_NAME[tag_name]
|
339
|
+
end
|
340
|
+
define_method (meth+"!").to_sym do
|
341
|
+
@boh_BO_METH_NAME ||= {}
|
342
|
+
@boh_BO_METH_NAME[tag_name] = true
|
343
|
+
end
|
344
|
+
define_method ("un" + meth).to_sym do
|
345
|
+
@boh_BO_METH_NAME ||= {}
|
346
|
+
@boh_BO_METH_NAME[tag_name] = false
|
347
|
+
end
|
348
|
+
define_method (meth+"?").to_sym do
|
349
|
+
@boh_BO_METH_NAME ||= {}
|
350
|
+
return nil != (@boh_BO_METH_NAME[tag_name] || nil)
|
351
|
+
end
|
352
|
+
define_method (meth+"=").to_sym do |prm|
|
353
|
+
@boh_BO_METH_NAME ||= {}
|
354
|
+
@boh_BO_METH_NAME[tag_name]=prm
|
355
|
+
end
|
356
|
+
end
|
357
|
+
tag_name
|
358
|
+
end
|
322
359
|
CODE
|
323
|
-
@@better_object_valid_methods[:
|
360
|
+
@@better_object_valid_methods[:create_tag] = hash
|
361
|
+
|
362
|
+
|
363
|
+
#
|
364
|
+
# Generator :push_unique
|
365
|
+
#
|
366
|
+
hash = {}
|
367
|
+
hash[:type] = :instance
|
368
|
+
hash[:explain] = "creates #push_unique method on Array"
|
369
|
+
hash[:code] = <<'CODE'
|
370
|
+
Array.class_eval do
|
371
|
+
def BO_METH_NAME(item)
|
372
|
+
push item unless self.include? item
|
373
|
+
self
|
374
|
+
end
|
375
|
+
end
|
376
|
+
CODE
|
377
|
+
hash[:rm] = <<'CODE'
|
378
|
+
Array.class_eval do
|
379
|
+
undef BO_METH_NAME
|
380
|
+
end
|
381
|
+
CODE
|
382
|
+
@@better_object_valid_methods[:push_unique] = hash
|
324
383
|
|
325
384
|
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:
|
4
|
+
version: 1.0.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:
|
11
|
+
date: 2018-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|