Sha256: 10ac29c806b687084b013d1109ce391ed87cc2874902c9e089156bae89f43d30

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require "mediator/processor"

class Mediator
  class Renderer < Processor
    attr_reader :data
    attr_reader :mediator

    def initialize mediator, data = nil
      @data     = data || {}
      @mediator = mediator
    end

    def get name, options = nil
      selector = (options && options[:from]) || name
      (options && options[:value]) || mediator.get(selector)
    end

    def has? name, options = nil
      selector = (options && options[:from]) || name
      (options && options.has_key?(:value))  || mediator.subject_has?(selector)
    end

    def id name, options = {}
      key name, options.merge(from: "#{name}_id")
    end

    def ids name, options = {}
      unless options[:from]
        if name[-1] == "s"
          options = options.merge(from: "#{name[0..-2]}_ids")
        else
          options = options.merge(from: "#{name}_ids")
        end
      end

      key name, options
    end

    def key name, options = nil, &block
      if name[-1] == "?"
        (options ||= {})[:from] = name
        name = name[0..-2].intern
      end

      value = get name, options
      return if empty? value, options

      value = block ? block[value] : value

      data[name] = value unless empty? value, options
    end

    def many name, options = nil, &block
      value = get name, options
      data[name] = value.map { |v| sub v, options, &block }.
        reject { |v| empty? v, options }
    end

    def one name, options = nil, &block
      value = get name, options
      return if empty? value, options
 
      value = sub value, options, &block 
      return if empty? value, options

      options && options[:merge] ? data.merge!(value) : data[name] = value
    end

    def union name, options = nil, &block
      (options ||= {}).merge! merge: true
      one name, options, &block
    end

    private

    def sub value, options, &block
      rendered = Mediator[value, context: mediator].render
      block ? block[rendered] : rendered
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mediator-0.3.0 lib/mediator/renderer.rb