Sha256: 98d60db29bb3a9933fd85e16167ab756fc62bfe4bf36a3124e0f6b12b1f268ad

Contents?: true

Size: 987 Bytes

Versions: 1

Compression:

Stored size: 987 Bytes

Contents

require 'json'

module Mayu

  class Renderer
    def initialize(spec)
      @spec = spec
    end

    def render(obj, spec: @spec)
      r = nil
      case obj
      when nil
        return nil
      when Array
        return obj.map { |_| render(_, spec: spec) }
      when Hash
        r = obj
      else
        case
        when obj.respond_to?(:as_json)
          r = obj.as_json
        when !spec.empty?
          r = {}
        else
          return obj
        end
      end
      next_specs, keys = [spec].flatten.partition { |_| Hash === _ }
      aref = Hash === obj
      r2 = r.transform_values do |v|
        render(v, spec: {})
      end
      keys.each do |k|
        r[k] = render(r.fetch(k) { aref ? obj.fetch(k) { obj.send(k) } : obj.send(k) }, spec: {})
      end
      (next_specs.inject(&:merge) || {}).each do |k, ns|
        r[k] = render(r.fetch(k) { aref ? obj.fetch(k) { obj.send(k) } : obj.send(k) }, spec: ns)
      end
      r2.merge(r)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mayu-0.1.0.beta1 lib/mayu/renderer.rb