Class: TinyZap::OpenGraph::Base

Inherits:
Object
  • Object
show all
Defined in:
app/models/tinyzap/open_graph/base.rb

Overview

Provides a way to specify OpenGraph keys for an OpenGraphModel, which can later be iterated upon to generate OpenGraph meta tags.

Direct Known Subclasses

Image, Model, Twitter

Defined Under Namespace

Classes: Property

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Base

Allows for the object to be initialized with args like ‘Image.new(url: “”, alt: “Logo image”)`.



21
22
23
24
25
26
27
# File 'app/models/tinyzap/open_graph/base.rb', line 21

def initialize(**kwargs)
  kwargs.each do |kwarg, value|
    self.send("#{kwarg}=", value)
  rescue NoMethodError
    raise NoMethodError, "#{kwarg.inspect} is not a property of #{self.inspect}"
  end
end

Class Method Details

.propertiesObject

Array where properties are registered.



37
38
39
# File 'app/models/tinyzap/open_graph/base.rb', line 37

def self.properties
  @properties ||= []
end

.property(*args, **kwargs) ⇒ Object

DSL for creating properties when defining a model.



30
31
32
33
34
# File 'app/models/tinyzap/open_graph/base.rb', line 30

def self.property(*args, **kwargs)
  property = Property.new *args, **kwargs
  attr_accessor property.name
  self.properties.append property
end

Instance Method Details

#propertiesObject

Iterates through the properties and also gets their value.



42
43
44
45
46
47
48
# File 'app/models/tinyzap/open_graph/base.rb', line 42

def properties
  Enumerator.new do |y|
    self.class.properties.each do |property|
      y << [ property, (self.send(property.name) || property.default) ]
    end
  end
end