Sha256: d7499f2f9f1c453683dd479179c9911015be02e07e76b70ee412246a9d5535e8

Contents?: true

Size: 1.68 KB

Versions: 10

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Playbook
  module PbStarRating
    class StarRating < Playbook::PbKit::Base
      PROPS = %i[configured_classname
                 configured_data
                 configured_hide_rating
                 configured_id
                 configured_rating].freeze

      def initialize(classname: default_configuration,
                     data: default_configuration,
                     hide_rating: default_configuration,
                     id: default_configuration,
                     rating: default_configuration)
        self.configured_classname = classname
        self.configured_data = data
        self.configured_hide_rating = hide_rating
        self.configured_id = id
        self.configured_rating = rating
      end

      def hide_rating
        true_value(configured_hide_rating, "hide_rating", nil)
      end

      def rating
        rating_options = []
        (0..5).step(0.5) do |number|
          rating_options.push strip_trailing_zero(number)
        end
        one_of_value(configured_rating.to_s, rating_options, "0")
      end

      def star_count
        [*1..rating.to_f]
      end

      def star_full
        (rating.to_f % 1).zero?
      end

      def kit_class
        kit_options = [
          "pb_star_rating_kit",
          hide_rating,
        ]
        kit_options.compact.join("_")
      end

      def to_partial_path
        "pb_star_rating/star_rating"
      end

    private

      def strip_trailing_zero(number)
        number.to_s.sub(/\.?0+$/, "")
      end

      DEFAULT = Object.new
      private_constant :DEFAULT
      def default_configuration
        DEFAULT
      end
      attr_accessor(*PROPS)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
playbook_ui-2.9.6 app/pb_kits/playbook/pb_star_rating/star_rating.rb
playbook_ui-2.9.5 app/pb_kits/playbook/pb_star_rating/star_rating.rb
playbook_ui-2.9.4 app/pb_kits/playbook/pb_star_rating/star_rating.rb
playbook_ui-2.9.3 app/pb_kits/playbook/pb_star_rating/star_rating.rb
playbook_ui-2.9.2 app/pb_kits/playbook/pb_star_rating/star_rating.rb
playbook_ui-2.9.1 app/pb_kits/playbook/pb_star_rating/star_rating.rb
playbook_ui-2.9.0 app/pb_kits/playbook/pb_star_rating/star_rating.rb
playbook_ui-2.8.9 app/pb_kits/playbook/pb_star_rating/star_rating.rb
playbook_ui-2.8.8 app/pb_kits/playbook/pb_star_rating/star_rating.rb
playbook_ui-2.8.7 app/pb_kits/playbook/pb_star_rating/star_rating.rb