class WebParser::Recipes

Attributes

recipe[R]
recipes[R]
type[R]

Public Class Methods

new(type=:single, recipe=nil, &block) click to toggle source
# File lib/web_parser/recipes.rb, line 5
def initialize type=:single, recipe=nil, &block
  raise "recipe must be set for type :each" if type == :each && !recipe
  @recipes, @type, @recipe = {}, type, recipe
  instance_eval(&block) if block_given?
end

Public Instance Methods

apply(doc) click to toggle source
# File lib/web_parser/recipes.rb, line 11
def apply doc
  recipes.inject({}) do |mem, (name, val)|
    if val.is_a?(Recipe)
      mem[name] = apply_recipe(doc, val)
    elsif val.is_a?(Recipes)
      mem[name] = apply_recipes(doc, val)
    end
    mem
  end
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/web_parser/recipes.rb, line 22
def method_missing name, *args, &block
  if    block_given? && args.size == 0
    @recipes[name] = Recipes.new(&block)
  elsif block_given? && (args.size == 2 || args.size == 3)
    args[2] ||= ->(val) { val } # just return array of elements
    @recipes[name] = Recipes.new(:each, Recipe.new(name, *args), &block)
  elsif !block_given? && (args.size == 2 || args.size == 3)
    @recipes[name] = Recipe.new(name, *args, &block)
  else
    super
  end
end

Private Instance Methods

apply_recipe(doc, recipe) click to toggle source
# File lib/web_parser/recipes.rb, line 37
def apply_recipe doc, recipe
  recipe.apply(doc)
end
apply_recipes(doc, recipes) click to toggle source
# File lib/web_parser/recipes.rb, line 41
def apply_recipes doc, recipes
  if recipes.type == :each
    recipes.recipe.apply(doc).map {|subdoc| recipes.apply(subdoc) }
  else
    recipes.apply(doc)
  end
end