Sha256: 0ca8923a29ae99df614012c7c16b26bd96d3a370bdd243f7e1a466d30571b6c0

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require "mediator/processor"

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

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

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

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

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

    def ids name, options = {}
      if name[-1] == "s" and !options[:from]
        id_name = "#{name[0..-2]}_ids"
      else
        id_name = "#{name}_ids"
      end

      options[:from] ||= name

      key id_name, options
    end

    def key name, options = nil, &block
      if name[-1] == "?"
        name = name[0..-2].intern
      end

      return unless has? name, options

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

      value = block ? block[value] : value

      mediator.set name, value unless empty? value, options
    end

    def many name, options = nil, &block
      data = get name, options
      subj = (options && options[:subject]) || mediator.get(name)

      data.map { |d| sub subj[data.index d], d, options, &block }.
        reject { |v| empty? v, options }
    end

    def one name, options = nil, &block
      data = get name, options
      subj = (options && options[:subject]) || mediator.get(name)

      sub subj, data, options, &block
    end

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

    private

    def sub subj, data, options, &block
      return if empty? data, options or subj.nil?

      Mediator[subj, mediator].parse data
      block[subj] if block

      subj
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mediator-0.2.0 lib/mediator/parser.rb