Sha256: 873ed978dc529a160cbede1b631dbd379b7529844c01ca923e4920fc7d23ebad

Contents?: true

Size: 830 Bytes

Versions: 1

Compression:

Stored size: 830 Bytes

Contents

require "cgi"
require "has_meta/version"

module HasMeta
  module Extensions
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def has_meta(options = {})
        options.each_pair do |meth, fields|
          define_method("meta_#{meth}") {|*args|
            length = args.first if args.is_a? Array
            length ||= 255
            field = [*fields].detect{|f| send(f).present?}
            return nil if field.nil?
            str = send(field).to_s.strip
            str.gsub!(' ', ' ')
            str.gsub!(/<.*?>/, '')
            str = ::CGI::unescapeHTML(str)
            str = (str[0,length - 3] + '...') if str.size > length
            str
          }
        end
      end
    end 

  end
end

ActiveModel::AttributeMethods.send :include, HasMeta::Extensions

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
has_meta-0.0.1 lib/has_meta.rb