Class: Brutal::Scaffold
- Inherits:
-
Object
- Object
- Brutal::Scaffold
- Defined in:
- lib/brutal/scaffold.rb
Overview
Brutal::Scaffold
Instance Attribute Summary collapse
- #actuals ⇒ Object readonly
- #contexts ⇒ Object readonly
- #header ⇒ Object readonly
- #subject ⇒ Object readonly
Instance Method Summary collapse
-
#blank_line ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength.
- #combinations_values ⇒ Object
- #context_names ⇒ Object
- #contexts_values ⇒ Object
-
#initialize(header, subject, *actuals, **contexts) ⇒ Scaffold
constructor
Initialize a new scaffold generator.
-
#inspect(object) ⇒ Object
Return a Ruby string that can be evaluated.
-
#to_s ⇒ String
Return a string representation.
Constructor Details
#initialize(header, subject, *actuals, **contexts) ⇒ Scaffold
Initialize a new scaffold generator
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
#actuals ⇒ Object (readonly)
8 9 10 |
# File 'lib/brutal/scaffold.rb', line 8 def actuals @actuals end |
#contexts ⇒ Object (readonly)
8 9 10 |
# File 'lib/brutal/scaffold.rb', line 8 def contexts @contexts end |
#header ⇒ Object (readonly)
8 9 10 |
# File 'lib/brutal/scaffold.rb', line 8 def header @header end |
#subject ⇒ Object (readonly)
8 9 10 |
# File 'lib/brutal/scaffold.rb', line 8 def subject @subject end |
Instance Method Details
#blank_line ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
63 64 65 66 67 |
# File 'lib/brutal/scaffold.rb', line 63 def blank_line "\n" \ "# #{('-' * 78)}\n" \ "\n" end |
#combinations_values ⇒ Object
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_names ⇒ Object
69 70 71 |
# File 'lib/brutal/scaffold.rb', line 69 def context_names contexts.keys.sort end |
#contexts_values ⇒ Object
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.
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_s ⇒ String
Return a string representation
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
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" |