Skip to content

Commit

Permalink
Adds documentation for observable_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
shawn42 committed Jun 7, 2013
1 parent 31b248b commit d618e62
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -30,8 +30,8 @@ The reason I wrote Gamebox is twofold: first, to aid in 48 hour game writing com

To create a new Gamebox project run:

```bash
gamebox zapper
```
$ gamebox zapper
```

This will create the directory structure and needed files to get a basic actor up on the screen:
Expand Down Expand Up @@ -61,10 +61,10 @@ zapper

To run your game:

```bash
cd zapper
bundle
rake
```
$ cd zapper
$ bundle
$ rake
```

## Stages
Expand Down
15 changes: 15 additions & 0 deletions lib/gamebox/lib/observable_attributes.rb
Expand Up @@ -24,6 +24,17 @@ def update_attributes(attributes)
end
end

# Adds attributes that are not currently set.
# Takes a list of symbols or a hash of symbols => default values
#
# examples:
#
# foo.has_attributes :x, :y
# # adds x, x=, y, y= methods
# #fires events x_changed, y_changed when the values change
#
# foo.has_attributes x: 5, y: 3
# # this form provides default values if these are new attributes
def has_attributes(*names)
if names.first.is_a? Hash
names.first.each do |name, default|
Expand All @@ -36,6 +47,8 @@ def has_attributes(*names)
end
end

# singular form of has_attributes
# takes the symbol name and an optional default value
def has_attribute(name, value=nil)
@evented_attributes ||= []
unless has_attribute? name
Expand All @@ -48,10 +61,12 @@ def has_attribute(name, value=nil)
end
end

# Returns true if the passed in symbol is an attr on this object
def has_attribute?(name)
@evented_attributes && @evented_attributes.include?(name)
end

# Returns a new hash containing the attr keys and values
def attributes
{}.tap do |atts|
if @evented_attributes
Expand Down

4 comments on commit d618e62

@jasonroelofs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your git commits are slacking! "Document observable_attributes", or should this file be renamed "has_attributes" or the method "observe_attributes"? Seems there may be a name clash here.

@shawn42
Copy link
Owner Author

@shawn42 shawn42 commented on d618e62 Jun 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure I checked this in at 3:30am (cut me some slack) ;) also I like the way include ObservableAttributes reads. The object will have observable attributes once this is included. just my $0.02

I am accepting pull requests ;)

@jasonroelofs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So does the code look like?

class MyActor
    include ObservableAttributes
    has_attributes :x, :y 
end

There's nothing linking one to the other, especially if there's code in between the include and the has_attributes. I would probably rename this to simply observable_attributes :x, :y then it's very obvious.

@shawn42
Copy link
Owner Author

@shawn42 shawn42 commented on d618e62 Jun 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end user of Gamebox does not use the Actor class in that way.
Gamebox sets up Actor:

class Actor
  include ObservableAttributes
end

The consumer of Gamebox just uses has_attributes from the Gamebox DSL.

define_actor :ship do
  has_attributes :x, :y
end

Please sign in to comment.