class Render::Generator

Constants

FAUX_DATA_MAX

Attributes

instances[RW]
algorithm[RW]
matcher[RW]
type[RW]

Public Class Methods

create!(type, matcher, algorithm) click to toggle source

When in non-live mode, e.g. testing, you can use custom generators to make fake data for a given attribute type and name-matcher. @param type [Class] Use generator exclusively for this type of data @param matcher [Regexp] Use generator for attribute whose name matches this @param algorithm [Proc, call] Call this to generate a value

# File lib/render/generator.rb, line 22
def create!(type, matcher, algorithm)
  generator = new(type, matcher, algorithm)
  instances.unshift(generator)
  generator
end
find(type, to_match) click to toggle source
# File lib/render/generator.rb, line 38
def find(type, to_match)
  instances.detect do |generator|
    (type == generator.type) && to_match.to_s.match(generator.matcher)
  end
end
new(type, matcher, algorithm) click to toggle source
# File lib/render/generator.rb, line 47
def initialize(type, matcher, algorithm)
  self.type = type
  self.matcher = matcher
  set_algorithm!(algorithm)
end
trigger(type, to_match, algorithm_argument = nil) click to toggle source
# File lib/render/generator.rb, line 28
def trigger(type, to_match, algorithm_argument = nil)
  generator = find(type, to_match)
  if generator
    generator.trigger(algorithm_argument)
  else
    Render.logger.warn("Could not find generator for type #{type} with matcher for #{to_match}, using nil")
    nil
  end
end

Private Class Methods

least_multiple(multiple_of, min) click to toggle source
# File lib/render/generator.rb, line 68
def self.least_multiple(multiple_of, min)
  lowest_multiple = multiple_of
  until (lowest_multiple > min)
    lowest_multiple += multiple_of
  end
  lowest_multiple
end

Public Instance Methods

trigger(algorithm_argument = nil) click to toggle source
# File lib/render/generator.rb, line 53
def trigger(algorithm_argument = nil)
  algorithm[algorithm_argument]
end

Private Instance Methods

set_algorithm!(algorithm) click to toggle source
# File lib/render/generator.rb, line 59
def set_algorithm!(algorithm)
  if algorithm.respond_to?(:call)
    self.algorithm = algorithm
  else
    raise Errors::Generator::MalformedAlgorithm.new(algorithm)
  end
end