Skip to content

Commit

Permalink
Merge 135c2a0 into e9b709e
Browse files Browse the repository at this point in the history
  • Loading branch information
tycooon committed Feb 3, 2019
2 parents e9b709e + 135c2a0 commit a03fc6f
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ source "https://rubygems.org"

# Specify your gem's dependencies in polist.gemspec
gemspec

gem "rubocop-config-umbrellio", github: "umbrellio/code-style"
5 changes: 2 additions & 3 deletions lib/polist.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "polist/builder"
require "polist/service"
require "polist/struct"
require "polist/version"

module Polist
end
32 changes: 32 additions & 0 deletions lib/polist/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require "uber/builder"

module Polist
module Builder
def self.included(base)
base.include(Uber::Builder)
base.extend(ClassMethods)
end

module ClassMethods
# Recursively runs class builders on class until no builders on that class found
# or some builder returns the class itself
def build_klass(*args)
klass = self

loop do
new_klass = klass.build!(klass, *args)
break if new_klass == klass
klass = new_klass
end

klass
end

def build(*args)
build_klass(*args).new(*args)
end
end
end
end
20 changes: 20 additions & 0 deletions lib/polist/struct.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Polist
module Struct
module ClassMethods
def struct(*attrs)
attr_accessor(*attrs)

define_method(:initialize) do |*args|
raise ArgumentError, "struct size differs" if args.length > attrs.length
attrs.zip(args).each { |attr, val| public_send(:"#{attr}=", val) }
end
end
end

def self.included(base)
base.extend(ClassMethods)
end
end
end
4 changes: 2 additions & 2 deletions polist.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

lib = File.expand_path("../lib", __FILE__)
lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "polist/version"

Expand All @@ -21,12 +21,12 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "activemodel", ">= 3.0"
spec.add_runtime_dependency "plissken", ">= 0.3"
spec.add_runtime_dependency "tainbox"
spec.add_runtime_dependency "uber"

spec.add_development_dependency "bundler"
spec.add_development_dependency "coveralls"
spec.add_development_dependency "pry"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rubocop-config-umbrellio"
spec.add_development_dependency "simplecov"
end
67 changes: 67 additions & 0 deletions spec/polist/builder_spec.rb
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
29 changes: 29 additions & 0 deletions spec/polist/struct_spec.rb
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

0 comments on commit a03fc6f

Please sign in to comment.