Sha256: 0ec428bcfc051b57d6efc4f9e4266943d59e26b075d57f4d3d83ea4789477397
Contents?: true
Size: 1.35 KB
Versions: 1
Compression:
Stored size: 1.35 KB
Contents
require 'active_support/all' module Embeddable::Concerns module Embeddable extend ActiveSupport::Concern SERVICES = { youtube: [ %r{^https?://(?:(?:www|m)\.)?youtube\.com/watch\?v=([^&]+)}, %r{^https?://(?:(?:www|m)\.)?youtu\.be/([^?]+)} ] } module ClassMethods # Denotes an embeddable for given name # # Options: # :from (source property for this embeddable, required) # # Example: # # class Post # include Concerns::Embeddable # embeddable :video, from: :video_url # end # def embeddable(name, options = {}) source = options.fetch :from define_method "#{name}_type" do url = send(source) return if url.blank? SERVICES.map do |service, patterns| service if patterns.any? { |pattern| url[pattern] } end.compact.first end define_method "#{name}_id" do url = send(source) return if url.blank? SERVICES.map do |service, patterns| patterns.map { |pattern| url[pattern, 1] } end.flatten.compact.first end SERVICES.each do |service, pattern| define_method "#{name}_on_#{service}?" do send("#{name}_type") == service end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
embeddable-0.0.1 | lib/embeddable/concerns/embeddable.rb |