Skip to content

Commit

Permalink
Refactor getter/property macros to work properly with Bool types
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Jan 14, 2019
1 parent 01d3cae commit ff60f3a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
22 changes: 22 additions & 0 deletions spec/std/object_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ private class TestObject
def self.getter12_value=(@@getter12_value)
end

@getter13_counter = 0
getter(getter13) { @getter13_counter += 1; false }

setter setter1
setter setter2 : Int32
setter setter3 : Int32 = 3
Expand All @@ -58,6 +61,9 @@ private class TestObject
property(property11) { 11 }
property property12 : Int32 { 10 + 2 }

@property13_counter = 0
property(property13) { @property13_counter += 1; false }

def initialize
@getter1 = 1
@getter2 = 2
Expand Down Expand Up @@ -205,6 +211,14 @@ describe Object do
obj2 = TestObject.new
obj2.getter12.should eq(24)
end

# TODO: enable after 0.27.1
pending "defines lazy getter with block returning false" do
obj = TestObject.new
obj.@getter13_counter.should eq(0)
3.times { obj.getter13.should be_false }
obj.@getter13_counter.should eq(1)
end
end

describe "getter!" do
Expand Down Expand Up @@ -330,6 +344,14 @@ describe Object do
obj.property12 = 13
obj.property12.should eq(13)
end

# TODO: enable after 0.27.1
pending "defines lazy property with block returning false" do
obj = TestObject.new
obj.@property13_counter.should eq(0)
3.times { obj.property13.should be_false }
obj.@property13_counter.should eq(1)
end
end

describe "property!" do
Expand Down
36 changes: 30 additions & 6 deletions src/object.cr
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,11 @@ class Object
# ```
# class Person
# def {{method_prefix}}birth_date
# {{var_prefix}}birth_date ||= Time.now
# if (_birth_date = {{var_prefix}}birth_date).nil?
# {{var_prefix}}birth_date = Time.now
# else
# _birth_date
# end
# end
# end
# ```
Expand All @@ -355,11 +359,19 @@ class Object
{{var_prefix}}\{{name.var.id}} : \{{name.type}}?

def {{method_prefix}}\{{name.var.id}}
{{var_prefix}}\{{name.var.id}} ||= \{{yield}}
if (_\{{name.var.id}} = {{var_prefix}}\{{name.var.id}}).nil?
{{var_prefix}}\{{name.var.id}} = \{{yield}}
else
_\{{name.var.id}}
end
end
\{% else %}
def {{method_prefix}}\{{name.id}}
{{var_prefix}}\{{name.id}} ||= \{{yield}}
if (_\{{name.id}} = {{var_prefix}}\{{name.id}}).nil?
{{var_prefix}}\{{name.id}} = \{{yield}}
else
_\{{name.id}}
end
end
\{% end %}
\{% else %}
Expand Down Expand Up @@ -790,7 +802,11 @@ class Object
# ```
# class Person
# def {{method_prefix}}birth_date
# {{var_prefix}}birth_date ||= Time.now
# if (_birth_date = {{var_prefix}}birth_date).nil?
# {{var_prefix}}birth_date = Time.now
# else
# _birth_date
# end
# end
#
# def {{method_prefix}}birth_date=({{var_prefix}}birth_date)
Expand All @@ -811,11 +827,19 @@ class Object
{{var_prefix}}\{{name.var.id}} : \{{name.type}}?

def {{method_prefix}}\{{name.var.id}}
{{var_prefix}}\{{name.var.id}} ||= \{{yield}}
if (_\{{name.var.id}} = {{var_prefix}}\{{name.var.id}}).nil?
{{var_prefix}}\{{name.var.id}} = \{{yield}}
else
_\{{name.var.id}}
end
end
\{% else %}
def {{method_prefix}}\{{name.id}}
{{var_prefix}}\{{name.id}} ||= \{{yield}}
if (_\{{name.id}} = {{var_prefix}}\{{name.id}}).nil?
{{var_prefix}}\{{name.id}} = \{{yield}}
else
_\{{name.id}}
end
end
\{% end %}
\{% else %}
Expand Down

0 comments on commit ff60f3a

Please sign in to comment.