module Transpec::Syntax::RSpecConfigure::ConfigModification

Public Instance Methods

block_node() click to toggle source
# File lib/transpec/syntax/rspec_configure/config_modification.rb, line 12
def block_node
  fail NotImplementedError
end

Private Instance Methods

block_arg_name() click to toggle source
# File lib/transpec/syntax/rspec_configure/config_modification.rb, line 63
def block_arg_name
  return nil unless block_node
  first_block_arg_name(block_node)
end
config_record_syntax(config_name, value = nil) click to toggle source
# File lib/transpec/syntax/rspec_configure/config_modification.rb, line 68
def config_record_syntax(config_name, value = nil)
  selector = config_name.to_s.sub(/=$/, '')
  syntax = "RSpec.configure { |c| c.#{selector}"

  value = 'something' if config_name.to_s.end_with?('=')
  syntax << " = #{value}" unless value.nil?

  syntax << ' }'
end
find_config_node(config_method_name) click to toggle source
# File lib/transpec/syntax/rspec_configure/config_modification.rb, line 30
def find_config_node(config_method_name)
  return nil unless block_node

  config_method_name = config_method_name.to_sym

  block_node.each_descendant(:send).find do |send_node|
    receiver_node, method_name, = *send_node
    next unless receiver_node == s(:lvar, block_arg_name)
    method_name == config_method_name
  end
end
modify_config_value!(config_node, value) click to toggle source
# File lib/transpec/syntax/rspec_configure/config_modification.rb, line 42
def modify_config_value!(config_node, value)
  arg_range = config_node.children[2].loc.expression
  replace(arg_range, value.to_s)

  config_name = config_node.loc.selector.source
  old_syntax = config_record_syntax(config_name, arg_range.source)
  new_syntax = config_record_syntax(config_name, value)
  add_record(old_syntax, new_syntax, type: :modification)
end
replace_config!(old_config_name, new_config_name) click to toggle source
# File lib/transpec/syntax/rspec_configure/config_modification.rb, line 52
def replace_config!(old_config_name, new_config_name)
  config_node = find_config_node(old_config_name)
  return unless config_node
  new_selector = new_config_name.to_s.sub(/=$/, '')
  replace(config_node.loc.selector, new_selector)

  old_syntax = config_record_syntax(old_config_name)
  new_syntax = config_record_syntax(new_config_name)
  add_record(old_syntax, new_syntax)
end
set_config_value!(config_name, value, comment = nil) click to toggle source
# File lib/transpec/syntax/rspec_configure/config_modification.rb, line 18
def set_config_value!(config_name, value, comment = nil)
  config_node = find_config_node("#{config_name}=")

  if config_node
    current_value = config_node.children[2].loc.expression.source
    return if value.to_s == current_value
    modify_config_value!(config_node, value)
  else
    add_config!(config_name, value, comment)
  end
end