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
Properties on this object that are arrays.
@api private @return [{String => Array<String, Object>}]
If the namespace this object represents had a value, it is available here @return [String, nil]
Regular properties on this object
@api private @return [{String => String, Object}]
Public Class Methods
Source
# File lib/open_graph_reader/object.rb, line 19 def self.included base base.extend DSL end
@private
Source
# File lib/open_graph_reader/object.rb, line 40 def initialize @properties = {} @children = Hash.new { |h, k| h[k] = [] } end
Create a new object. If your class overrides this don’t forget to call super
.
Public Instance Methods
Source
# 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
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]
Source
# 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
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.
Source
# File lib/open_graph_reader/object.rb, line 58 def content= value value = self.class.content_processor.call(value) @content = value end
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
Source
# File lib/open_graph_reader/object.rb, line 49 def property? name self.class.available_properties.include? name.to_s end
Whether this object has the given property
@param [#to_s] name @return [Bool]
Source
# File lib/open_graph_reader/object.rb, line 92 def to_s content || super end
Returns {#content} if available.
@return [String]