class Frebby

Public Class Methods

_transform_key_hook(key, **data) click to toggle source
# File lib/frebby/hooks.rb, line 25
def _transform_key_hook(key, **data)
  results = @@hooks[:key].map { |hook| hook.call(key, **data) }
  results.compact.first || key
end
_transform_result_hook(result, **data) click to toggle source
# File lib/frebby/hooks.rb, line 35
def _transform_result_hook(result, **data)
  results = @@hooks[:result].map { |hook| hook.call(result, **data) }
  results.compact.first || result
end
_transform_value_hook(value, **data) click to toggle source
# File lib/frebby/hooks.rb, line 30
def _transform_value_hook(value, **data)
  results = @@hooks[:value].map { |hook| hook.call(value, **data) }
  results.compact.first || value
end
as_json(&blk) click to toggle source

rubocop:enable Naming/UncommunicativeMethodParamName

# File lib/frebby/transform.rb, line 25
def as_json(&blk)
  item = Frebby.new(&blk)
  Frebby._transform_result_hook(item).to_json
end
customize_key(&hook) click to toggle source
# File lib/frebby/hooks.rb, line 13
def customize_key(&hook)
  @@hooks[:key] << hook
end
customize_result(&hook) click to toggle source
# File lib/frebby/hooks.rb, line 21
def customize_result(&hook)
  @@hooks[:result] << hook
end
customize_value(&hook) click to toggle source
# File lib/frebby/hooks.rb, line 17
def customize_value(&hook)
  @@hooks[:value] << hook
end
pluralize(key, as: nil) click to toggle source

rubocop:disable Naming/UncommunicativeMethodParamName ^ Because I think `as` makes sense here and allows for a nice DSL

# File lib/frebby/transform.rb, line 13
def pluralize(key, as: nil)
  customize_key { |k| as if k == key.to_s } unless as.nil?

  customize_value do |v, original_key:, transformed_key:, **_data|
    keys = [key.to_s, as.to_s].compact
    should_be_array = keys.include?(original_key) ||
                      keys.include?(transformed_key)
    should_be_array && !v.is_a?(Array) ? [v] : nil
  end
end
read_file(filename) click to toggle source
# File lib/frebby/recursion.rb, line 8
def read_file(filename)
  source = caller_locations(1..1).first.absolute_path
  current_dir = source == '-' ? Dir.pwd : File.dirname(source)
  file = File.expand_path(filename, current_dir)
  raise "File #{file} does not exist" unless File.exist?(file)
  raise "Could not read file #{file}" unless File.readable?(file)
  JSON.parse run_recursive(file)
end
read_text(text) click to toggle source
# File lib/frebby/recursion.rb, line 17
def read_text(text)
  JSON.parse run_recursive(stdin_text: text)
end
transform(key, into:) click to toggle source
# File lib/frebby/transform.rb, line 7
def transform(key, into:)
  customize_key { |k| into if k == key.to_s }
end

Private Class Methods

run_recursive(*args, stdin_text: nil) click to toggle source
# File lib/frebby/recursion.rb, line 23
def run_recursive(*args, stdin_text: nil)
  args.unshift 'frebby'
  Open3.popen3 args.join(' ') do |stdin, stdout, stderr, thread|
    stdin.write stdin_text unless stdin_text.nil?
    stdin.close
    error = stderr.read
    status = thread.value
    raise error unless status.success? && error.empty?
    stdout.read
  end
end