lib/node_mutation.rb in node_mutation-1.6.2 vs lib/node_mutation.rb in node_mutation-1.7.0
- old
+ new
@@ -7,13 +7,10 @@
class NodeMutation
class MethodNotSupported < StandardError; end
class ConflictActionError < StandardError; end
- KEEP_RUNNING = 1
- THROW_ERROR = 2
-
autoload :Adapter, "node_mutation/adapter"
autoload :ParserAdapter, "node_mutation/parser_adapter"
autoload :Action, 'node_mutation/action'
autoload :AppendAction, 'node_mutation/action/append_action'
autoload :DeleteAction, 'node_mutation/action/delete_action'
@@ -23,10 +20,11 @@
autoload :ReplaceAction, 'node_mutation/action/replace_action'
autoload :ReplaceWithAction, 'node_mutation/action/replace_with_action'
autoload :WrapAction, 'node_mutation/action/wrap_action'
autoload :NoopAction, 'node_mutation/action/noop_action'
autoload :Result, 'node_mutation/result'
+ autoload :Strategy, 'node_mutation/strategy'
attr_reader :actions
# Configure NodeMutation
# @param [Hash] options options to configure
@@ -45,14 +43,14 @@
def self.adapter
@adapter ||= ParserAdapter.new
end
# Get the strategy
- # @return [Integer] current strategy, could be {NodeMutation::KEEP_RUNNING} or {NodeMutation::THROW_ERROR},
- # by default is {NodeMutation::KEEP_RUNNING}
+ # @return [Integer] current strategy, could be {NodeMutation::Strategy::KEEP_RUNNING} or {NodeMutation::Strategy::THROW_ERROR},
+ # by default is {NodeMutation::Strategy::KEEP_RUNNING}
def self.strategy
- @strategy ||= KEEP_RUNNING
+ @strategy ||= Strategy::KEEP_RUNNING
end
# Initialize a NodeMutation.
# @param source [String] file source
def initialize(source)
@@ -212,11 +210,11 @@
conflict_actions = []
source = +@source
@actions.sort_by! { |action| [action.start, action.end] }
conflict_actions = get_conflict_actions
- if conflict_actions.size > 0 && NodeMutation.strategy == THROW_ERROR
+ if conflict_actions.size > 0 && strategy?(Strategy::THROW_ERROR)
raise ConflictActionError, "mutation actions are conflicted"
end
@actions.reverse_each do |action|
source[action.start...action.end] = action.new_code if action.new_code
end
@@ -240,11 +238,11 @@
end
conflict_actions = []
@actions.sort_by! { |action| [action.start, action.end] }
conflict_actions = get_conflict_actions
- if conflict_actions.size > 0 && NodeMutation.strategy == THROW_ERROR
+ if conflict_actions.size > 0 && strategy?(Strategy::THROW_ERROR)
raise ConflictActionError, "mutation actions are conflicted"
end
NodeMutation::Result.new(
affected: true,
conflicted: !conflict_actions.empty?,
@@ -267,19 +265,23 @@
while j > -1
# if we have two insert actions at same position.
same_position = begin_pos == @actions[j].start && begin_pos == end_pos && @actions[j].start == @actions[j].end
# if we have two actions with overlapped range.
overlapped_position = begin_pos < @actions[j].end
- if same_position || overlapped_position
+ if (!strategy?(Strategy::ALLOW_INSERT_AT_SAME_POSITION) && same_position) || overlapped_position
conflict_actions << @actions.delete_at(j)
else
i = j
begin_pos = @actions[i].start
end_pos = @actions[i].end
end
j -= 1
end
conflict_actions
+ end
+
+ def strategy?(strategy)
+ NodeMutation.strategy & strategy == strategy
end
def format_actions(actions)
actions.map { |action| OpenStruct.new(start: action.start, end: action.end, new_code: action.new_code ) }
end