Skip to content

Commit

Permalink
Entities update properties cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
tredfern committed Feb 6, 2022
1 parent f32037c commit 105288b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions moonpie/entities/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ function Actions.remove(entity)
end

function Actions.updateProperty(entity, property, value, copyValues)
if type(property) == "table" and property.name and property.value then
value = property.value
property = property.name
end

return {
type = Actions.types.UPDATE_PROPERTY,
payload = {
Expand Down
11 changes: 11 additions & 0 deletions moonpie/entities/actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

describe("moonpie.entities.actions", function()
local Actions = require "moonpie.entities.actions"
local Property = require "moonpie.entities.property"

it("can create an add entity action", function()
local entity = {}
Expand Down Expand Up @@ -36,4 +37,14 @@ describe("moonpie.entities.actions", function()
local action = Actions.updateProperty(entity, "name", "Foobar", true)
assert.is_true(action.payload.copyValues)
end)

it("can take a property type to update and will replace the property value", function()
local entity = {}
local prop = Property("foo", "value")
local action = Actions.updateProperty(entity, prop)

assert.equals(entity, action.payload.entity)
assert.equals("foo", action.payload.property)
assert.equals("value", action.payload.value)
end)
end)

0 comments on commit 105288b

Please sign in to comment.