Sha256: dff8074f407a79d04c6f16b577406b27583647f78da5481abfeeb0eae7e47c65

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module Emotions
  module Emotional
    extend ActiveSupport::Concern

    module ClassMethods
      def acts_as_emotional
        has_many :emotions, as: :emotional, class_name: 'Emotions::Emotion'

        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          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
        RUBY

        Emotions.emotions.each do |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
            end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
emotions-0.1 lib/emotions/emotional.rb