Skip to content
Presenters for DataMapper models
Ruby
Find file
Pull request Compare This branch is 1 commit ahead, 2 commits behind okbreathe:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
test
.document
.gitignore
Gemfile
LICENSE
LICENSE.txt
README.rdoc
Rakefile
VERSION
dm-perspectives.gemspec

README.rdoc

dm-perspectives

Presenters for DataMapper models.

Stop model obesity! We all love 'fat model', 'skinny controller', but sometimes it can be taken too far in an attempt to keep all logic out of the controller or view.

The presentation of a model is something that is definitely related, but absolutely not the responsibility of the model. `dm-perspectives` allows you to decompose presentation into smaller separate classes. If you have methods like `display_name` or `formatted_date` inappropriately stuffed into a model - then this plugin is for you.

@person = Person.new(:first_name => "John", :last_name => "Doe")

@person.perspective(:display).name # => "Doe, John"

Usage

Perspectives can be created either in the model:

require 'dm-perspectives'

class Widget
  include DataMapper::Resource
  property :age, Integer
end

Widget.perspectives # => []

class Widget
  include DataMapper::Resource
  perspective :foo do
    ...
  end
end

Widget.perspectives # => [:foo]

Or in separate class:

class WidgetPerspectives < DataMapper::Perspectives::Base

  perspective :foo do
    def age_in_dog_years
      @resource.age * 7
    end
  end

  perspective :bar do
    ...
  end
end

Widget.perspectives # => [:foo,:bar]

@widget = Widget.new(:age => 10)

@widget.age # => 10
@widget.perspective(:foo).age_in_dog_years # => 70

In order for `dm-perspectives` to find your Perspectives, they must be named in the format `#{ModelName}Perspectives`.

Inside the perspective, the presented object is always accessible through the instance variable `@resource`.

Including Methods

Perspectives, unless inheriting from another perspective will not have any methods by themselves. You can add methods to perspectives by passing an `:include` option when defining a perspective.

perspective :bar, :include => :properties do
  ...
end

This will include object properties as methods of the perspective.

perspective :bar, :include => [:method1, :method2] do
  ...
end

This will include object `methods`, `method1`, and `method2` in the perspective. You can also exclude certain methods by passing in an `:exclude` option (this is only really useful when passing the special `:properties` option).

Inheritence

I usually make a base perspective, and then have subsequent perspectives inherit from it.

class ApplicationPerspectives < DataMapper::Perspectives::Base
end

Contributing to dm-perspectives

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright © 2011 Asher Van Brunt. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.