Sha256: 43e23f84b9cf36a940a397828a843676d96a91e83e0bc12911c8a363c650f759

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Emotions
  module Emotional
    extend ActiveSupport::Concern

    included do
      has_many :emotions, as: :emotional, class_name: 'Emotions::Emotion'

      Emotions.emotions.each do |emotion|
        define_emotion_methods(emotion)
      end
    end

    def _emotions_about(emotive)
      self.emotions.where(emotive_id: emotive.id, emotive_type: emotive.class.name)
    end

    def emotions_about(emotive)
      _emotions_about(emotive).map(&:emotion).map(&:to_sym)
    end

    def express!(emotion, emotive)
      send("#{emotion}_about!", emotive)
    end

    def no_longer_express!(emotion, emotive)
      send("no_longer_#{emotion}_about!", emotive)
    end

    module ClassMethods
      def define_emotion_methods(emotion)
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{emotion}_about?(emotive)
            #{emotion}_about(emotive).exists?
          end

          def #{emotion}_about?(emotive)
            #{emotion}_about(emotive).exists?
          end

          def #{emotion}_about!(emotive)
            #{emotion}_about(emotive).first_or_create!
          end

          def no_longer_#{emotion}_about!(emotive)
            #{emotion}_about(emotive).destroy_all
          end

          def #{emotion}_about(emotive)
            _emotions_about(emotive).where(emotion: #{emotion.to_s.inspect})
          end
        RUBY
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
emotions-0.1.3 lib/emotions/emotional.rb
emotions-0.1.2 lib/emotions/emotional.rb
emotions-0.1.1 lib/emotions/emotional.rb