Class: FiveStar::BaseRater

Inherits:
Object
  • Object
show all
Defined in:
lib/five-star/base_rater.rb

Overview

Base implementation of a class to give a rating, weighting and description to a rateable instance.

You are expected to subclass this class and override the default implementation with your own implementation.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rateable) ⇒ BaseRater

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.

Create a new instance of rater

Parameters:

  • rateable (Object)

    the instance of the Object being rated



55
56
57
# File 'lib/five-star/base_rater.rb', line 55

def initialize(rateable)
  @rateable = rateable
end

Class Method Details

.build(rateable) ⇒ Object

Called to build a new instance of the rater with the given object being rated.

Parameters:

  • rateable (Object)

    the instance of the Object being rated

Returns:

  • (Object)

    the instance of Rating class created ready to be used



16
17
18
# File 'lib/five-star/base_rater.rb', line 16

def self.build(rateable)
  new(rateable)
end

.rating_weight(weighting) ⇒ undefined

Set the weighting for this rating classifcation class. This should a valid floating point within the scale configured.

Examples:

class GoreRater < FiveStar.base_rater
  rating_weight 0.4
  # ...
end

Parameters:

  • weighting (Float)

    the weighting value

Returns:

  • (undefined)


35
36
37
# File 'lib/five-star/base_rater.rb', line 35

def self.rating_weight(weighting)
  @weighting = weighting
end

.weightingFloat

Return the weighting value for this rating classifcation class.

Returns:

  • (Float)

    the weighting value. Defaults to 1.0



45
46
47
# File 'lib/five-star/base_rater.rb', line 45

def self.weighting
  @weighting ||= Configuration::DEFAULT_WEIGHTING
end

Instance Method Details

#descriptionString

Return the rating description for the rater given to the rateable object. Override this method to customise the message.

Examples:

class GoreRater < FiveStar.base_rater
  rating_weight 0.4

  def description
    "The film #{film.title} has #{film.number_of_swear_words} and was rated at #{rating}"
  end
end

rater.description # => "The film Alien was rated 8 for gore"

Returns:

  • (String)

    the description



77
78
79
# File 'lib/five-star/base_rater.rb', line 77

def description
  "#{self.class} rated #{rateable_name} at #{rating} with weighting of #{weighting}"
end

#ratingFloat

Return the rating for the rater given to the rateable object. You are expected to override this method to perform your own calculation for the rating based on your own criteria. If this is an expensive operation then the result should be cached as this method can be called more than once, for example by the description method.

Examples:

class GoreRater < FiveStar.base_rater
  rating_weight 0.4

  def rating
    # count the pints of blood spilt in the film and return a rating
    if film.blood_spilt == :a_lot
      10
    elsif film.blood_spilt == :a_little
      5
    else
      0
    end
  end
end

rater.rating # => 6

Returns:

  • (Float)

    the rating value calculated. Defaults to minimum rating value unless overridden

Raises:

  • (FiveStar::RatingError)

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



112
113
114
# File 'lib/five-star/base_rater.rb', line 112

def rating
  configuration.min_rating
end

#weightingFloat

Return the weighting value for this rating classifcation class.

Returns:

  • (Float)

    the weighting value



122
123
124
# File 'lib/five-star/base_rater.rb', line 122

def weighting
  self.class.weighting
end