Skip to content

Commit

Permalink
use understore instead of downcase
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Pleasant-Ryan committed Mar 29, 2016
1 parent 047f6a0 commit 993e38c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
source 'https://rubygems.org'

gem "i18n", "0.7.0"
gem "activesupport-inflector", "0.1.0"

group :development, :test do
gem 'pry', '0.10.1'
gem 'funkify', '0.0.4'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
GEM
remote: https://rubygems.org/
specs:
activesupport-inflector (0.1.0)
coderay (1.1.0)
diff-lcs (1.2.5)
funkify (0.0.4)
i18n (0.7.0)
method_source (0.8.2)
pry (0.10.1)
coderay (~> 1.1.0)
Expand All @@ -28,7 +30,9 @@ PLATFORMS
ruby

DEPENDENCIES
activesupport-inflector (= 0.1.0)
funkify (= 0.0.4)
i18n (= 0.7.0)
pry (= 0.10.1)
rspec (= 3.4.0)

Expand Down
6 changes: 3 additions & 3 deletions lib/monadt/adt.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'pry'
require 'active_support/inflector'

AdtPattern = Struct.new :klass, :lambda

Expand All @@ -13,10 +13,10 @@ def data(*fields)

def decorate_adt(klass)
klass.constants.each do |v|
name = v.to_s.downcase
name = v.to_s.underscore
const = klass.const_get v
klass.constants.each do |is_v|
is_name = is_v.to_s.downcase
is_name = is_v.to_s.underscore
ret = is_v == v
const.class_eval do
define_method "is_#{is_name}?" do ret end
Expand Down
16 changes: 8 additions & 8 deletions spec/monadt/adt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class TestAdt
One = data :foo, :bar
Two = data :foo
Three = data
AndThree = data
end

decorate_adt TestAdt
Expand All @@ -14,22 +14,22 @@ class UseAdts
def adt_func(o)
match o,
with(TestAdt::One, ->(foo, bar) { foo.to_s + bar.to_s }),
with(TestAdt::Three, ->() { 10 }),
with(TestAdt::AndThree, ->() { 10 }),
with(Default, ->() { "default" })
end

def adt_func2(o)
match o,
with(TestAdt::One) { |foo, bar| foo.to_s + bar.to_s },
with(TestAdt::Three) { 10 },
with(TestAdt::AndThree) { 10 },
with(Default) { "default" }
end
end

describe 'Algebraic Data Types' do
let(:v1) { TestAdt.one 1, :five }
let(:v2) { TestAdt.two "hoi" }
let(:v3) { TestAdt.three }
let(:v3) { TestAdt.and_three }
let(:subject) { UseAdts.new }

describe 'proc/block based ADTs' do
Expand All @@ -51,18 +51,18 @@ def adt_func2(o)
it 'supports blocks' do
expect(v1.is_one?).to be true
expect(v1.is_two?).to be false
expect(v1.is_three?).to be false
expect(v1.is_and_three?).to be false
expect(v1.to_s).to eq("One(1, five)")

expect(v2.is_one?).to be false
expect(v2.is_two?).to be true
expect(v2.is_three?).to be false
expect(v2.is_and_three?).to be false
expect(v2.to_s).to eq("Two(hoi)")

expect(v3.is_one?).to be false
expect(v3.is_two?).to be false
expect(v3.is_three?).to be true
expect(v3.to_s).to eq("Three")
expect(v3.is_and_three?).to be true
expect(v3.to_s).to eq("AndThree")
end
end
end

0 comments on commit 993e38c

Please sign in to comment.