Class: Brutal::Scaffold

Inherits:
Object
  • Object
show all
Defined in:
lib/brutal/scaffold.rb

Overview

Brutal::Scaffold

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, subject, *actuals, **contexts) ⇒ Scaffold

Initialize a new scaffold generator.

Since:

  • 1.0.0



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/brutal/scaffold.rb', line 21

def initialize(header, subject, *actuals, **contexts)
  warn("Empty subject!")        if subject.empty?
  warn("Empty actual values!")  if actuals.empty?
  warn("Empty contexts!")       if contexts.empty?

  eval(header) # rubocop:disable Security/Eval

  @header   = header
  @subject  = subject
  @actuals  = actuals
  @contexts = contexts
end

Instance Attribute Details

#actualsObject (readonly)

Specifies templates to challenge evaluated subjects & get results.

Since:

  • 1.0.0



9
10
11
# File 'lib/brutal/scaffold.rb', line 9

def actuals
  @actuals
end

#contextsObject (readonly)

Specifies a list of variables to populate the subject’s template.

Since:

  • 1.0.0



12
13
14
# File 'lib/brutal/scaffold.rb', line 12

def contexts
  @contexts
end

#headerObject (readonly)

Specifies the code to execute before generating the test suite.

Since:

  • 1.0.0



15
16
17
# File 'lib/brutal/scaffold.rb', line 15

def header
  @header
end

#subjectObject (readonly)

Specifies the template of the code to be declined across contexts.

Since:

  • 1.0.0



18
19
20
# File 'lib/brutal/scaffold.rb', line 18

def subject
  @subject
end

Instance Method Details

#actual_ruby_code(actual_str) ⇒ Object

Since:

  • 1.0.0



85
86
87
88
89
90
91
92
# File 'lib/brutal/scaffold.rb', line 85

def actual_ruby_code(actual_str)
  <<~RUBY_CODE
    actual = begin
    #{actual_str.gsub(/^/, '  ')}
    end

  RUBY_CODE
end

#actual_ruby_codesObject

Since:

  • 1.0.0



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/brutal/scaffold.rb', line 70

def actual_ruby_codes
  combinations_values.map do |values|
    actual_str = format(inspect(subject), **attributes(*values))
    string = actual_ruby_code(actual_str)
    actual = eval(actual_str) # rubocop:disable Security/Eval, Lint/UselessAssignment

    actuals.each do |actual_value|
      result_str = format(actual_value, subject: "actual")
      string += "raise if #{result_str} != #{eval(result_str).inspect}\n" # rubocop:disable Security/Eval
    end

    string
  end
end

#attributes(*values) ⇒ Object

Since:

  • 1.0.0



48
49
50
51
52
# File 'lib/brutal/scaffold.rb', line 48

def attributes(*values)
  context_names.each_with_index.inject({}) do |h, (name, i)|
    h.merge(name.to_sym => inspect(values.fetch(i)))
  end
end

#combinations_valuesObject

Since:

  • 1.0.0



62
63
64
# File 'lib/brutal/scaffold.rb', line 62

def combinations_values
  Array(contexts_values[0]).product(*Array(contexts_values[1..]))
end

#context_namesObject

Since:

  • 1.0.0



54
55
56
# File 'lib/brutal/scaffold.rb', line 54

def context_names
  contexts.keys.sort
end

#contexts_valuesObject

Since:

  • 1.0.0



58
59
60
# File 'lib/brutal/scaffold.rb', line 58

def contexts_values
  context_names.map { |context_name| contexts.fetch(context_name) }
end

#header_ruby_codeObject

Since:

  • 1.0.0



94
95
96
97
98
# File 'lib/brutal/scaffold.rb', line 94

def header_ruby_code
  <<~RUBY_CODE
    #{header.chomp}
  RUBY_CODE
end

#inspect(object) ⇒ Object

Return a Ruby string that can be evaluated.

Since:

  • 1.0.0



35
36
37
38
39
# File 'lib/brutal/scaffold.rb', line 35

def inspect(object)
  return object.to_s unless object.is_a?(::String)

  object.strip
end

#ruby_linesObject

Since:

  • 1.0.0



66
67
68
# File 'lib/brutal/scaffold.rb', line 66

def ruby_lines
  [header_ruby_code] + actual_ruby_codes
end

#separator_ruby_codeObject

Since:

  • 1.0.0



100
101
102
103
104
105
# File 'lib/brutal/scaffold.rb', line 100

def separator_ruby_code
  <<~RUBY_CODE

    #{thematic_break_ruby_code}
  RUBY_CODE
end

#thematic_break_ruby_codeObject

Since:

  • 1.0.0



107
108
109
110
111
# File 'lib/brutal/scaffold.rb', line 107

def thematic_break_ruby_code
  <<~RUBY_CODE
    # #{'-' * 78}
  RUBY_CODE
end

#to_sString

Return a string representation.

Returns:

  • (String)

Since:

  • 1.0.0



44
45
46
# File 'lib/brutal/scaffold.rb', line 44

def to_s
  ruby_lines.join(separator_ruby_code)
end