Class: Ogg::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/ogg/generator.rb

Defined Under Namespace

Classes: InvalidBasicProperty, Property, StructuredProperty

Constant Summary collapse

BASIC_PROPERTIES =
%w[title type url image]
OPTIONAL_PROPERTIES =
%w[
  audio description determiner locale
  site_name video
]
STRUCTURED_PROPERTIES =
{
  :image  => %w[url secure_url type width height],
  :video  => %w[secure_url type width height],
  :audio  => %w[secure_url type],
  :locale => %w[alternate]
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Generator

Returns a new instance of Generator.



122
123
124
125
# File 'lib/ogg/generator.rb', line 122

def initialize(options = {}, &block)
  @raise = options[:raise]
  instance_eval(&block) if block_given?
end

Class Method Details

.define_properties(names, options = {}) ⇒ Object



98
99
100
# File 'lib/ogg/generator.rb', line 98

def define_properties(names, options = {})
  names.each{|name| define_property(name, options) }
end

.define_property(name, options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ogg/generator.rb', line 80

def define_property(name, options = {})
  define_method(name) do
    properties = instance_variable_get(:@properties)
    return properties[name] if properties.is_a?(Hash) and properties[name]
    properties = instance_variable_set(:@properties, {}) unless properties.is_a?(Hash)
    property = properties[name] = Property.new(name)
    options[:children].each do |child|
      options_for_child = {:parent => name, :alternative => child == "url"}
      property.add_child(StructuredProperty.new(child, options_for_child))
    end if options[:children]
    property
  end
  define_method("#{name}=") do |value|
    property = __send__(name)
    property.content = value
  end
end

Instance Method Details

#basic_propertiesObject



127
128
129
130
131
132
133
# File 'lib/ogg/generator.rb', line 127

def basic_properties
  BASIC_PROPERTIES.map{|name|
    property_instance = __send__(name)
    raise_invalid_basic_property(name) if @raise and !property_instance.content
    property_instance.meta_tag
  }.delete_if(&:empty?).join("\n")
end

#htmlObject



142
143
144
145
146
147
148
149
# File 'lib/ogg/generator.rb', line 142

def html
  @properties ||= {}
  if @raise
    diff = BASIC_PROPERTIES - @properties.keys
    raise_invalid_basic_property(diff * ", ") unless diff.empty?
  end
  @properties.values.map(&:meta_tag).delete_if(&:empty?).join("\n")
end

#optional_propertiesObject



135
136
137
138
139
140
# File 'lib/ogg/generator.rb', line 135

def optional_properties
  OPTIONAL_PROPERTIES.map{|name|
    property_instance = __send__(name)
    property_instance.meta_tag
  }.delete_if(&:empty?).join("\n")
end

#raise_invalid_basic_property(name) ⇒ Object



151
152
153
# File 'lib/ogg/generator.rb', line 151

def raise_invalid_basic_property(name)
  raise InvalidBasicProperty, "`#{name}` should be set."
end