Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
roktas committed Sep 23, 2024
1 parent 265e8bf commit 7960d4d
Show file tree
Hide file tree
Showing 28 changed files with 397 additions and 1,410 deletions.
2 changes: 1 addition & 1 deletion graphics/lib/sevgi/graphics/document/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Base < Proto
mixture :Duplicate
mixture :Identify
mixture :Lint
mixture :Replicate
mixture :Save
mixture :Tile
mixture :Transform
mixture :Underscore
mixture :Validate
Expand Down
2 changes: 1 addition & 1 deletion graphics/lib/sevgi/graphics/mixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
require_relative "mixtures/polyfills"
require_relative "mixtures/rdf"
require_relative "mixtures/render"
require_relative "mixtures/replicate"
require_relative "mixtures/save"
require_relative "mixtures/tile"
require_relative "mixtures/transform"
require_relative "mixtures/underscore"
require_relative "mixtures/validate"
Expand Down
38 changes: 1 addition & 37 deletions graphics/lib/sevgi/graphics/mixtures/identify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,11 @@ def Disidentify
Traverse do |element|
next unless element[:id]

element[:_id] = element.attributes.delete(:id)
element[:"#{ATTRIBUTE_INTERNAL_PREFIX}id"] = element.attributes.delete(:id)
end
end

def IdentifyAsTable(...) = IdentifyAsTable.(...)

def IdentifyAsList(...) = IdentifyAsList.(...)

def Reidentify
end

def Identifiers = Identifiers.new(self)

SEPARATOR = "-"

IdentifyAs = Data.define(:id) do
def label(*indexes)
id and [ id, *indexes ].map(&:to_s).join(SEPARATOR)
end
end

IdentifyAsList = Class.new(IdentifyAs) do
def self.call(element, i)
element.each_with_index do |it, index|
i and (label = self[i].label(index + 1)) and it[:id] = label
end
end
end

IdentifyAsTable = Class.new(IdentifyAs) do
def self.call(element, ix: nil, iy: nil)
element.each_with_index do |row, irow|
iy and (label = self[iy].label(irow + 1)) and row[:id] = label
ix and row.children.each_with_index do |col, icol|
(label = self[ix].label(irow + 1, icol + 1)) and col[:id] = label
end
end
end
end

private_constant :IdentifyAs, :IdentifyAsTable, :IdentifyAsList
end
end
end
Expand Down
120 changes: 0 additions & 120 deletions graphics/lib/sevgi/graphics/mixtures/replicate.rb

This file was deleted.

92 changes: 92 additions & 0 deletions graphics/lib/sevgi/graphics/mixtures/tile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: true

module Sevgi
module Graphics
module Mixtures
module Tile
DEFAULT_PROC = proc do |element, href, *positions|
element[:id] = [ href, *positions.compact ].join("-")
end

# rubocop:disable Layout/MultilineArrayLineBreaks,Metrics/MethodLength

def Tile(id = nil, nx: Undefined, dx: Undefined, ox: 0, ny: Undefined, dy: Undefined, oy: 0, &block)
Assert.(tiled = id || self[:id], nx:, dx:, ox:, ny:, dy:, oy:)

href, proc, coords = tiled, block || DEFAULT_PROC, proc do |x, y|
x.zero? ? (y.zero? ? {} : { y: }) : (y.zero? ? { x: } : { x:, y: }) # for pretty kwargs handling
end

public_send(id.nil? ? :With : :Within) do
ny.times do |i|
nx.times do |j|
proc.(use(href: "##{href}", **coords.(j * dx + ox, i * dy + oy)), href, i + 1, j + 1)
end
end
end
end

def TileX(id = nil, n: Undefined, d: Undefined, o: 0, &block)
Assert.(tiled = id || self[:id], n:, d:, o:)

href, proc, coords = tiled, block || DEFAULT_PROC, proc do |x|
x.zero? ? {} : { x: } # for pretty kwargs handling
end

public_send(id.nil? ? :With : :Within) do
n.times do |i|
proc.(use(href: "##{href}", **coords.(i * d + o)), href, i + 1)
end
end
end

def TileY(id = nil, n: Undefined, d: Undefined, o: 0, &block)
Assert.(tiled = id ||self[:id], n:, d:, o:)

href, proc, coords = tiled, block || DEFAULT_PROC, proc do |y|
y.zero? ? {} : { y: } # for pretty kwargs handling
end

public_send(id.nil? ? :With : :Within) do
n.times do |i|
proc.(use(href: "##{href}", **coords.(i * d + o)), href, i + 1)
end
end
end
end

module Assert
extend self

ASSERTION = {
n: proc { |name, value| "Argument '#{name}' must be a positive integer" unless value.positive? },
nx: proc { |name, value| "Argument '#{name}' must be a positive integer" unless value.positive? },
ny: proc { |name, value| "Argument '#{name}' must be a positive integer" unless value.positive? },
d: proc { |name, value| "Argument '#{name}' must be a number" unless value.is_a?(::Numeric) },
dx: proc { |name, value| "Argument '#{name}' must be a number" unless value.is_a?(::Numeric) },
dy: proc { |name, value| "Argument '#{name}' must be a number" unless value.is_a?(::Numeric) },
o: proc { |name, value| "Argument '#{name}' must be a number" unless value.is_a?(::Numeric) },
ox: proc { |name, value| "Argument '#{name}' must be a number" unless value.is_a?(::Numeric) },
oy: proc { |name, value| "Argument '#{name}' must be a number" unless value.is_a?(::Numeric) }
}.freeze

def call(id, **kwargs)
ArgumentError.("Tiled element must have an id") unless id

kwargs.each do |name, value|
issue = "Argument '#{name}' required" if value == Undefined

unless issue
next unless (assertion = ASSERTION[name])
next unless (issue = assertion.call(name, value))
end

ArgumentError.(issue)
end
end
end

private_constant :Assert
end
end
end
78 changes: 0 additions & 78 deletions graphics/test/graphics/mixtures/inkscape_test.rb

This file was deleted.

Loading

0 comments on commit 7960d4d

Please sign in to comment.