Skip to content

Instantly share code, notes, and snippets.

@skystrife
Created April 21, 2012 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skystrife/2434581 to your computer and use it in GitHub Desktop.
Save skystrife/2434581 to your computer and use it in GitHub Desktop.
Add chained attr accessors using coffeescript classes
# utility that allows us to defined chained attr accessors on classes
Function::chain_accessor = (attrs...) ->
for attr in attrs
# add the function to the prototype. we wrap the function in a closure
# to ensure that "attr" keeps its value so we are accessing the right
# attribute when we call this function later
@::[attr] = do (attr) ->
(data) ->
return @["_#{attr}"] unless data?
@["_#{attr}"] = data
return @
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment