Sha256: 79f151da9e4ecbf1a2064b910daf211f45bfb2bef297c18f12af56eb86f43d00
Contents?: true
Size: 1.97 KB
Versions: 2
Compression:
Stored size: 1.97 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)) || data.has_key?(selector) || data.has_key?(selector.to_s) 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
mediator-0.1.1 | lib/mediator/parser.rb |
mediator-0.1.0 | lib/mediator/parser.rb |