module OpenGraphReader::Object

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

@example Define a new object

class MyObject
  include OpenGraphReader::Object

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

Attributes

children[R]

Properties on this object that are arrays.

@api private @return [{String => Array<String, Object>}]

content[R]

If the namespace this object represents had a value, it is available here @return [String, nil]

properties[R]

Regular properties on this object

@api private @return [{String => String, Object}]

Public Class Methods

included(base) click to toggle source

@private

# File lib/open_graph_reader/object.rb, line 19
def self.included base
  base.extend DSL
end
new() click to toggle source

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

# File lib/open_graph_reader/object.rb, line 40
def initialize
  @properties = {}
  @children = Hash.new {|h, k| h[k] = [] }
end

Public Instance Methods

[](name) click to toggle source

Get a property on this object.

@api private @param [#to_s] name @todo right error? @raise [UndefinedPropertyError] If the requested property is undefined. @return [String, Object]

# 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) click to toggle source

Set the property to the given value.

@api private @param [#to_s] name @param [String, Object] value @raise [UndefinedPropertyError] If the requested property is undefined.

# 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
content=(value) click to toggle source

Set the content for this object in case it is also a property on another object. If a processor is defined, it will be called.

@api private @param [String] value

# File lib/open_graph_reader/object.rb, line 58
def content= value
  value = self.class.content_processor.call(value)
  @content = value
end
property?(name) click to toggle source

Whether this object has the given property

@param [#to_s] name @return [Bool]

# File lib/open_graph_reader/object.rb, line 49
def property? name
  self.class.available_properties.include? name.to_s
end
to_s() click to toggle source

Returns {#content} if available.

@return [String]

Calls superclass method
# File lib/open_graph_reader/object.rb, line 92
def to_s
  content || super
end