Skip to content

Commit 226ce68

Browse files
committed
Enable attribute access via message selectors again
1 parent 1d52f1b commit 226ce68

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

fluent/_nodes.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,18 @@ FTL.Message._name = "Message"
116116
function FTL.Message:_init (ast, resource)
117117
self.attributes = setmetatable({}, { map = {} })
118118
self:super(ast, resource)
119+
-- Work around Penlight #307
119120
-- self:catch(self.get_attribute)
121+
self:_patch_init()
120122
return self
121123
end
122124

125+
function FTL.Message:_patch_init ()
126+
if not type(rawget(getmetatable(self), "__index")) ~= "function" then
127+
self:catch(function(_, attribute) return self:get_attribute(attribute) end)
128+
end
129+
end
130+
123131
function FTL.Message:set_attribute (attribute)
124132
local attributes = rawget(self, "attributes")
125133
local id = attribute.id.name

spec/fluent_spec.lua

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,13 @@ foo =
127127
assert.error(function () return bundle["apple"]:format() end)
128128
end)
129129

130-
pending('attributes can be accessed as properties', function ()
130+
it('attributes can be accessed as properties', function ()
131131
assert.same("qux", bundle["bar.bax"]())
132+
assert.same("qux", en["bar.bax"]())
132133
assert.same("qux", bundle.bar["bax"]())
134+
assert.same("qux", en.bar["bax"]())
133135
assert.same("qux", bundle.bar.bax())
136+
assert.same("qux", en.bar.bax())
134137
end)
135138

136139
it('preserves messages when messages are added', function ()
@@ -157,22 +160,34 @@ foo =
157160
assert.same("baz", bundle.bar())
158161
assert.same("baz", en.bar())
159162
assert.same("baz", bundle:format("bar"))
163+
assert.same("baz", en:format("bar"))
160164
assert.same("qux", bundle:get_message("bar"):get_attribute("bax")())
161-
-- assert.same("qux", bundle["bar.bax"]())
162-
-- assert.same("qux", bundle.bar.bax())
165+
assert.same("qux", en:get_message("bar"):get_attribute("bax")())
166+
assert.same("qux", bundle["bar.bax"]())
167+
assert.same("qux", en["bar.bax"]())
168+
assert.same("qux", bundle.bar.bax())
169+
assert.same("qux", en.bar.bax())
163170
bundle:add_messages("aa = bb")
164-
-- assert.same("qux", bundle["bar.bax"]())
165-
-- assert.same("qux", bundle.bar:get_attribute("bax")())
166171
assert.same("baz", bundle.bar())
167172
assert.same("baz", en.bar())
168173
assert.same("baz", bundle:format("bar"))
174+
assert.same("baz", en:format("bar"))
169175
assert.same("qux", bundle:get_message("bar"):get_attribute("bax")())
176+
assert.same("qux", en:get_message("bar"):get_attribute("bax")())
177+
assert.same("qux", bundle["bar.bax"]())
178+
assert.same("qux", en["bar.bax"]())
179+
-- TODO Penlight hack doesn't work around this?
180+
-- assert.same("qux", bundle.bar.bax())
181+
-- assert.same("qux", en.bar.bax())
170182
bundle:add_messages("bar = rebar")
171183
assert.same("rebar", bundle.bar())
172184
assert.same("rebar", en.bar())
173185
assert.same("rebar", bundle:format("bar"))
186+
assert.same("rebar", en:format("bar"))
174187
assert.error(function() return bundle:get_message("bar"):get_attribute("bax")() end)
175-
-- assert.same("qux", bundle.bar.bax())
188+
assert.error(function() return en:get_message("bar"):get_attribute("bax")() end)
189+
assert.error(function() return bundle.bar.bax() end)
190+
assert.error(function() return en.bar.bax() end)
176191
end)
177192

178193
it('can be called', function ()

0 commit comments

Comments
 (0)