Skip to content

Need a way to deal with objects changing state (i.e. arrays) #24

Description

@catmando

For example

define_state(:foo) { ['joe', 'fred'] }

def add_friend(friend) 
  foo << friend
end

This does not work as foo << friend updates the array object, but there is no way to know that the foo state has been updated.

I have implemented a couple of solutions, and am interested in which people would prefer:

  1. define foo! method that will return an object that wrap the current value of foo, and observe any changes to that object. So you can write
def add_friend(friend)
  foo! << friend
end
  1. define foo! method that simple does foo = foo to indicate a state change. This would return self so that the methods could be chained
def add_friend(friend)
  foo << friend; foo!
end

or

def add_friend(friend)
  foo << friend
ensure
  foo!.bar!
end

2b) In addition the Proc class can be updated so this works

lambda { |new_friend| foo << new_friend }.foo! 

Just looking for people's input, preferences, or other ideas!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions