Class: Render::Generator

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

Constant Summary collapse

FAUX_DATA_MAX =
1_000_000.freeze

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.



47
48
49
50
51
# File 'lib/render/generator.rb', line 47

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.



15
16
17
# File 'lib/render/generator.rb', line 15

def instances
  @instances
end

Instance Attribute Details

#algorithmObject

Returns the value of attribute algorithm.



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

def algorithm
  @algorithm
end

#matcherObject

Returns the value of attribute matcher.



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

def matcher
  @matcher
end

#typeObject

Returns the value of attribute type.



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

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



22
23
24
25
26
# 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) ⇒ Object



38
39
40
41
42
# 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

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



28
29
30
31
32
33
34
35
36
# 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

Instance Method Details

#trigger(algorithm_argument = nil) ⇒ Object



53
54
55
# File 'lib/render/generator.rb', line 53

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