mirror of
https://github.com/usetrmnl/terminus.git
synced 2026-08-13 14:29:27 -07:00
Refactored screens temp pather as root object
Necessary to remove from the `Upserters` namespace to reduce confusion and also to emphasise this is a more universal class that will be soon be used by upserters and other objects. Milestone: patch
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "dry/monads"
|
||||
require "inspectable"
|
||||
|
||||
module Terminus
|
||||
module Aspects
|
||||
module Screens
|
||||
# Saves content as image to temporary file path for optional processing.
|
||||
class TempPather
|
||||
include Deps["aspects.sanitizer", "aspects.screens.shoter", "aspects.screens.converter"]
|
||||
include Dry::Monads[:result]
|
||||
include Inspectable[sanitizer: :type]
|
||||
|
||||
def call(mold, &) = Pathname.mktmpdir { process mold, it, & }
|
||||
|
||||
private
|
||||
|
||||
def process mold, directory
|
||||
mold.output_path = directory.join mold.file_name
|
||||
|
||||
capture_input(mold, directory).bind { converter.call mold }
|
||||
.bind { |path| block_given? ? yield(path) : path }
|
||||
end
|
||||
|
||||
def capture_input mold, directory
|
||||
content = sanitizer.call mold.content
|
||||
|
||||
shoter.call(content, directory.join("input.png"), **mold.viewport)
|
||||
.fmap { |path| mold.input_path = path }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -9,12 +9,14 @@ module Terminus
|
||||
module Upserters
|
||||
# Creates screen record with image attachment from HTML content.
|
||||
class HTML
|
||||
include Deps["aspects.screens.upserters.temp_path", repository: "repositories.screen"]
|
||||
include Deps["aspects.screens.temp_pather", repository: "repositories.screen"]
|
||||
include Initable[struct: proc { Terminus::Structs::Screen.new }]
|
||||
include Dry::Monads[:result]
|
||||
|
||||
def call mold
|
||||
temp_path.call(mold) { |path| Success repository.upsert_with_image(path, mold, struct) }
|
||||
temp_pather.call mold do |path|
|
||||
Success repository.upsert_with_image(path, mold, struct)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "dry/monads"
|
||||
require "inspectable"
|
||||
|
||||
module Terminus
|
||||
module Aspects
|
||||
module Screens
|
||||
module Upserters
|
||||
# Saves HTML content as image to temporary file path for optional processing.
|
||||
class TempPath
|
||||
include Deps["aspects.sanitizer", "aspects.screens.shoter", "aspects.screens.converter"]
|
||||
include Dry::Monads[:result]
|
||||
include Inspectable[sanitizer: :type]
|
||||
|
||||
def call(mold, &) = Pathname.mktmpdir { process mold, it, & }
|
||||
|
||||
private
|
||||
|
||||
def process mold, directory
|
||||
mold.output_path = directory.join mold.file_name
|
||||
|
||||
capture_input(mold, directory).bind { converter.call mold }
|
||||
.bind { |path| block_given? ? yield(path) : path }
|
||||
end
|
||||
|
||||
def capture_input mold, directory
|
||||
content = sanitizer.call mold.content
|
||||
|
||||
shoter.call(content, directory.join("input.png"), **mold.viewport)
|
||||
.fmap { |path| mold.input_path = path }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
+7
-7
@@ -2,10 +2,10 @@
|
||||
|
||||
require "hanami_helper"
|
||||
|
||||
RSpec.describe Terminus::Aspects::Screens::Upserters::TempPath, :db do
|
||||
RSpec.describe Terminus::Aspects::Screens::TempPather, :db do
|
||||
using Refinements::Struct
|
||||
|
||||
subject(:creator) { described_class.new }
|
||||
subject(:pather) { described_class.new }
|
||||
|
||||
include_context "with screen mold"
|
||||
|
||||
@@ -15,23 +15,23 @@ RSpec.describe Terminus::Aspects::Screens::Upserters::TempPath, :db do
|
||||
before { mold.with! model_id: model.id }
|
||||
|
||||
it "answers path with specific name and extension (without block)" do
|
||||
expect(creator.call(mold).to_s).to match(%r(/test\.png))
|
||||
expect(pather.call(mold).to_s).to match(%r(/test\.png))
|
||||
end
|
||||
|
||||
it "answers pathname (without block)" do
|
||||
expect(creator.call(mold)).to match(kind_of(Pathname))
|
||||
expect(pather.call(mold)).to match(kind_of(Pathname))
|
||||
end
|
||||
|
||||
it "answers path with specific name and extension (with block)" do
|
||||
capture = nil
|
||||
creator.call(mold) { capture = it.to_s }
|
||||
pather.call(mold) { capture = it.to_s }
|
||||
|
||||
expect(capture).to match(%r(/test\.png))
|
||||
end
|
||||
|
||||
it "answers pathname (with block)" do
|
||||
capture = nil
|
||||
creator.call(mold) { capture = it }
|
||||
pather.call(mold) { capture = it }
|
||||
|
||||
expect(capture).to match(kind_of(Pathname))
|
||||
end
|
||||
@@ -39,7 +39,7 @@ RSpec.describe Terminus::Aspects::Screens::Upserters::TempPath, :db do
|
||||
|
||||
describe "#inspect" do
|
||||
it "has inspected attributes" do
|
||||
expect(creator.inspect).to match_inspection(sanitizer: "Terminus::Aspects::Sanitizer")
|
||||
expect(pather.inspect).to match_inspection(sanitizer: "Terminus::Aspects::Sanitizer")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -5,7 +5,7 @@ require "hanami_helper"
|
||||
RSpec.describe Terminus::Aspects::Screens::Upserters::HTML, :db do
|
||||
using Refinements::Struct
|
||||
|
||||
subject(:creator) { described_class.new }
|
||||
subject(:upserter) { described_class.new }
|
||||
|
||||
include_context "with screen mold"
|
||||
|
||||
@@ -15,7 +15,7 @@ RSpec.describe Terminus::Aspects::Screens::Upserters::HTML, :db do
|
||||
before { mold.with! model_id: model.id }
|
||||
|
||||
it "answers screen" do
|
||||
result = creator.call mold
|
||||
result = upserter.call mold
|
||||
|
||||
expect(result.success).to have_attributes(
|
||||
model_id: model.id,
|
||||
@@ -36,7 +36,7 @@ RSpec.describe Terminus::Aspects::Screens::Upserters::HTML, :db do
|
||||
|
||||
describe "#inspect" do
|
||||
it "has inspected attributes" do
|
||||
expect(creator.inspect).to match_inspection(sanitizer: "Terminus::Aspects::Sanitizer")
|
||||
expect(upserter.inspect).to match_inspection(sanitizer: "Terminus::Aspects::Sanitizer")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user