Module: Transpec::Syntax::RSpecConfigure::Framework::ConfigurationAddition

Included in:
Transpec::Syntax::RSpecConfigure::Framework
Defined in:
lib/transpec/syntax/rspec_configure/framework.rb

Instance Method Summary collapse

Instance Method Details

#add_configuration!(config_name, value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/transpec/syntax/rspec_configure/framework.rb', line 68

def add_configuration!(config_name, value)
  lines = [body_indentation + "#{config_variable_name}.#{config_name} = #{value}"]

  unless block_node
    lines.unshift(framework_begin_code)
    lines << framework_end_code
  end

  lines.unshift('') unless empty_block_body?(block_node_to_insert_code)
  lines.map! { |line| line + "\n" }

  insertion_position = beginning_of_line_range(block_node_to_insert_code.loc.end)
  source_rewriter.insert_before(insertion_position, lines.join(''))
end

#block_node_to_insert_codeObject



119
120
121
# File 'lib/transpec/syntax/rspec_configure/framework.rb', line 119

def block_node_to_insert_code
  block_node || rspec_configure.node
end

#body_indentationObject



95
96
97
98
99
100
101
# File 'lib/transpec/syntax/rspec_configure/framework.rb', line 95

def body_indentation
  if block_node
    indentation_of_line(block_node) + (' ' * 2)
  else
    indentation_of_line(rspec_configure.node) + (' ' * 4)
  end
end

#config_variable_nameObject



83
84
85
# File 'lib/transpec/syntax/rspec_configure/framework.rb', line 83

def config_variable_name
  block_arg_name || new_config_variable_name
end

#empty_block_body?(block_node) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/transpec/syntax/rspec_configure/framework.rb', line 123

def empty_block_body?(block_node)
  (block_node.loc.end.line - block_node.loc.begin.line) <= 1
end

#framework_begin_codeObject



103
104
105
106
107
108
109
# File 'lib/transpec/syntax/rspec_configure/framework.rb', line 103

def framework_begin_code
  code = format(
    '%s.%s :rspec do |%s|',
    rspec_configure.block_arg_name, block_method_name, config_variable_name
  )
  rspec_configure_body_indentation + code
end

#framework_end_codeObject



111
112
113
# File 'lib/transpec/syntax/rspec_configure/framework.rb', line 111

def framework_end_code
  rspec_configure_body_indentation + 'end'
end

#new_config_variable_nameObject



87
88
89
90
91
92
93
# File 'lib/transpec/syntax/rspec_configure/framework.rb', line 87

def new_config_variable_name
  case rspec_configure.block_arg_name
  when :rspec then self.class.name.split('::').last.downcase
  when :c     then 'config'
  else 'c'
  end
end

#rspec_configure_body_indentationObject



115
116
117
# File 'lib/transpec/syntax/rspec_configure/framework.rb', line 115

def rspec_configure_body_indentation
  indentation_of_line(rspec_configure.node) + (' ' * 2)
end