Class: TopHat::TwitterCardHelper::TwitterCardGenerator

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers
Defined in:
lib/tophat/twitter_card.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) {|_self| ... } ⇒ TwitterCardGenerator

Returns a new instance of TwitterCardGenerator.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
# File 'lib/tophat/twitter_card.rb', line 9

def initialize(type, &block)
  @type = type
  @card_data = {}

  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

:nodoc



37
38
39
40
41
# File 'lib/tophat/twitter_card.rb', line 37

def method_missing(method, *args, &block) #:nodoc
  @card_data ||= {}
  @card_data[method] = args.shift
  add_nested_attributes(method, &block) if block_given?
end

Instance Attribute Details

#card_dataObject (readonly)

Returns the value of attribute card_data.



7
8
9
# File 'lib/tophat/twitter_card.rb', line 7

def card_data
  @card_data
end

Instance Method Details

#add_nested_attributes(method, &block) ⇒ Object



30
31
32
33
34
35
# File 'lib/tophat/twitter_card.rb', line 30

def add_nested_attributes(method, &block)
  image_generator = TwitterCardGenerator.new(method, &block)
  image_generator.card_data.each do |key, value|
    @card_data["#{method}:#{key}"] = value
  end
end

#to_htmlObject Also known as: render



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tophat/twitter_card.rb', line 16

def to_html
  output = ActiveSupport::SafeBuffer.new
  output << tag(:meta, :name => 'twitter:card', :value => @type)
  @card_data = @card_data.delete_if { |k, v| v.nil? }
  @card_data.each do |key, value|
    tag_name = "twitter:#{key}".squeeze(":")
    output << "\n".html_safe
    output << tag(:meta, :name => tag_name, :value => value)
  end
  output << "\n".html_safe unless @card_data.empty?
  output
end