rubyment 0.7.25733511 → 0.7.25752196

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubyment.rb +466 -23
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b710e93d0b0389965a9b49b720148c6e365463a
4
- data.tar.gz: b212104266d18535aaf66e800ea3360017876310
3
+ metadata.gz: 0ff1f7d16c03cf284f9c40a12b9f9fb4a9461703
4
+ data.tar.gz: 716243cf167c62a24a6b27d637a82d54459b1cc6
5
5
  SHA512:
6
- metadata.gz: 6509d00070397420335d4e9e36e99b8a90dc66d9db658e658d8ad947991a47df6843759114fe122ec17f33e7cc20639942640d76475f75a0cfba9f1705aba9a4
7
- data.tar.gz: 9c7ebeeb1255a241d461c200b51227f9d158cc777a6eede7595d3f0e03b1047374381a4cda9f44f3b4591da611ad3d05e1218ec6ca97feabc69f56ebd0b3a177
6
+ metadata.gz: 5220772a0d31f7acffe2ec685f3538baef96dd56f0dfad3282e29133dbeb5ef157a993e26c6e6313b21cbd4ae94ba5f275bd058c35f43c535245eb576323bf20
7
+ data.tar.gz: 789476d027b047058543b76143e71f149f3544b487a14f6ecc54f3d3c6f6903d8e9296aaaca75aea37cf1126f19554900d343fa2020041b1d65578c59a867deb
@@ -142,7 +142,20 @@ end # of NewModule
142
142
  end
143
143
 
144
144
 
145
- end
145
+ =begin
146
+ if you use vim or a similar editor, you can use such a function as:
147
+ :read !./rubyment.rb invoke_double puts code_rubyment_debug_puts
148
+ =end
149
+ def code_rubyment_debug_puts
150
+ code =<<-ENDHEREDOC
151
+ debug && (stderr.puts "=\#{}")
152
+ ENDHEREDOC
153
+ [code]
154
+
155
+ end
156
+
157
+
158
+ end # of RubymentRubyCodeGenerationModule
146
159
 
147
160
 
148
161
  =begin
@@ -262,7 +275,50 @@ module RubymentStringsModule
262
275
  end
263
276
 
264
277
 
265
- end
278
+ =begin
279
+ Function that can generate distribution or combinations of
280
+ an array of strings.
281
+
282
+ Closed for extension
283
+
284
+ examples:
285
+ strings__product [ "model 1", "model 2"], [":"], ["variant with sound system", "variant no sound system"], [" "], ["and stabilizer", "and without stabilizer"]
286
+ # => ["model 1:variant with sound system and stabilizer",
287
+ # "model 1:variant with sound system and without stabilizer",
288
+ # ...
289
+ # "model 2:variant no sound system and stabilizer",
290
+ # "model 2:variant no sound system and without stabilizer"]
291
+
292
+
293
+ =end
294
+ def strings__product *strings
295
+ arrays = arrays__product *strings
296
+ invoke__basic_sender_array [ arrays, :map ], &:join
297
+ end
298
+
299
+
300
+ =begin
301
+ Function that can generate a match of
302
+ an array of strings.
303
+
304
+ Closed for extension
305
+
306
+ examples:
307
+
308
+ # compare the results below, with calling strings__product [ "model 1", "model 2"], [":"], ["variant with sound system", "variant no sound system"], [" "], ["and stabilizer", "and without stabilizer"]
309
+
310
+ strings__zip [ "model 1", "model 2"], [":"], ["variant with sound system", "variant no sound system"], [" "], ["and stabilizer", "and without stabilizer"]
311
+ # => ["model 1:variant with sound system and stabilizer",
312
+ # "model 2variant no sound systemand without stabilizer"]
313
+
314
+ =end
315
+ def strings__zip *strings
316
+ arrays = arrays__zip *strings
317
+ invoke__basic_sender_array [ arrays, :map ], &:join
318
+ end
319
+
320
+
321
+ end # of RubymentStringsModule
266
322
 
267
323
 
268
324
  =begin
@@ -333,6 +389,88 @@ module RubymentArraysModule
333
389
  end
334
390
 
335
391
 
392
+ =begin
393
+ returns an array with the indexes of ocurrences
394
+ of element in each array of arrays.
395
+
396
+ examples:
397
+ array__index [ [ :a, :b, :c], [ nil, :b ], [:a] ], :a
398
+ # => [0, nil, 0]
399
+
400
+ r.array__index [ [ :a, :b, :c], [ nil, :b ], [:a] ], :b
401
+ # => [1, 1, nil]
402
+
403
+
404
+ =end
405
+ def array__index arrays, element
406
+
407
+ pattern_exec__mapping_an_object [
408
+ arrays,
409
+ "map",
410
+ "index",
411
+ element,
412
+ ]
413
+
414
+ end
415
+
416
+
417
+ =begin
418
+
419
+ returns an array with the elements at the given
420
+ position in each array of arrays.
421
+
422
+ it's equivalent of calling arrays.transpose[position],
423
+ however, it saves the transposed memory.
424
+
425
+ examples:
426
+ array__at [ [ :a, :b, :c], [ nil, :b ], [:a] ], 0
427
+ # => [:a, nil, :a]
428
+
429
+ array__at [ [ :a, :b, :c], [ nil, :b ], [:a] ], 2
430
+ # => [:c, nil, nil]
431
+
432
+ # slices can be given in an array:
433
+ array__at [ [ :a, :b, :c], [ nil, :b ], [:a] ], [1..2]
434
+ # => [[:b, :c], [:b], []]
435
+
436
+ =end
437
+ def array__at arrays, position
438
+
439
+ pattern_exec__mapping_an_object [
440
+ arrays,
441
+ "map",
442
+ "slice",
443
+ position,
444
+ ]
445
+
446
+ end
447
+
448
+
449
+ =begin
450
+ Just a convenience function for achieving the
451
+ same as first_array.product other_arrays
452
+
453
+ Closed for extension
454
+
455
+ examples:
456
+
457
+ arrays__product [ "model 1", "model 2"], [":"], ["variant with sound system", "variant no system"], ["and stabilizer", "and without stabilizer"]
458
+ # => [["model 1", ":", "variant with sound system", "and stabilizer"],
459
+ # ["model 1", ":", "variant with sound system", "and without stabilizer"],
460
+ # ...
461
+ # ["model 2", ":", "variant no system", "and stabilizer"],
462
+ # ["model 2", ":", "variant no system", "and without stabilizer"]]
463
+
464
+
465
+
466
+
467
+ =end
468
+ def arrays__product *args
469
+ first, *others = args
470
+ first.product *others
471
+ end
472
+
473
+
336
474
  end # of RubymentArraysModule
337
475
 
338
476
 
@@ -435,7 +573,188 @@ module RubymentInternalModule
435
573
  include InternalRubymentModule
436
574
 
437
575
 
438
- end # of InternalRubymentModule
576
+ =begin
577
+ merges the current @memory with m
578
+ (so @memory will contain the resulting merged
579
+ hash).
580
+ The merge is shallow, ie, only top-level
581
+ keys are merged.
582
+ If the same key is present in both objects,
583
+ the one in m will prevail.
584
+ =end
585
+ def rubyment_memory__merge_shallow m
586
+ @memory.merge! m
587
+ end
588
+
589
+
590
+ =begin
591
+ merges the m with the current @memory
592
+ (so @memory will contain the resulting merged
593
+ hash).
594
+ The merge is shallow, ie, only top-level
595
+ keys are merged.
596
+ If the same key is present in both objects,
597
+ the one in @memory will prevail. Note that this
598
+ is the inverted behaviour as of
599
+ #rubyment_memory__merge_shallow
600
+ =end
601
+ def rubyment_memory__merge_shallow_on m
602
+ rubyment_memory__set m.merge @memory
603
+ end
604
+
605
+
606
+ =begin
607
+ returns a m, or copy of @memory if not given,
608
+ without the keys listed in closure_keys, or m[:closure_keys],
609
+ if not given (still, m["closure_keys"] if m[:closure_keys]
610
+ is empty).
611
+ =end
612
+ def rubyment_memory__without_closure_keys m=nil, closure_keys=nil
613
+ m = m.nne @memory.dup
614
+ closure_keys = closure_keys.nne(
615
+ m[:closure_keys]
616
+ ).nne(
617
+ m["closure_keys"]
618
+ )
619
+ closure_keys.each {|closure_key|
620
+ m.delete closure_key
621
+ }
622
+ m
623
+ end
624
+
625
+
626
+ =begin
627
+ dumps the contents of m, or @memory if not given,
628
+ into filepath, or m[:memory_json_file_default]
629
+ if not given (still m["memory_json_file_default"]
630
+ if nil).
631
+ =end
632
+ def rubyment_memory__to_json_file filepath=nil, m=nil
633
+ m = m.nne @memory
634
+ filepath = filepath.nne(
635
+ m[:memory_json_file_default]
636
+ ).nne(
637
+ m["memory_json_file_default"]
638
+ )
639
+ file__json [
640
+ filepath,
641
+ m,
642
+ ]
643
+ end
644
+
645
+
646
+ =begin
647
+ ensure the top level keys of m, @memory if not given,
648
+ are all symbols
649
+ returns a copy of m, @memory if not given, where
650
+ all the keys are ensured to be symbols.
651
+
652
+ When a ruby hash is stored as a JSON file, the symbol
653
+ keys will be restored as strings. When that JSON file
654
+ is reloaded, those strings will be loaded as strings.
655
+ Therefore the resulting hash is not the same as the
656
+ original. There may be other issues with keys having
657
+ other types than strings and symbols, and deep keys
658
+ can also be lost with that transformation. This function
659
+ just takes cares of the issues created by the Rubyment
660
+ memory implementation itself.
661
+
662
+ other special cases:
663
+ m[:closure_keys] contents will be converted into symbols
664
+
665
+ =end
666
+ def rubyment_memory__symbol_keys_shallow m = nil
667
+ update_memory = m.nne.negate_me
668
+ m = m.nne @memory
669
+ m = m.map { |k, v| [k.to_sym, v] }.to_h
670
+ m[:closure_keys].map!(&:to_sym)
671
+ update_memory && (@memory = m) || m
672
+ end
673
+
674
+
675
+ =begin
676
+ merge
677
+ @memory with the hash loaded from filepath, or
678
+ @memory[:memory_json_file_default] if not given
679
+ (still @memory["memory_json_file_default"] if nil).
680
+
681
+ The merge is shallow, ie, only top-level
682
+ keys are merged.
683
+ If the same key is present in both objects,
684
+ the one in m will prevail.
685
+ =end
686
+ def rubyment_memory__merge_shallow_with_json_file filepath=nil, debug=nil
687
+ filepath = filepath.nne(
688
+ @memory[:memory_json_file_default]
689
+ ).nne(
690
+ @memory["memory_json_file_default"]
691
+ )
692
+ m = load__file_json_quiet [
693
+ filepath,
694
+ debug,
695
+ ]
696
+ rubyment_memory__merge_shallow(
697
+ rubyment_memory__without_closure_keys(
698
+ rubyment_memory__symbol_keys_shallow m
699
+ )
700
+ )
701
+ end
702
+
703
+
704
+ =begin
705
+ merge
706
+ @memory with the hash loaded from filepath, or
707
+ @memory[:memory_json_file_default] if not given
708
+ (still @memory["memory_json_file_default"] if nil).
709
+
710
+ The merge is shallow, ie, only top-level
711
+ keys are merged.
712
+ If the same key is present in both objects,
713
+ the one in @memory will prevail. Note that
714
+ this is the inverse behaviour compared to
715
+ #rubyment_memory__merge_shallow_with_json_file
716
+ =end
717
+ def rubyment_memory__merge_shallow_on_load_json_file filepath=nil, debug=nil
718
+ filepath = filepath.nne(
719
+ @memory[:memory_json_file_default]
720
+ ).nne(
721
+ @memory["memory_json_file_default"]
722
+ )
723
+ m = load__file_json_quiet [
724
+ filepath,
725
+ debug,
726
+ ]
727
+ rubyment_memory__merge_shallow_on(
728
+ rubyment_memory__without_closure_keys(
729
+ rubyment_memory__symbol_keys_shallow m
730
+ )
731
+ )
732
+ end
733
+
734
+
735
+ =begin
736
+ returns a string having help for the idea of persistence
737
+ achieved with many of the functions of this module:
738
+ =end
739
+ def help__concept__rubyment_memory_persistence
740
+ string = [
741
+ "# saves the current memory to the default memory_json_file_default ('memory.rubyment.json' by default, in the current dir): ",
742
+ "rubyment_memory__to_json_file",
743
+ "",
744
+ "# if you add a new key to that file, you can load it to memory with",
745
+ "rubyment_memory__merge_shallow_on_load_json_file",
746
+ "# in the above case, keys in the current memory will prevail, when they equal.",
747
+ "",
748
+ "# this function will try to restore the memory, as much as possible (not everything is serializable), to the state of when it was saved: ",
749
+ "rubyment_memory__merge_shallow_with_json_file",
750
+ "# in the above case, keys in the json file will prevail, when they equal.",
751
+ "",
752
+ ]
753
+ [ string.join("\n") ]
754
+ end
755
+
756
+
757
+ end # of RubymentInternalModule
439
758
 
440
759
 
441
760
  =begin
@@ -521,6 +840,48 @@ module RubymentModifierForClassObjectModule
521
840
  end
522
841
 
523
842
 
843
+ =begin
844
+ # begin_documentation
845
+
846
+ This module offers functions for invocation of Rubyment
847
+ (and Ruby) code in general.
848
+
849
+ # end_documentation
850
+ =end
851
+ module RubymentInvocationModule
852
+
853
+
854
+ =begin
855
+ Takes an array as parameter and invokes a
856
+ method (second element of that array) of an object
857
+ (first element of that array) giving the remaining
858
+ array as argument list, and optionally giving
859
+ a block as extra parameter.
860
+
861
+ Closed for extensions
862
+
863
+ examples:
864
+
865
+ invoke__basic_sender_array [ 9, "next" ]
866
+ # => 10
867
+
868
+ invoke__basic_sender_array [ self, "p", "args", "to", "p" ]
869
+ # => ["args", "to", "p"]
870
+
871
+ invoke__basic_sender_array [ [["S", "2"], ["<", "3" ]], :map ], &:join
872
+ # => ["S2", "<3"]
873
+
874
+
875
+ =end
876
+ def invoke__basic_sender_array args, &block
877
+ object, method_name, *args_to_method = args
878
+ object.send method_name, *args_to_method, &block
879
+ end
880
+
881
+
882
+ end # of RubymentInvocationModule
883
+
884
+
524
885
  =begin
525
886
  # begin_documentation
526
887
  This module receives functions that are being worked on.
@@ -775,32 +1136,18 @@ module RubymentExperimentModule
775
1136
 
776
1137
  =begin
777
1138
  write the second argument of the array args into the filepath in the first argument
1139
+ read it back with #load__file_json_quiet
778
1140
  =end
779
1141
  def file__json args=[]
780
1142
  require 'json'
781
1143
  file_path,
782
1144
  enum,
783
1145
  reserved = args
1146
+ FileUtils.mkdir_p File.dirname file_path
784
1147
  File.write file_path, JSON.pretty_generate({ :root.to_s => enum })
785
1148
  end
786
1149
 
787
1150
 
788
- =begin
789
- inverse of #file__json
790
- =end
791
- def load__file_json args=[]
792
- require 'json'
793
- file_path,
794
- reserved = args
795
- file_contents = File.read file_path
796
- puts "file_contents size=#{file_contents.size}"
797
- loaded = JSON.parse file_contents
798
- puts "loaded size=#{loaded.size}"
799
- puts "loaded=#{loaded.inspect}"
800
- loaded[:root.to_s]
801
- end
802
-
803
-
804
1151
  =begin
805
1152
  c = :c
806
1153
  experiment__input_select [[:a, :b, :c], c ]
@@ -2317,7 +2664,66 @@ trying to get the interface compatible with
2317
2664
  end
2318
2665
 
2319
2666
 
2320
- end
2667
+ =begin
2668
+ inverse of #file__json
2669
+ =end
2670
+ def load__file_json_quiet args=[]
2671
+ require 'json'
2672
+ file_path,
2673
+ debug,
2674
+ reserved = args
2675
+ debug = debug.nne
2676
+ stderr = @memory[:stderr]
2677
+ debug && (stderr.puts "{#{__method__} starting")
2678
+ debug && (stderr.puts "caller=#{caller_label}")
2679
+ debug && (stderr.puts "args=#{args.inspect}")
2680
+ file_contents = File.read file_path
2681
+ debug && (stderr.puts "file_contents size=#{file_contents.size}")
2682
+ loaded = JSON.parse file_contents
2683
+ debug && (stderr.puts "loaded size=#{loaded.size}")
2684
+ debug && (stderr.puts "loaded=#{loaded.inspect}")
2685
+ rv = loaded[:root.to_s]
2686
+ # if raises exception before it will be unbalanced :
2687
+ debug && (stderr.puts "#{__method__} will return #{rv.inspect}")
2688
+ debug && (stderr.puts "#{__method__} returning}")
2689
+ rv
2690
+ end
2691
+
2692
+
2693
+ =begin
2694
+
2695
+ To generate and execute a code like this:
2696
+
2697
+ arrays.map { |a|
2698
+ a.index element
2699
+ }
2700
+
2701
+ just give as a code_pattern:
2702
+
2703
+ [
2704
+ arrays,
2705
+ "map",
2706
+ "index",
2707
+ element,
2708
+ ]
2709
+
2710
+ =end
2711
+ def pattern_exec__mapping_an_object code_pattern
2712
+
2713
+ iterating_object, # arrays
2714
+ yielding_method_name, # "map"
2715
+ mapping_method_name, # "index"
2716
+ args_to_mapping_method, # element
2717
+ reserved = code_pattern
2718
+
2719
+ iterating_object.send(yielding_method_name) { |a|
2720
+ a.send mapping_method_name, *args_to_mapping_method
2721
+ }
2722
+
2723
+ end
2724
+
2725
+
2726
+ end # of RubymentExperimentModule
2321
2727
 
2322
2728
 
2323
2729
  =begin
@@ -2982,7 +3388,36 @@ module RubymentDeprecatedModule
2982
3388
  end
2983
3389
 
2984
3390
 
2985
- end
3391
+ =begin
3392
+ inverse of #file__json
3393
+ note that, contrary to the common convention,
3394
+ the debug output is on for this function
3395
+ (due to a mistake, and to respect API-backwards-compatibility)
3396
+ =end
3397
+ def load__file_json args=[]
3398
+ require 'json'
3399
+ file_path,
3400
+ quiet,
3401
+ reserved = args
3402
+ debug = quiet.nne.negate_me
3403
+ stderr = @memory[:stderr]
3404
+ debug && (stderr.puts "{#{__method__} starting")
3405
+ debug && (stderr.puts "caller=#{caller_label}")
3406
+ debug && (stderr.puts "args=#{args.inspect}")
3407
+ file_contents = File.read file_path
3408
+ debug && (stderr.puts "file_contents size=#{file_contents.size}")
3409
+ loaded = JSON.parse file_contents
3410
+ debug && (stderr.puts "loaded size=#{loaded.size}")
3411
+ debug && (stderr.puts "loaded=#{loaded.inspect}")
3412
+ rv = loaded[:root.to_s]
3413
+ # if raises exception before it will be unbalanced :
3414
+ debug && (stderr.puts "#{__method__} will return #{rv.inspect}")
3415
+ debug && (stderr.puts "#{__method__} returning}")
3416
+ rv
3417
+ end
3418
+
3419
+
3420
+ end # of RubymentDeprecatedModule
2986
3421
 
2987
3422
 
2988
3423
  =begin
@@ -3477,6 +3912,7 @@ module RubymentModule
3477
3912
  include RubymentStringsModule
3478
3913
  include RubymentArraysModule
3479
3914
  include RubymentInternalModule
3915
+ include RubymentInvocationModule
3480
3916
  include RubymentExperimentModule
3481
3917
  include RubymentMaintainedModule
3482
3918
  include RubymentDeprecatedModule
@@ -3541,11 +3977,18 @@ module RubymentModule
3541
3977
  :running_dir => Dir.pwd,
3542
3978
  :home_dir => Dir.home,
3543
3979
  :system_user => ENV['USER'] || ENV['USERNAME'],
3544
- :system_user_is_super => ENV['USER'] == "root", # changed plan: platform indenpend.
3980
+ :system_user_is_super => ENV['USER'] == "root",
3545
3981
  :static_separator_key => "strings_having_this" + "_string_not_guaranteed_to_work",
3546
3982
  :static_end_key => "strings_havinng_this_string" + "_also_not_guaranteed_to_work",
3547
3983
  :static_separator_key_per_execution => "strings_having_this" + "_string_not_guaranteed_to_work" + (Proc.new {}).to_s + Time.now.to_s,
3548
- :threads => [Thread.current]
3984
+ :threads => [Thread.current],
3985
+ :memory_json_file_default => "memory.rubyment.json",
3986
+ :closure_keys => [
3987
+ :stdin,
3988
+ :stderr,
3989
+ :stdout,
3990
+ :threads,
3991
+ ],
3549
3992
  }
3550
3993
  @memory.update memory.to_h
3551
3994
  invoke @memory[:invoke].to_a
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyment
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.25733511
4
+ version: 0.7.25752196
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribamar Santarosa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-05 00:00:00.000000000 Z
11
+ date: 2018-12-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: a gem for keeping Rubyment, a set of ruby helpers
14
14
  email: ribamar@gmail.com