Module: Embeddable::ClassMethods

Defined in:
lib/embeddable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#embeddablesObject (readonly)

Returns the value of attribute embeddables.



39
40
41
# File 'lib/embeddable.rb', line 39

def embeddables
  @embeddables
end

Instance Method Details

#embeddable(name, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/embeddable.rb', line 41

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

  define_method "#{name}?" do
    send("#{name}_id") ? true : false
  end

  SERVICES.each do |service, pattern|

    define_method "#{name}_on_#{service}?" do
      send("#{name}_type") == service
    end

  end

  define_method "#{name}_source" do
    source
  end

  @embeddables << name
end