Module: BBLib::HashPathProcs

Defined in:
lib/hash_path/processors.rb

Class Method Summary collapse

Class Method Details

._parse_date(value, patterns, format) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/hash_path/processors.rb', line 126

def self._parse_date(value, patterns, format)
  formatted = nil
  patterns.each do |pattern|
    next unless formatted.nil?
    formatted = Time.strptime(value.to_s, pattern.to_s) rescue nil
    formatted = formatted.strftime(format) if format
  end
  formatted
end

.append(child, *args, class_based: true) ⇒ Object



73
74
75
# File 'lib/hash_path/processors.rb', line 73

def self.append(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, :dup, [:concat, args.first])
end

.avg(child, *args, class_based: true) ⇒ Object



206
207
208
209
# File 'lib/hash_path/processors.rb', line 206

def self.avg(child, *args, class_based: true)
  nums = child.value.to_s.extract_numbers
  child.replace_with(nums.inject { |s, x| s + x }.to_f / nums.size.to_f)
end

.class_based_proc(child, class_based, *methods) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hash_path/processors.rb', line 47

def self.class_based_proc(child, class_based, *methods)
  child.replace_with(
    if class_based && child.node_class == Hash
      child.value.map { |k, v| [BBLib.recursive_send(k, *methods), v] }.to_h
    elsif class_based && child.node_class == Array
      child.children.values.flat_map { |v| class_based_proc(v, true, *methods) }
    else
      BBLib.recursive_send(child.value, *methods)
    end
  )
end

.concat(child, *args, class_based: true) ⇒ Object



231
232
233
# File 'lib/hash_path/processors.rb', line 231

def self.concat(child, *args, class_based: true)
  child.replace_with(([child.value] + child.root.find(args.first)).map { |v| v.to_s }.join)
end

.custom(child, *args, class_based: true) ⇒ Object



186
187
188
# File 'lib/hash_path/processors.rb', line 186

def self.custom(child, *args, class_based: true)
  class_based_proc(child, class_based, *args)
end

.debug(child, *args, class_based: true) ⇒ Object



68
69
70
71
# File 'lib/hash_path/processors.rb', line 68

def self.debug(child, *args, class_based: true)
  puts "DEBUG: #{child.value}" + (args.empty? ? '' : "(#{args.join(', ')})")
  child
end

.delete(child, *args, class_based: true) ⇒ Object



176
177
178
# File 'lib/hash_path/processors.rb', line 176

def self.delete(child, *args, class_based: true)
  child.kill
end

.downcase(child, *args, class_based: true) ⇒ Object



152
153
154
# File 'lib/hash_path/processors.rb', line 152

def self.downcase(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, :downcase)
end

.encapsulate(child, *args, class_based: true) ⇒ Object



190
191
192
# File 'lib/hash_path/processors.rb', line 190

def self.encapsulate(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, [:concat, args.first.to_s], [:prepend, args.first.to_s])
end

.evaluate(child, *args, class_based: true) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/hash_path/processors.rb', line 59

def self.evaluate(child, *args, class_based: true)
  value = child.value
  if args.first.is_a?(String)
    child.replace_with(eval(args.first))
  else
    child.replace_with(args.first.call(value))
  end
end

.extract(child, *args, class_based: true) ⇒ Object



97
98
99
# File 'lib/hash_path/processors.rb', line 97

def self.extract(child, *args, class_based: true)
  class_based_proc(child, class_based, [:scan, args.first], [:[], BBLib.named_args(*args)[:slice] || (0..-1)])
end

.extract_first(child, *args, class_based: true) ⇒ Object



101
102
103
# File 'lib/hash_path/processors.rb', line 101

def self.extract_first(child, *args, class_based: true)
  class_based_proc(child, class_based, [:scan, args.first], :first)
end

.extract_floats(child, *args, class_based: true) ⇒ Object



223
224
225
# File 'lib/hash_path/processors.rb', line 223

def self.extract_floats(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, :extract_floats)
end

.extract_integers(child, *args, class_based: true) ⇒ Object



219
220
221
# File 'lib/hash_path/processors.rb', line 219

def self.extract_integers(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, :extract_integers)
end

.extract_last(child, *args, class_based: true) ⇒ Object



105
106
107
# File 'lib/hash_path/processors.rb', line 105

def self.extract_last(child, *args, class_based: true)
  class_based_proc(child, class_based, [:scan, args.first], :last)
end

.extract_numbers(child, *args, class_based: true) ⇒ Object



227
228
229
# File 'lib/hash_path/processors.rb', line 227

def self.extract_numbers(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, :extract_numbers)
end

.format_articles(child, *args, class_based: true) ⇒ Object



168
169
170
# File 'lib/hash_path/processors.rb', line 168

def self.format_articles(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, [:format_articles, args.first || :front])
end

.max(child, *args, class_based: true) ⇒ Object



198
199
200
# File 'lib/hash_path/processors.rb', line 198

def self.max(child, *args, class_based: true)
  class_based_proc(child, class_based, :max)
end

.min(child, *args, class_based: true) ⇒ Object



202
203
204
# File 'lib/hash_path/processors.rb', line 202

def self.min(child, *args, class_based: true)
  class_based_proc(child, class_based, :min)
end

.parse_date(child, *args, class_based: true) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hash_path/processors.rb', line 109

def self.parse_date(child, *args, class_based: true)
  format = BBLib.named_args(*args)[:format]
  child.replace_with(
    if class_based && child.node_class == Hash
      child.value.map do |k, v|
        [_parse_date(k, args, format), v]
      end.to_h
    elsif class_based && child.node_class == Array
      child.value.map do |v|
        _parse_date(v, args, format)
      end
    else
      _parse_date(child.value, args, format)
    end
  )
end

.parse_date_unix(child, *args, class_based: true) ⇒ Object



136
137
138
# File 'lib/hash_path/processors.rb', line 136

def self.parse_date_unix(child, *args, class_based: true)
  child.replace_with(parse_date(child, *args, class_based: class_based).to_f)
end

.parse_duration(child, *args, class_based: true) ⇒ Object



140
141
142
# File 'lib/hash_path/processors.rb', line 140

def self.parse_duration(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, [:parse_duration, BBLib.named_args(*args)])
end

.parse_file_size(child, *args, class_based: true) ⇒ Object



144
145
146
# File 'lib/hash_path/processors.rb', line 144

def self.parse_file_size(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, [:parse_file_size, BBLib.named_args(*args)])
end

.prepend(child, *args, class_based: true) ⇒ Object



77
78
79
# File 'lib/hash_path/processors.rb', line 77

def self.prepend(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, :dup, [:prepend, args.first])
end

.remove(child, *args, class_based: true) ⇒ Object



180
181
182
183
184
# File 'lib/hash_path/processors.rb', line 180

def self.remove(child, *args, class_based: true)
  methods = args.map { |a| [:gsub, a, ''] }
  methods.shift(:to_s)
  class_based_proc(child, class_based, *methods )
end

.remove_symbols(child, *args, class_based: true) ⇒ Object



164
165
166
# File 'lib/hash_path/processors.rb', line 164

def self.remove_symbols(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, :drop_symbols)
end

.replace(child, *args, class_based: true) ⇒ Object



87
88
89
90
91
# File 'lib/hash_path/processors.rb', line 87

def self.replace(child, *args, class_based: true)
  methods = [:to_s]
  BBLib.hash_args(*args).each { |k, v| methods << [:gsub, (k ? k : k.to_s), v.to_s] }
  class_based_proc(child, class_based, *methods)
end

.replace_with(child, *args, class_based: true) ⇒ Object



93
94
95
# File 'lib/hash_path/processors.rb', line 93

def self.replace_with(child, *args, class_based: true)
  child.replace_with(args.first)
end

.reverse(child, *args, class_based: true) ⇒ Object



172
173
174
# File 'lib/hash_path/processors.rb', line 172

def self.reverse(child, *args, class_based: true)
  class_based_proc(child, class_based, :reverse)
end

.reverse_concat(child, *args, class_based: true) ⇒ Object



235
236
237
# File 'lib/hash_path/processors.rb', line 235

def self.reverse_concat(child, *args, class_based: true)
  child.replace_with(([child.value] + child.root.find(args.first)).map { |v| v.to_s }.reverse.join)
end

.roman(child, *args, class_based: true) ⇒ Object



160
161
162
# File 'lib/hash_path/processors.rb', line 160

def self.roman(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, (args.first == :to ? :to_roman : :from_roman))
end

.split(child, *args, class_based: true) ⇒ Object



81
82
83
84
85
# File 'lib/hash_path/processors.rb', line 81

def self.split(child, *args, class_based: true)
  class_based_proc(child, class_based, [:msplit, args])
rescue => e
  class_based_proc(child, class_based, :to_s, [:msplit, args])
end

.strip(child, *args, class_based: true) ⇒ Object



215
216
217
# File 'lib/hash_path/processors.rb', line 215

def self.strip(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, :strip)
end

.sum(child, *args, class_based: true) ⇒ Object



211
212
213
# File 'lib/hash_path/processors.rb', line 211

def self.sum(child, *args, class_based: true)
  child.replace_with(value.to_s.extract_numbers.inject { |s, x| s + x })
end

.to_string(child, *args, class_based: true) ⇒ Object



148
149
150
# File 'lib/hash_path/processors.rb', line 148

def self.to_string(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s)
end

.uncapsulate(child, *args, class_based: true) ⇒ Object



194
195
196
# File 'lib/hash_path/processors.rb', line 194

def self.uncapsulate(child, *args, class_based: true)
  class_based_proc(child, class_based, [:uncapsulate, args.first])
end

.upcase(child, *args, class_based: true) ⇒ Object



156
157
158
# File 'lib/hash_path/processors.rb', line 156

def self.upcase(child, *args, class_based: true)
  class_based_proc(child, class_based, :to_s, :upcase)
end