Module: OpenGraphReader::Object

Included in:
Article, Book, Music, Og, OpenGraphReader::Og::Audio, OpenGraphReader::Og::Image, OpenGraphReader::Og::Locale, OpenGraphReader::Og::Video, Profile, Video
Defined in:
lib/open_graph_reader/object.rb,
lib/open_graph_reader/object/dsl.rb,
lib/open_graph_reader/object/registry.rb,
lib/open_graph_reader/object/dsl/types.rb

Overview

This module provides the base functionality for all OpenGraph objects and makes the DSL methods for describing them available when included.

Examples:

Define a new object

class MyObject
  include OpenGraphReader::Object

   namespace :my, :object
   content :string
   string :name, required: true
end

Defined Under Namespace

Modules: DSL Classes: Registry

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#children{String => Array<String, Object>} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Properties on this object that are arrays.

Returns:

  • ({String => Array<String, Object>})


37
38
39
# File 'lib/open_graph_reader/object.rb', line 37

def children
  @children
end

#contentString?

If the namespace this object represents had a value, it is available here

Returns:

  • (String, nil)


25
26
27
# File 'lib/open_graph_reader/object.rb', line 25

def content
  @content
end

#properties{String => String, Object} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Regular properties on this object

Returns:

  • ({String => String, Object})


31
32
33
# File 'lib/open_graph_reader/object.rb', line 31

def properties
  @properties
end

Instance Method Details

#[](name) ⇒ String, Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

right error?

Get a property on this object.

Parameters:

Returns:

Raises:



70
71
72
73
# File 'lib/open_graph_reader/object.rb', line 70

def [] name
  raise UndefinedPropertyError, "Undefined property #{name} on #{inspect}" unless property? name
  public_send name.to_s
end

#[]=(name, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set the property to the given value.

Parameters:

Raises:



81
82
83
84
85
86
87
# File 'lib/open_graph_reader/object.rb', line 81

def []= name, value
  if property?(name)
    public_send "#{name}=", value
  elsif OpenGraphReader.config.strict
    raise UndefinedPropertyError, "Undefined property #{name} on #{inspect}"
  end
end

#initializeObject

Create a new object. If your class overrides this don’t forget to call super.



40
41
42
43
# File 'lib/open_graph_reader/object.rb', line 40

def initialize
  @properties = {}
  @children = Hash.new {|h, k| h[k] = [] }
end

#property?(name) ⇒ Bool

Whether this object has the given property

Parameters:

Returns:

  • (Bool)


49
50
51
# File 'lib/open_graph_reader/object.rb', line 49

def property? name
  self.class.available_properties.include? name.to_s
end

#to_sString

Returns #content if available.

Returns:

  • (String)


92
93
94
# File 'lib/open_graph_reader/object.rb', line 92

def to_s
  content || super
end