Sha256: ebfd28c9336eb6b72b4222e8082b9db7d10106d052356ecfd4eb826543831e7f

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module Emotions
  module Emotional
    extend ActiveSupport::Concern

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

      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 = #{emotion}_about(emotive).first_or_initialize
            emotion.save!
          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

2 entries across 2 versions & 1 rubygems

Version Path
emotions-0.1.6 lib/emotions/emotional.rb
emotions-0.1.5 lib/emotions/emotional.rb