From 463b8ec076fd1a150a122a160d007e3476358586 Mon Sep 17 00:00:00 2001 From: Bryan Hockey Date: Wed, 6 Dec 2023 15:11:30 -0500 Subject: [PATCH] enums without .new --- lauth/lib/lauth/determination/allowed.rb | 2 +- lauth/lib/lauth/determination/base.rb | 36 ++++++++++++++---------- lauth/lib/lauth/determination/denied.rb | 2 +- lauth/lib/lauth/ops/authorize.rb | 4 +-- lauth/spec/determination_spec.rb | 8 ++---- lauth/spec/ops/authorize_spec.rb | 4 +-- lauth/spec/requests/authorized_spec.rb | 2 +- 7 files changed, 30 insertions(+), 28 deletions(-) diff --git a/lauth/lib/lauth/determination/allowed.rb b/lauth/lib/lauth/determination/allowed.rb index a1dee3c6..768d366c 100644 --- a/lauth/lib/lauth/determination/allowed.rb +++ b/lauth/lib/lauth/determination/allowed.rb @@ -4,7 +4,7 @@ class Allowed < Base ALLOWED = "allowed" - def type + def self.type ALLOWED end diff --git a/lauth/lib/lauth/determination/base.rb b/lauth/lib/lauth/determination/base.rb index 02e1add3..6b999d90 100644 --- a/lauth/lib/lauth/determination/base.rb +++ b/lauth/lib/lauth/determination/base.rb @@ -2,27 +2,33 @@ module Lauth module Determination class Base - def type - raise NotImplementedError - end + class << self + def type + raise NotImplementedError + end - def eql?(other) - type == other.to_s - end - alias == eql? + def eql?(other) + type == other.to_s + end + alias == eql? - def to_s - to_str - end + def to_s + to_str + end - def to_str - type - end + def to_str + type + end - def inspect - to_s + def inspect + to_s + end end + private + + def new; end + end end end diff --git a/lauth/lib/lauth/determination/denied.rb b/lauth/lib/lauth/determination/denied.rb index 1a5cad70..95dc3304 100644 --- a/lauth/lib/lauth/determination/denied.rb +++ b/lauth/lib/lauth/determination/denied.rb @@ -4,7 +4,7 @@ class Denied < Base DENIED = "denied" - def type + def self.type DENIED end diff --git a/lauth/lib/lauth/ops/authorize.rb b/lauth/lib/lauth/ops/authorize.rb index 88683076..42dfef10 100644 --- a/lauth/lib/lauth/ops/authorize.rb +++ b/lauth/lib/lauth/ops/authorize.rb @@ -14,9 +14,9 @@ def self.call(request:) def call determination = if grant_repo.for_user_and_uri(request.user, request.uri).any? - Determination::Allowed.new + Determination::Allowed else - Determination::Denied.new + Determination::Denied end Lauth::Access::Result.new(determination:) end diff --git a/lauth/spec/determination_spec.rb b/lauth/spec/determination_spec.rb index d2dbead7..96721b4a 100644 --- a/lauth/spec/determination_spec.rb +++ b/lauth/spec/determination_spec.rb @@ -9,10 +9,6 @@ expect(determination).to eq string.to_sym end - it "equals itself" do - expect(determination).to eq determination.class.new - end - it "prints as a string" do expect(determination.to_s).to eql string end @@ -24,12 +20,12 @@ end describe Lauth::Determination::Allowed do - let(:determination) { Lauth::Determination::Allowed.new } + let(:determination) { Lauth::Determination::Allowed} include_examples "a determination", "allowed" end describe Lauth::Determination::Denied do - let(:determination) { Lauth::Determination::Denied.new } + let(:determination) { Lauth::Determination::Denied} include_examples "a determination", "denied" end diff --git a/lauth/spec/ops/authorize_spec.rb b/lauth/spec/ops/authorize_spec.rb index 7a553e35..d8eda5d6 100644 --- a/lauth/spec/ops/authorize_spec.rb +++ b/lauth/spec/ops/authorize_spec.rb @@ -20,7 +20,7 @@ result = Lauth::Ops::Authorize.call(request: request) - expect(result.determination).to eq Lauth::Determination::Denied.new + expect(result.determination).to eq Lauth::Determination::Denied end end @@ -38,7 +38,7 @@ result = Lauth::Ops::Authorize.new(request: request).call - expect(result.determination).to eq Lauth::Determination::Allowed.new + expect(result.determination).to eq Lauth::Determination::Allowed end end end diff --git a/lauth/spec/requests/authorized_spec.rb b/lauth/spec/requests/authorized_spec.rb index 6456c63b..3db2f2fb 100644 --- a/lauth/spec/requests/authorized_spec.rb +++ b/lauth/spec/requests/authorized_spec.rb @@ -18,7 +18,7 @@ get "/authorized", {user: "lauth-allowed", uri: "/restricted-by-username/"} body = JSON.parse(last_response.body, symbolize_names: true) - expect(body).to eq({determination: Lauth::Determination::Allowed.new}) + expect(body).to eq({determination: Lauth::Determination::Allowed}) end end end