Class: Imasgen::GeneratorBase

Inherits:
Object
  • Object
show all
Defined in:
lib/imasgen/generator_base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGeneratorBase

Returns a new instance of GeneratorBase.



8
9
10
11
12
13
# File 'lib/imasgen/generator_base.rb', line 8

def initialize
  @namedic = []
  @used_pair = []
  @auto_reset = false
  @be_strict = true
end

Instance Attribute Details

#auto_resetObject

Returns the value of attribute auto_reset.



5
6
7
# File 'lib/imasgen/generator_base.rb', line 5

def auto_reset
  @auto_reset
end

#be_strictObject

Returns the value of attribute be_strict.



6
7
8
# File 'lib/imasgen/generator_base.rb', line 6

def be_strict
  @be_strict
end

Class Method Details

.getyamlObject



72
73
74
# File 'lib/imasgen/generator_base.rb', line 72

def self.getyaml
  throw NotImplementedError
end

Instance Method Details

#hotchpotchObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/imasgen/generator_base.rb', line 49

def hotchpotch
  # hotchpotch mode
  item = lambda {
    @basedic ||= @be_strict ? @namedic.select {|i| i[:last_name] != '' && i[:first_name] != ''} : @namedic
    # make shuffle dictionary
    @first_names ||= @basedic.map {|i| {first_name: i[:first_name], first_name_furigana: i[:first_name_furigana]}}
    @last_names ||= @basedic.map {|i| {last_name: i[:last_name], last_name_furigana: i[:last_name_furigana]}}
    # shuffle!!
    @comb ||= @first_names.product(@last_names).map {|i| i[0].merge(i[1])}
    # get non duplicate item
    ret = @comb.reject {|i| i[:used]}.sample
    return ret unless ret.nil?
    if @auto_reset
      @comb.each {|i| i.delete(:used)}
      ret
    else
      raise StandardError.new('Already used all name pairs.')
    end
  }.call
  item[:used] = true
  Imasgen::Name.new(item)
end

#nameObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/imasgen/generator_base.rb', line 32

def name
  # return random item.(no shuffle in first name and last name)
  item = lambda {
    @basedic ||= @be_strict ? @namedic.select {|i| i[:last_name] != '' && i[:first_name] != ''} : @namedic
    retval = @basedic.reject {|i| i[:used]}.sample
    raise StandardError.new('Already used all name pairs.') if retval.nil? && !@auto_reset
    if retval.nil? && @auto_reset
      # delete used flag
      @basedic.each {|i| i.delete(:used)}
      retval = @namedic.sample
    end
    retval
  }.call
  item[:used] = true
  Imasgen::Name.new(item)
end

#resetObject



19
20
21
22
23
24
25
# File 'lib/imasgen/generator_base.rb', line 19

def reset
  # reset dictonary cache
  @basedic = nil
  @first_names = nil
  @last_names = nil
  @comb = nil
end

#uniqueObject



15
16
17
# File 'lib/imasgen/generator_base.rb', line 15

def unique
  @unique_i ||= self.class.new
end