Class: Jumon::Subject

Inherits:
Object
  • Object
show all
Defined in:
lib/jumon/subject.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Subject

Returns a new instance of Subject.



7
8
9
10
11
# File 'lib/jumon/subject.rb', line 7

def initialize(base)
  @template = base[:subject]
  @variables = base[:variables]
  @subjects = [@template]
end

Instance Attribute Details

#subjectsObject (readonly)

Returns the value of attribute subjects.



5
6
7
# File 'lib/jumon/subject.rb', line 5

def subjects
  @subjects
end

#templateObject (readonly)

Returns the value of attribute template.



5
6
7
# File 'lib/jumon/subject.rb', line 5

def template
  @template
end

#variablesObject (readonly)

Returns the value of attribute variables.



5
6
7
# File 'lib/jumon/subject.rb', line 5

def variables
  @variables
end

Instance Method Details

#add_subject(var_i, pat_i) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jumon/subject.rb', line 22

def add_subject(var_i, pat_i)
  copy = subjects.dup
  copy.each do |c|
    result = c.gsub(
      "{{#{variables[var_i][:name]}}}",
      variables[var_i][:patterns][pat_i]
    )
    @subjects.push(result)
  end

  if pat_i < variables[var_i][:patterns].length - 1
    add_subject(var_i, pat_i + 1)
  elsif var_i < variables.length - 1
    add_subject(var_i + 1, 0)
  end
end

#set_subjectsObject



13
14
15
16
17
18
19
20
# File 'lib/jumon/subject.rb', line 13

def set_subjects
  return unless variables

  add_subject(0, 0)
  @subjects = @subjects.uniq.reject do |s|
    variables.any? { |v| s.include?("{{#{v[:name]}}}") }
  end
end