Module: FiveStar::Rateable

Defined in:
lib/five-star/rateable.rb

Overview

A module to be included to enhance an object with the interface below.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extends base class or a module with Rateable methods

Parameters:

  • base (Object)

    the object to mix in this Rateable module

Returns:

  • (undefined)


12
13
14
# File 'lib/five-star/rateable.rb', line 12

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#configurationFiveStar::Configuration

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reference to Configuration used for this rateable instance. Delegates to class.

Returns:



114
115
116
# File 'lib/five-star/rateable.rb', line 114

def configuration
  self.class.configuration
end

#rateable_nameString

The name of the object that is rateable. This may be used by raters when generating descriptions. This can be overridden to provide a better response, otherwise is the class name.

Returns:

  • (String)

    name of the class



105
106
107
# File 'lib/five-star/rateable.rb', line 105

def rateable_name
  self.class.name
end

#ratingFloat

Return the rating given to the rateable object by calculating based on set raters and their configuration.

Examples:

film = Film.new
film.rating # => 6.0

Returns:

  • (Float)

    rating calculated by set raters for the object

Raises:

  • (FiveStar::RatingError)

    raises error if any raters return either rating or weighting that is outside of configuration bounds.



78
79
80
# File 'lib/five-star/rateable.rb', line 78

def rating
  rating_calculator.rate(self.class.configuration, raters)
end

#rating_descriptionsArray

Return the rating description for each rater given to the rateable object. These are returned in the order in which the rating classes were defined in rate_with.

Examples:

film = Film.new
film.rating_descriptions # => ["The film Alien was rated 8 for gore", ...]

Returns:

  • (Array)

    list of descriptions from each rater



94
95
96
# File 'lib/five-star/rateable.rb', line 94

def rating_descriptions
  raters.map { |rater| rater.description }
end