Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Allow defaults to reference methods inside class (#320)

authored by

Ricky Chilcott and committed by
GitHub
7653ee84 d6e73754

+15 -3
+3 -3
lib/literal/property.rb
··· 87 87 RUBY_KEYWORDS[@name] || @name.name 88 88 end 89 89 90 - def default_value 90 + def default_value(receiver) 91 91 case @default 92 - when Proc then @default.call 92 + when Proc then receiver.instance_exec(&@default) 93 93 else @default 94 94 end 95 95 end ··· 190 190 escaped_name << 191 191 "\n " << 192 192 escaped_name << 193 - " = __property__.default_value\n end\n" 193 + " = __property__.default_value(self)\n end\n" 194 194 end 195 195 196 196 def generate_initializer_check_type(buffer = +"")
+12
test/properties.test.rb
··· 93 93 prop :block, Proc, :&, reader: :public, default: -> { proc { "Hello" } } 94 94 end 95 95 96 + class WithContextualDefault 97 + extend Literal::Properties 98 + prop :hello, String, reader: :private, default: "Hello".freeze 99 + prop :world, String, reader: :private, default: "World".freeze 100 + prop :combined, String, reader: :public, default: -> { "#{hello} #{world}" } 101 + end 102 + 96 103 class WithNilableType 97 104 extend Literal::Properties 98 105 prop :name, Literal::Types::NilableType.new(String), :positional ··· 138 145 139 146 object = WithDefaultBlock.new { "World" } 140 147 assert_equal object.block.call, "World" 148 + end 149 + 150 + test "default value (as a proc) executes in the context of the receiver" do 151 + object = WithContextualDefault.new 152 + assert_equal object.combined, "Hello World" 141 153 end 142 154 143 155 test "properties are enumerable" do