-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
107 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
class User | ||
include Polist::Builder | ||
|
||
builds do |role| | ||
case role | ||
when /admin/ | ||
Admin | ||
end | ||
end | ||
|
||
attr_accessor :role | ||
|
||
def initialize(role) | ||
self.role = role | ||
end | ||
end | ||
|
||
class Admin < User | ||
builds do |role| | ||
role == "super_admin" ? SuperAdmin : Admin | ||
end | ||
|
||
class SuperAdmin < self | ||
def super? | ||
true | ||
end | ||
end | ||
|
||
def super? | ||
false | ||
end | ||
end | ||
|
||
RSpec.describe Polist::Builder do | ||
let(:user) { User.build(role) } | ||
|
||
context "user role" do | ||
let(:role) { "user" } | ||
|
||
it "builds user" do | ||
expect(user.class).to eq(User) | ||
expect(user.role).to eq("user") | ||
end | ||
end | ||
|
||
context "admin role" do | ||
let(:role) { "admin" } | ||
|
||
it "builds admin" do | ||
expect(user.class).to eq(Admin) | ||
expect(user.role).to eq("admin") | ||
expect(user.super?).to eq(false) | ||
end | ||
end | ||
|
||
context "super_admin role" do | ||
let(:role) { "super_admin" } | ||
|
||
it "builds super_admin" do | ||
expect(user.class).to eq(Admin::SuperAdmin) | ||
expect(user.role).to eq("super_admin") | ||
expect(user.super?).to eq(true) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
class Point | ||
include Polist::Struct | ||
|
||
struct :x, :y | ||
end | ||
|
||
RSpec.describe Polist::Struct do | ||
specify "basic usage" do | ||
a = Point.new(15, 25) | ||
expect(a.x).to eq(15) | ||
expect(a.y).to eq(25) | ||
end | ||
|
||
context "too many arguments" do | ||
it "raises exception" do | ||
expect { Point.new(15, 25, 35) }.to raise_error(ArgumentError, "struct size differs") | ||
end | ||
end | ||
|
||
context "only 1 argument" do | ||
it "defaults to nil" do | ||
a = Point.new(15) | ||
expect(a.x).to eq(15) | ||
expect(a.y).to eq(nil) | ||
end | ||
end | ||
end |