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



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/brutal/scaffold.rb', line 11

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)

Since:

  • 1.0.0



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

def actuals
  @actuals
end

#contextsObject (readonly)

Since:

  • 1.0.0



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

def contexts
  @contexts
end

#headerObject (readonly)

Since:

  • 1.0.0



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

def header
  @header
end

#subjectObject (readonly)

Since:

  • 1.0.0



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

def subject
  @subject
end

Instance Method Details

#blank_lineObject

rubocop:enable Metrics/AbcSize, Metrics/MethodLength

Since:

  • 1.0.0



63
64
65
66
67
# File 'lib/brutal/scaffold.rb', line 63

def blank_line
  "\n"                \
  "# #{('-' * 78)}\n" \
  "\n"
end

#combinations_valuesObject

Since:

  • 1.0.0



77
78
79
# File 'lib/brutal/scaffold.rb', line 77

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

#context_namesObject

Since:

  • 1.0.0



69
70
71
# File 'lib/brutal/scaffold.rb', line 69

def context_names
  contexts.keys.sort
end

#contexts_valuesObject

Since:

  • 1.0.0



73
74
75
# File 'lib/brutal/scaffold.rb', line 73

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

#inspect(object) ⇒ Object

Return a Ruby string that can be evaluated.

Since:

  • 1.0.0



25
26
27
28
29
# File 'lib/brutal/scaffold.rb', line 25

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

  object.strip
end

#to_sString

Return a string representation

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Since:

  • 1.0.0



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/brutal/scaffold.rb', line 36

def to_s
  header.chomp + "\n" + blank_line + combinations_values.map do |values|
    attributes = context_names.each_with_index.inject({}) do |h, (name, i)|
      h.merge(name.to_sym => inspect(values.fetch(i)))
    end

    actual_str = format(inspect(subject), **attributes)

    string = "      actual = begin\n      \#{actual_str.gsub(/^/, '  ')}\n      end\n\n    CODE\n\n    actual = eval(actual_str) # rubocop:disable Security/Eval, Lint/UselessAssignment\n\n    actuals.each do |actual_value|\n      result_str = format(actual_value, subject: 'actual')\n      string += \"raise if \#{result_str} != \#{eval(result_str).inspect}\\n\" # rubocop:disable Security/Eval\n    end\n\n    string\n  end.join(blank_line)\nend\n"