Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
roktas committed Sep 14, 2024
1 parent 4ef276f commit c6f2633
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
12 changes: 6 additions & 6 deletions graphics/lib/sevgi/graphics/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

module Sevgi
module Graphics
ATTRIBUTE_INTERNAL_PREFIX = "-"
ATTRIBUTE_UPDATE_SUFFIX = "+"

module Attribute
module Ident
INTERNAL_PREFIX = "_"
UPDATE_SUFFIX = "+"

def internal?(given)
(@internal ||= {})[given] ||= given.start_with?(INTERNAL_PREFIX)
(@internal ||= {})[given] ||= given.start_with?(ATTRIBUTE_INTERNAL_PREFIX)
end

def id(given)
(@id ||= {})[given] ||= (updateable?(given) ? given.to_s.delete_suffix(UPDATE_SUFFIX) : given).to_sym
(@id ||= {})[given] ||= (updateable?(given) ? given.to_s.delete_suffix(ATTRIBUTE_UPDATE_SUFFIX) : given).to_sym
end

def updateable?(given)
(@updateable ||= {})[given] ||= given.end_with?(UPDATE_SUFFIX)
(@updateable ||= {})[given] ||= given.end_with?(ATTRIBUTE_UPDATE_SUFFIX)
end
end

Expand Down
2 changes: 1 addition & 1 deletion graphics/lib/sevgi/graphics/mixtures/duplicate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def Duplicate(dx: nil, dy: nil, parent: nil, &block)
duplicated.Traverse do |element|
element.children = element.children.map(&:dup)
id = (element.attributes = element.attributes.dup).delete(:id)
element[:_id] = id if id
element[:"#{ATTRIBUTE_INTERNAL_PREFIX}id"] = id if id
block&.call(element)
end

Expand Down
4 changes: 3 additions & 1 deletion graphics/lib/sevgi/graphics/mixtures/save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def Out(*, **, &filter)
F.out(self.(**), *, &filter)
end

def Save(path = nil, default: nil, &filter)
def Save(path = nil, default: nil, backup_suffix: nil, &filter)
default ||= F.subext(EXT, caller_locations(1..1).first.path)

if path
Expand All @@ -19,6 +19,8 @@ def Save(path = nil, default: nil, &filter)
default
end => path

::FileUtils.cp(path, "#{path}#{backup_suffix}") if backup_suffix && !backup_suffix.empty? && ::File.exist?(path)

Out(F.touch(path), &filter)
end
end
Expand Down
2 changes: 1 addition & 1 deletion graphics/test/graphics/mixtures/duplicable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_duplicate_deletes_id_attribute_by_default
def test_duplicate_can_produce_new_id_attribute
doc = SVG do
line(id: "original", "data-var": "main var").Duplicate do |element|
element[:id] = "#{element[:_id]}-copy" if element[:_id]
element[:id] = "#{element[:"#{ATTRIBUTE_INTERNAL_PREFIX}id"]}-copy" if element[:"#{ATTRIBUTE_INTERNAL_PREFIX}id"]
end
end

Expand Down

0 comments on commit c6f2633

Please sign in to comment.