Class: TopHat::TwitterCardHelper::TwitterCardGenerator
- Inherits:
-
Object
- Object
- TopHat::TwitterCardHelper::TwitterCardGenerator
- Includes:
- ActionView::Helpers
- Defined in:
- lib/tophat/twitter_card.rb
Instance Attribute Summary collapse
-
#card_data ⇒ Object
readonly
Returns the value of attribute card_data.
Instance Method Summary collapse
- #add_nested_attributes(method, &block) ⇒ Object
-
#initialize(type) {|_self| ... } ⇒ TwitterCardGenerator
constructor
A new instance of TwitterCardGenerator.
-
#method_missing(method, *args, &block) ⇒ Object
:nodoc.
- #render ⇒ Object
Constructor Details
#initialize(type) {|_self| ... } ⇒ TwitterCardGenerator
Returns a new instance of TwitterCardGenerator.
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
34 35 36 37 38 |
# File 'lib/tophat/twitter_card.rb', line 34 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_data ⇒ Object (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
27 28 29 30 31 32 |
# File 'lib/tophat/twitter_card.rb', line 27 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 |
#render ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tophat/twitter_card.rb', line 16 def render output = ActiveSupport::SafeBuffer.new output << tag(:meta, :name => 'twitter:card', :value => @type) @card_data.each do |key, value| output << "\n".html_safe output << tag(:meta, :name => "twitter:#{key}", :value => value) end output << "\n".html_safe unless @card_data.empty? output end |