Class: Render::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/render/generator.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, matcher, algorithm) ⇒ Generator

Returns a new instance of Generator.



40
41
42
43
44
# File 'lib/render/generator.rb', line 40

def initialize(type, matcher, algorithm)
  self.type = type
  self.matcher = matcher
  set_algorithm!(algorithm)
end

Class Attribute Details

.instancesObject

Returns the value of attribute instances.



13
14
15
# File 'lib/render/generator.rb', line 13

def instances
  @instances
end

Instance Attribute Details

#algorithmObject

Returns the value of attribute algorithm.



38
39
40
# File 'lib/render/generator.rb', line 38

def algorithm
  @algorithm
end

#matcherObject

Returns the value of attribute matcher.



38
39
40
# File 'lib/render/generator.rb', line 38

def matcher
  @matcher
end

#typeObject

Returns the value of attribute type.



38
39
40
# File 'lib/render/generator.rb', line 38

def type
  @type
end

Class Method Details

.create!(type, matcher, algorithm) ⇒ Object

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.

Parameters:

  • type (Class)

    Use generator exclusively for this type of data

  • matcher (Regexp)

    Use generator for attribute whose name matches this

  • algorithm (Proc, #call)

    Call this to generate a value



20
21
22
23
24
# File 'lib/render/generator.rb', line 20

def create!(type, matcher, algorithm)
  generator = new(type, matcher, algorithm)
  instances.unshift(generator)
  generator
end

.find(type, to_match) ⇒ Object



31
32
33
34
35
# File 'lib/render/generator.rb', line 31

def find(type, to_match)
  instances.detect do |generator|
    (generator.type == type) && to_match.match(generator.matcher)
  end
end

.trigger(type, to_match, algorithm_argument = nil) ⇒ Object



26
27
28
29
# File 'lib/render/generator.rb', line 26

def trigger(type, to_match, algorithm_argument = nil)
  generator = find(type, to_match)
  generator.trigger(algorithm_argument)
end

Instance Method Details

#trigger(algorithm_argument = nil) ⇒ Object



46
47
48
# File 'lib/render/generator.rb', line 46

def trigger(algorithm_argument = nil)
  algorithm[algorithm_argument]
end