Class: Hyrum::Generators::FakeGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrum/generators/fake_generator.rb

Constant Summary collapse

DATA_FILE =
File.expand_path('../data/fake_messages.json', __dir__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FakeGenerator

Returns a new instance of FakeGenerator.



10
11
12
# File 'lib/hyrum/generators/fake_generator.rb', line 10

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/hyrum/generators/fake_generator.rb', line 8

def options
  @options
end

Instance Method Details

#generateObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hyrum/generators/fake_generator.rb', line 14

def generate
  messages = load_messages
  key = options[:key]&.to_s&.downcase
  number = (options[:number] || 1).to_i

  return messages unless key

  key_with_prefix = key.start_with?('e') ? key : "e#{key}"
  available_messages = messages[key_with_prefix] || []
  selected_messages = available_messages.sample([number, available_messages.length].min)

  # Prepend the original message if provided
  selected_messages = [options[:message]] + selected_messages if options[:message]

  # Return as a hash to match expected format
  { options[:key] => selected_messages }
end