bblib 1.0.2 → 2.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.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +502 -8
  3. data/lib/bblib.rb +1 -28
  4. data/lib/bblib/all.rb +5 -0
  5. data/lib/{time → bblib/classes}/cron.rb +0 -0
  6. data/lib/{string → bblib/classes}/fuzzy_matcher.rb +0 -0
  7. data/lib/bblib/cli.rb +3 -0
  8. data/lib/bblib/cli/color.rb +51 -0
  9. data/lib/bblib/cli/exceptions/invalid_argument.rb +5 -0
  10. data/lib/bblib/cli/exceptions/missing_argument.rb +5 -0
  11. data/lib/bblib/cli/exceptions/missing_required_argument.rb +5 -0
  12. data/lib/bblib/cli/exceptions/opts_parser.rb +9 -0
  13. data/lib/bblib/cli/option.rb +116 -0
  14. data/lib/bblib/cli/options/basic_option.rb +19 -0
  15. data/lib/bblib/cli/options/bool.rb +19 -0
  16. data/lib/bblib/cli/options/command.rb +11 -0
  17. data/lib/bblib/cli/options/date.rb +14 -0
  18. data/lib/bblib/cli/options/float.rb +14 -0
  19. data/lib/bblib/cli/options/integer.rb +14 -0
  20. data/lib/bblib/cli/options/json.rb +16 -0
  21. data/lib/bblib/cli/options/regexp.rb +17 -0
  22. data/lib/bblib/cli/options/string.rb +13 -0
  23. data/lib/bblib/cli/options/symbol.rb +13 -0
  24. data/lib/bblib/cli/options/time.rb +14 -0
  25. data/lib/bblib/cli/options/toggle.rb +27 -0
  26. data/lib/bblib/cli/options/untoggle.rb +18 -0
  27. data/lib/bblib/cli/opts_parser.rb +65 -0
  28. data/lib/bblib/core.rb +37 -0
  29. data/lib/{hash → bblib/core/classes}/hash_struct.rb +9 -2
  30. data/lib/bblib/core/classes/splitter.rb +101 -0
  31. data/lib/{time → bblib/core/classes}/task_timer.rb +0 -0
  32. data/lib/{hash → bblib/core/classes}/tree_hash.rb +22 -22
  33. data/lib/bblib/core/exceptions/abstract.rb +3 -0
  34. data/lib/bblib/core/exceptions/exception.rb +5 -0
  35. data/lib/{hash_path → bblib/core/hash_path}/hash_path.rb +0 -2
  36. data/lib/{hash_path → bblib/core/hash_path}/part.rb +0 -0
  37. data/lib/{mixins → bblib/core/mixins}/attrs.rb +17 -7
  38. data/lib/{mixins → bblib/core/mixins}/bbmixins.rb +1 -0
  39. data/lib/{mixins → bblib/core/mixins}/bridge.rb +0 -0
  40. data/lib/bblib/core/mixins/delegator.rb +90 -0
  41. data/lib/{class → bblib/core/mixins}/effortless.rb +0 -0
  42. data/lib/{mixins → bblib/core/mixins}/family_tree.rb +1 -1
  43. data/lib/{mixins → bblib/core/mixins}/hooks.rb +0 -0
  44. data/lib/{mixins → bblib/core/mixins}/logger.rb +0 -0
  45. data/lib/{mixins → bblib/core/mixins}/prototype.rb +0 -0
  46. data/lib/{mixins → bblib/core/mixins}/serializer.rb +0 -0
  47. data/lib/{mixins → bblib/core/mixins}/simple_init.rb +41 -3
  48. data/lib/{mixins → bblib/core/mixins}/type_init.rb +0 -0
  49. data/lib/{array/bbarray.rb → bblib/core/util/array.rb} +0 -0
  50. data/lib/{string → bblib/core/util}/cases.rb +0 -0
  51. data/lib/{file/bbfile.rb → bblib/core/util/file.rb} +31 -7
  52. data/lib/{hash/bbhash.rb → bblib/core/util/hash.rb} +0 -3
  53. data/lib/{logging/bblogging.rb → bblib/core/util/logging.rb} +11 -2
  54. data/lib/{string → bblib/core/util}/matching.rb +0 -0
  55. data/lib/{number/bbnumber.rb → bblib/core/util/number.rb} +0 -0
  56. data/lib/{object/bbobject.rb → bblib/core/util/object.rb} +2 -0
  57. data/lib/{opal/bbopal.rb → bblib/core/util/opal.rb} +0 -0
  58. data/lib/{os/bbos.rb → bblib/core/util/os.rb} +0 -0
  59. data/lib/{string → bblib/core/util}/pluralization.rb +0 -0
  60. data/lib/{string → bblib/core/util}/regexp.rb +0 -0
  61. data/lib/{string → bblib/core/util}/roman.rb +0 -0
  62. data/lib/{string/bbstring.rb → bblib/core/util/string.rb} +18 -24
  63. data/lib/{time/bbtime.rb → bblib/core/util/time.rb} +1 -5
  64. data/lib/bblib/cron.rb +2 -0
  65. data/lib/bblib/fuzzy_matcher.rb +2 -0
  66. data/lib/bblib/html.rb +4 -0
  67. data/lib/{html → bblib/html}/builder.rb +0 -0
  68. data/lib/{html → bblib/html}/tag.rb +0 -0
  69. data/lib/{html → bblib/html}/tag_set.rb +0 -0
  70. data/lib/bblib/system.rb +3 -0
  71. data/lib/bblib/system/command.rb +7 -0
  72. data/lib/bblib/system/system.rb +39 -0
  73. data/lib/bblib/version.rb +1 -1
  74. metadata +71 -44
  75. data/lib/error/abstract.rb +0 -3
  76. data/lib/hash_path/path_hash.rb +0 -84
  77. data/lib/hash_path/proc.rb +0 -93
  78. data/lib/hash_path/processors.rb +0 -239
  79. data/lib/html/bbhtml.rb +0 -3
  80. data/lib/system/bbsystem.rb +0 -42
@@ -0,0 +1,2 @@
1
+ require_relative 'core'
2
+ require_relative 'classes/cron'
@@ -0,0 +1,2 @@
1
+ require_relative 'core'
2
+ require_relative 'classes/fuzzy_matcher'
@@ -0,0 +1,4 @@
1
+ require_relative 'core'
2
+ require_relative 'html/builder'
3
+ require_relative 'html/tag'
4
+ require_relative 'html/tag_set'
File without changes
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ require_relative 'core'
2
+ require_relative 'system/command'
3
+ require_relative 'system/system'
@@ -0,0 +1,7 @@
1
+ module BBLib
2
+ module Command
3
+ def self.quote(arg)
4
+ arg =~ /\s+/ ? "\"#{arg}\"" : arg.to_s
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ module BBLib
2
+ module System
3
+ SPECIAL_PROGRAMS = ['pry', 'irb.cmd', 'irb'].freeze
4
+
5
+ # A string representation of the command line that evoked this ruby instance (platform agnostic)
6
+ def self.cmd_line(*args, include_args: true, include_ruby: true, prefix: nil, suffix: nil)
7
+ args = ARGV if args.empty?
8
+ include_ruby = false if special_program?
9
+ "#{prefix}#{include_ruby ? Command.quote(Gem.ruby) : nil} #{Command.quote($PROGRAM_NAME)}" \
10
+ " #{include_args ? args.map { |a| Command.quote(a) }.join(' ') : nil}#{suffix}"
11
+ .strip
12
+ end
13
+
14
+ # EXPERIMENTAL: Reloads the original file that was called
15
+ # Use at your own risk, this could cause some weird issues
16
+ def self.reload(include_args: true)
17
+ return false if special_program?
18
+ load cmd_line(*args, include_ruby: false, include_args: include_args)
19
+ end
20
+
21
+ # EXPERIMENTAL: Restart the ruby process that is currently running.
22
+ # Use at your own risk
23
+ def self.restart(*args, include_args: true, stay_alive: 1)
24
+ exit(0)
25
+ rescue SystemExit
26
+ opts = BBLib::OS.windows? ? { new_pgroup: true } : { pgroup: true }
27
+ pid = spawn(cmd_line(*args, include_args: include_args, prefix: (BBLib::OS.windows? ? 'start ' : nil)), **opts)
28
+ Process.detach(pid)
29
+ sleep(stay_alive)
30
+ exit(0) if special_program?
31
+ end
32
+
33
+ # Detects whether a console like irb or pry was used to launch this process
34
+ def self.special_program?
35
+ SPECIAL_PROGRAMS.include?($PROGRAM_NAME)
36
+ end
37
+ end
38
+
39
+ end
@@ -1,3 +1,3 @@
1
1
  module BBLib
2
- VERSION = '1.0.2'.freeze
2
+ VERSION = '2.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bblib
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Black
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-28 00:00:00.000000000 Z
11
+ date: 2018-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,50 +84,77 @@ files:
84
84
  - bblib.gemspec
85
85
  - bin/console
86
86
  - bin/setup
87
- - lib/array/bbarray.rb
88
87
  - lib/bblib.rb
88
+ - lib/bblib/all.rb
89
+ - lib/bblib/classes/cron.rb
90
+ - lib/bblib/classes/fuzzy_matcher.rb
91
+ - lib/bblib/cli.rb
92
+ - lib/bblib/cli/color.rb
93
+ - lib/bblib/cli/exceptions/invalid_argument.rb
94
+ - lib/bblib/cli/exceptions/missing_argument.rb
95
+ - lib/bblib/cli/exceptions/missing_required_argument.rb
96
+ - lib/bblib/cli/exceptions/opts_parser.rb
97
+ - lib/bblib/cli/option.rb
98
+ - lib/bblib/cli/options/basic_option.rb
99
+ - lib/bblib/cli/options/bool.rb
100
+ - lib/bblib/cli/options/command.rb
101
+ - lib/bblib/cli/options/date.rb
102
+ - lib/bblib/cli/options/float.rb
103
+ - lib/bblib/cli/options/integer.rb
104
+ - lib/bblib/cli/options/json.rb
105
+ - lib/bblib/cli/options/regexp.rb
106
+ - lib/bblib/cli/options/string.rb
107
+ - lib/bblib/cli/options/symbol.rb
108
+ - lib/bblib/cli/options/time.rb
109
+ - lib/bblib/cli/options/toggle.rb
110
+ - lib/bblib/cli/options/untoggle.rb
111
+ - lib/bblib/cli/opts_parser.rb
112
+ - lib/bblib/core.rb
113
+ - lib/bblib/core/classes/hash_struct.rb
114
+ - lib/bblib/core/classes/splitter.rb
115
+ - lib/bblib/core/classes/task_timer.rb
116
+ - lib/bblib/core/classes/tree_hash.rb
117
+ - lib/bblib/core/exceptions/abstract.rb
118
+ - lib/bblib/core/exceptions/exception.rb
119
+ - lib/bblib/core/hash_path/hash_path.rb
120
+ - lib/bblib/core/hash_path/part.rb
121
+ - lib/bblib/core/mixins/attrs.rb
122
+ - lib/bblib/core/mixins/bbmixins.rb
123
+ - lib/bblib/core/mixins/bridge.rb
124
+ - lib/bblib/core/mixins/delegator.rb
125
+ - lib/bblib/core/mixins/effortless.rb
126
+ - lib/bblib/core/mixins/family_tree.rb
127
+ - lib/bblib/core/mixins/hooks.rb
128
+ - lib/bblib/core/mixins/logger.rb
129
+ - lib/bblib/core/mixins/prototype.rb
130
+ - lib/bblib/core/mixins/serializer.rb
131
+ - lib/bblib/core/mixins/simple_init.rb
132
+ - lib/bblib/core/mixins/type_init.rb
133
+ - lib/bblib/core/util/array.rb
134
+ - lib/bblib/core/util/cases.rb
135
+ - lib/bblib/core/util/file.rb
136
+ - lib/bblib/core/util/hash.rb
137
+ - lib/bblib/core/util/logging.rb
138
+ - lib/bblib/core/util/matching.rb
139
+ - lib/bblib/core/util/number.rb
140
+ - lib/bblib/core/util/object.rb
141
+ - lib/bblib/core/util/opal.rb
142
+ - lib/bblib/core/util/os.rb
143
+ - lib/bblib/core/util/pluralization.rb
144
+ - lib/bblib/core/util/regexp.rb
145
+ - lib/bblib/core/util/roman.rb
146
+ - lib/bblib/core/util/string.rb
147
+ - lib/bblib/core/util/time.rb
148
+ - lib/bblib/cron.rb
149
+ - lib/bblib/fuzzy_matcher.rb
150
+ - lib/bblib/html.rb
151
+ - lib/bblib/html/builder.rb
152
+ - lib/bblib/html/tag.rb
153
+ - lib/bblib/html/tag_set.rb
154
+ - lib/bblib/system.rb
155
+ - lib/bblib/system/command.rb
156
+ - lib/bblib/system/system.rb
89
157
  - lib/bblib/version.rb
90
- - lib/class/effortless.rb
91
- - lib/error/abstract.rb
92
- - lib/file/bbfile.rb
93
- - lib/hash/bbhash.rb
94
- - lib/hash/hash_struct.rb
95
- - lib/hash/tree_hash.rb
96
- - lib/hash_path/hash_path.rb
97
- - lib/hash_path/part.rb
98
- - lib/hash_path/path_hash.rb
99
- - lib/hash_path/proc.rb
100
- - lib/hash_path/processors.rb
101
- - lib/html/bbhtml.rb
102
- - lib/html/builder.rb
103
- - lib/html/tag.rb
104
- - lib/html/tag_set.rb
105
- - lib/logging/bblogging.rb
106
- - lib/mixins/attrs.rb
107
- - lib/mixins/bbmixins.rb
108
- - lib/mixins/bridge.rb
109
- - lib/mixins/family_tree.rb
110
- - lib/mixins/hooks.rb
111
- - lib/mixins/logger.rb
112
- - lib/mixins/prototype.rb
113
- - lib/mixins/serializer.rb
114
- - lib/mixins/simple_init.rb
115
- - lib/mixins/type_init.rb
116
- - lib/number/bbnumber.rb
117
- - lib/object/bbobject.rb
118
- - lib/opal/bbopal.rb
119
- - lib/os/bbos.rb
120
- - lib/string/bbstring.rb
121
- - lib/string/cases.rb
122
- - lib/string/fuzzy_matcher.rb
123
- - lib/string/matching.rb
124
- - lib/string/pluralization.rb
125
- - lib/string/regexp.rb
126
- - lib/string/roman.rb
127
- - lib/system/bbsystem.rb
128
- - lib/time/bbtime.rb
129
- - lib/time/cron.rb
130
- - lib/time/task_timer.rb
131
158
  homepage: https://github.com/bblack16/bblib-ruby
132
159
  licenses:
133
160
  - MIT
@@ -1,3 +0,0 @@
1
- class AbstractError < StandardError
2
- # Enhance at some point
3
- end
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # This module provides similar functionality as hash path, but instead
4
- # generates a PathHash object which wraps a Hash or Array. Elements may
5
- # be accessed via method calls rather than path strings.
6
-
7
- module BBLib
8
- def self.path_hash(hash)
9
- PathHash.new(hash)
10
- end
11
-
12
- # Wraps a hash in a PathHash object which allows ActiveRecord-like access to hash parameters.
13
- # For example, methods are treated as keys passed in to Hash's [] method and ._ can be used to
14
- # indicate that the next element should be searched for recursively.
15
- class PathHash < BasicObject
16
- attr_reader :hash, :recursive
17
-
18
- def initialize(hash)
19
- @hash = hash
20
- end
21
-
22
- def [](val)
23
- PathHash.new(@hash.map { |h| h[val] })
24
- end
25
-
26
- def _val
27
- @hash
28
- end
29
-
30
- alias _v _val
31
-
32
- def _fval
33
- @hash.first
34
- rescue
35
- @hash
36
- end
37
-
38
- alias _f _fval
39
-
40
- def _
41
- @recursive = true
42
- self
43
- end
44
-
45
- def _path(arg, formula = nil)
46
- method_missing arg, formula
47
- end
48
-
49
- # Does not fall back on super as ALL input is accepted
50
- def method_missing(arg, formula = nil)
51
- arg = (@recursive ? "..#{arg}" : arg.to_s) +
52
- (formula ? "(#{formula})" : '')
53
- if @hash.is_a?(::Array)
54
- PathHash.new @hash.flat_map { |h| if h.is_a?(::Array) || h.is_a?(::Hash) then h.hash_path(arg) end }
55
- else
56
- PathHash.new @hash.hpath(arg)
57
- end
58
- end
59
-
60
- def respond_to_missing?(*args)
61
- true || super
62
- end
63
- end
64
- end
65
-
66
- # Monkey Patches
67
- class Hash
68
- def path_hash
69
- BBLib.path_hash(self)
70
- end
71
-
72
- alias phash path_hash
73
- alias _ph path_hash
74
- end
75
-
76
- # Monkey Patches
77
- class Array
78
- def path_hash
79
- BBLib.path_hash(self)
80
- end
81
-
82
- alias phash path_hash
83
- alias _ph path_hash
84
- end
@@ -1,93 +0,0 @@
1
- # frozen_string_literal: true
2
- require_relative 'processors'
3
-
4
- module BBLib
5
- # This class wraps around a hash path or set of paths and maps a set of actions for modifying elements at the matching
6
- # path.
7
- class HashPathProc
8
- include BBLib::Effortless
9
-
10
- attr_ary_of String, :paths, default: [''], serialize: true, uniq: true
11
- attr_of [String, Symbol], :action, default: nil, allow_nil: true, serialize: true, pre_proc: proc { |arg| HashPathProc.map_action(arg.to_s.to_sym) }
12
- attr_ary :args, default: [], serialize: true
13
- attr_hash :options, default: {}, serialize: true
14
- attr_of [String, Proc], :condition, default: nil, allow_nil: true, serialize: true
15
- attr_bool :recursive, default: false, serialize: true
16
- attr_bool :class_based, default: true, serialize: true
17
-
18
- def process(hash)
19
- return hash unless @action && hash
20
- tree = hash.to_tree_hash
21
- paths.each do |path|
22
- children = recursive ? tree.find(path).flat_map(&:descendants) : tree.find(path)
23
- children.each do |child|
24
- next unless check_condition(child.value)
25
- HashPathProcs.send(find_action(action), child, *full_args, class_based: class_based)
26
- end
27
- end
28
- hash.replace(tree.value) rescue tree.value
29
- end
30
-
31
- def check_condition(value)
32
- return true unless condition
33
- if condition.is_a?(String)
34
- eval(condition)
35
- else
36
- condition.call(value)
37
- end
38
- rescue => e
39
- false
40
- end
41
-
42
- protected
43
-
44
- USED_KEYWORDS = [:action, :args, :paths, :recursive, :condition].freeze
45
-
46
- def find_action(action)
47
- (HashPathProcs.respond_to?(action) ? action : :custom)
48
- end
49
-
50
- def full_args
51
- (HASH_PATH_PROC_TYPES.include?(action) ? [] : [action]) +
52
- args +
53
- (options.empty? || options.nil? ? [] : [options])
54
- end
55
-
56
- def self.map_action(action)
57
- clean = HASH_PATH_PROC_TYPES.find { |k, v| action == k || v.include?(action) }
58
- clean ? clean.first : action
59
- end
60
-
61
- def simple_init(*args)
62
- options = BBLib.named_args(*args)
63
- options.merge(options.delete(:options)) if options[:options]
64
- USED_KEYWORDS.each { |k| options.delete(k) }
65
- self.options = options
66
- if args.first.is_a?(Symbol) && @action.nil?
67
- self.action = args.shift
68
- self.paths = args.shift if args.first.is_a?(String)
69
- elsif action && args.first.is_a?(String)
70
- self.paths = args.first
71
- end
72
- self.args += args.find_all { |arg| !arg.is_a?(Hash) } unless args.empty?
73
- end
74
- end
75
- end
76
-
77
- # Monkey patches
78
- class Hash
79
- def hash_path_proc(*args)
80
- BBLib::HashPathProc.new(*args).process(self)
81
- end
82
-
83
- alias hpath_proc hash_path_proc
84
- end
85
-
86
- # Monkey patches
87
- class Array
88
- def hash_path_proc(*args)
89
- BBLib::HashPathProc.new(*args).process(self)
90
- end
91
-
92
- alias hpath_proc hash_path_proc
93
- end
@@ -1,239 +0,0 @@
1
-
2
- module BBLib
3
- def self.hash_path_proc(hash, *args)
4
- HashPathProc.new(*args).process(hash)
5
- end
6
-
7
- HASH_PATH_PROC_TYPES = {
8
- evaluate: [:eval, :equation, :equate],
9
- debug: [:puts],
10
- append: [:suffix],
11
- prepend: [:prefix],
12
- split: [:delimit, :delim, :separate, :msplit],
13
- replace: [:swap],
14
- replace_with: [],
15
- extract: [:grab, :scan],
16
- extract_first: [:grab_first, :scan_first],
17
- extract_last: [:grab_last, :scan_last],
18
- parse_date: [:date, :parse_time, :time],
19
- parse_date_unix: [:unix_time, :unix_date],
20
- parse_duration: [:duration],
21
- parse_file_size: [:file_size],
22
- to_string: [:to_s, :stringify],
23
- downcase: [:lower, :lowercase, :to_lower],
24
- upcase: [:upper, :uppercase, :to_upper],
25
- roman: [:convert_roman, :roman_numeral, :parse_roman],
26
- remove_symbols: [:chop_symbols, :drop_symbols],
27
- format_articles: [:articles],
28
- reverse: [:invert],
29
- delete: [:del],
30
- remove: [:rem],
31
- custom: [:send],
32
- encapsulate: [],
33
- uncapsulate: [],
34
- extract_integers: [:extract_ints, :extract_i],
35
- extract_floats: [:extract_f],
36
- extract_numbers: [:extract_nums],
37
- max_number: [:max, :maximum, :maximum_number],
38
- min_number: [:min, :minimum, :minimum_number],
39
- avg_number: [:avg, :average, :average_number],
40
- sum_number: [:sum],
41
- strip: [:trim],
42
- concat: [:join, :concat_with],
43
- reverse_concat: [:reverse_join, :reverse_concat_with]
44
- }.freeze
45
-
46
- module HashPathProcs
47
- def self.class_based_proc(child, class_based, *methods)
48
- child.replace_with(
49
- if class_based && child.node_class == Hash
50
- child.value.map { |k, v| [BBLib.recursive_send(k, *methods), v] }.to_h
51
- elsif class_based && child.node_class == Array
52
- child.children.values.flat_map { |v| class_based_proc(v, true, *methods) }
53
- else
54
- BBLib.recursive_send(child.value, *methods)
55
- end
56
- )
57
- end
58
-
59
- def self.evaluate(child, *args, class_based: true)
60
- value = child.value
61
- if args.first.is_a?(String)
62
- child.replace_with(eval(args.first))
63
- else
64
- child.replace_with(args.first.call(value))
65
- end
66
- end
67
-
68
- def self.debug(child, *args, class_based: true)
69
- puts "DEBUG: #{child.value}" + (args.empty? ? '' : "(#{args.join(', ')})")
70
- child
71
- end
72
-
73
- def self.append(child, *args, class_based: true)
74
- class_based_proc(child, class_based, :to_s, :dup, [:concat, args.first])
75
- end
76
-
77
- def self.prepend(child, *args, class_based: true)
78
- class_based_proc(child, class_based, :to_s, :dup, [:prepend, args.first])
79
- end
80
-
81
- def self.split(child, *args, class_based: true)
82
- class_based_proc(child, class_based, [:msplit, args])
83
- rescue => e
84
- class_based_proc(child, class_based, :to_s, [:msplit, args])
85
- end
86
-
87
- def self.replace(child, *args, class_based: true)
88
- methods = [:to_s]
89
- BBLib.hash_args(*args).each { |k, v| methods << [:gsub, (k ? k : k.to_s), v.to_s] }
90
- class_based_proc(child, class_based, *methods)
91
- end
92
-
93
- def self.replace_with(child, *args, class_based: true)
94
- child.replace_with(args.first)
95
- end
96
-
97
- def self.extract(child, *args, class_based: true)
98
- class_based_proc(child, class_based, [:scan, args.first], [:[], BBLib.named_args(*args)[:slice] || (0..-1)])
99
- end
100
-
101
- def self.extract_first(child, *args, class_based: true)
102
- class_based_proc(child, class_based, [:scan, args.first], :first)
103
- end
104
-
105
- def self.extract_last(child, *args, class_based: true)
106
- class_based_proc(child, class_based, [:scan, args.first], :last)
107
- end
108
-
109
- def self.parse_date(child, *args, class_based: true)
110
- format = BBLib.named_args(*args)[:format]
111
- child.replace_with(
112
- if class_based && child.node_class == Hash
113
- child.value.map do |k, v|
114
- [_parse_date(k, args, format), v]
115
- end.to_h
116
- elsif class_based && child.node_class == Array
117
- child.value.map do |v|
118
- _parse_date(v, args, format)
119
- end
120
- else
121
- _parse_date(child.value, args, format)
122
- end
123
- )
124
- end
125
-
126
- def self._parse_date(value, patterns, format)
127
- formatted = nil
128
- patterns.each do |pattern|
129
- next unless formatted.nil?
130
- formatted = Time.strptime(value.to_s, pattern.to_s) rescue nil
131
- formatted = formatted.strftime(format) if format
132
- end
133
- formatted
134
- end
135
-
136
- def self.parse_date_unix(child, *args, class_based: true)
137
- child.replace_with(parse_date(child, *args, class_based: class_based).to_f)
138
- end
139
-
140
- def self.parse_duration(child, *args, class_based: true)
141
- class_based_proc(child, class_based, :to_s, [:parse_duration, BBLib.named_args(*args)])
142
- end
143
-
144
- def self.parse_file_size(child, *args, class_based: true)
145
- class_based_proc(child, class_based, :to_s, [:parse_file_size, BBLib.named_args(*args)])
146
- end
147
-
148
- def self.to_string(child, *args, class_based: true)
149
- class_based_proc(child, class_based, :to_s)
150
- end
151
-
152
- def self.downcase(child, *args, class_based: true)
153
- class_based_proc(child, class_based, :to_s, :downcase)
154
- end
155
-
156
- def self.upcase(child, *args, class_based: true)
157
- class_based_proc(child, class_based, :to_s, :upcase)
158
- end
159
-
160
- def self.roman(child, *args, class_based: true)
161
- class_based_proc(child, class_based, :to_s, (args.first == :to ? :to_roman : :from_roman))
162
- end
163
-
164
- def self.remove_symbols(child, *args, class_based: true)
165
- class_based_proc(child, class_based, :to_s, :drop_symbols)
166
- end
167
-
168
- def self.format_articles(child, *args, class_based: true)
169
- class_based_proc(child, class_based, :to_s, [:format_articles, args.first || :front])
170
- end
171
-
172
- def self.reverse(child, *args, class_based: true)
173
- class_based_proc(child, class_based, :reverse)
174
- end
175
-
176
- def self.delete(child, *args, class_based: true)
177
- child.kill
178
- end
179
-
180
- def self.remove(child, *args, class_based: true)
181
- methods = args.map { |a| [:gsub, a, ''] }
182
- methods.shift(:to_s)
183
- class_based_proc(child, class_based, *methods )
184
- end
185
-
186
- def self.custom(child, *args, class_based: true)
187
- class_based_proc(child, class_based, *args)
188
- end
189
-
190
- def self.encapsulate(child, *args, class_based: true)
191
- class_based_proc(child, class_based, :to_s, [:concat, args.first.to_s], [:prepend, args.first.to_s])
192
- end
193
-
194
- def self.uncapsulate(child, *args, class_based: true)
195
- class_based_proc(child, class_based, [:uncapsulate, args.first])
196
- end
197
-
198
- def self.max(child, *args, class_based: true)
199
- class_based_proc(child, class_based, :max)
200
- end
201
-
202
- def self.min(child, *args, class_based: true)
203
- class_based_proc(child, class_based, :min)
204
- end
205
-
206
- def self.avg(child, *args, class_based: true)
207
- nums = child.value.to_s.extract_numbers
208
- child.replace_with(nums.inject { |s, x| s + x }.to_f / nums.size.to_f)
209
- end
210
-
211
- def self.sum(child, *args, class_based: true)
212
- child.replace_with(value.to_s.extract_numbers.inject { |s, x| s + x })
213
- end
214
-
215
- def self.strip(child, *args, class_based: true)
216
- class_based_proc(child, class_based, :to_s, :strip)
217
- end
218
-
219
- def self.extract_integers(child, *args, class_based: true)
220
- class_based_proc(child, class_based, :to_s, :extract_integers)
221
- end
222
-
223
- def self.extract_floats(child, *args, class_based: true)
224
- class_based_proc(child, class_based, :to_s, :extract_floats)
225
- end
226
-
227
- def self.extract_numbers(child, *args, class_based: true)
228
- class_based_proc(child, class_based, :to_s, :extract_numbers)
229
- end
230
-
231
- def self.concat(child, *args, class_based: true)
232
- child.replace_with(([child.value] + child.root.find(args.first)).map { |v| v.to_s }.join)
233
- end
234
-
235
- def self.reverse_concat(child, *args, class_based: true)
236
- child.replace_with(([child.value] + child.root.find(args.first)).map { |v| v.to_s }.reverse.join)
237
- end
238
- end
239
- end