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>})


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

def children
  @children
end

#contentString?

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

Returns:

  • (String, nil)


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

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})


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

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:



72
73
74
75
# File 'lib/open_graph_reader/object.rb', line 72

def [] name
  raise UndefinedPropertyError, "Undefined property #{name} on #{inspect}" unless has_property? name
  public_send name.to_s #properties[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:



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

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

#has_property?(name) ⇒ Bool

Whether this object has the given property

Parameters:

Returns:

  • (Bool)


51
52
53
# File 'lib/open_graph_reader/object.rb', line 51

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

#initializeObject

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



42
43
44
45
# File 'lib/open_graph_reader/object.rb', line 42

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

#to_sString

Returns #content if available.

Returns:

  • (String)


94
95
96
# File 'lib/open_graph_reader/object.rb', line 94

def to_s
  content || super
end