module OpenGraphReader::Object::DSL

This module provides the methods to define new types and properties, as well as setting other metadata necessary to describe an object, such as its namespace.

Attributes

content_processor[R]

The processor for the content attribute.

@api private @return [Proc]

Public Class Methods

define_type(name, &processor) click to toggle source

@!macro [attach] define_type

@!method $1(name, options={})
  @!macro define_type_description

Defines a new DSL method for modeling a new type

@yield convert and validate @yieldparam [::Object] value the value to be converted and validated @yieldparam [Array<::Object>] *args any additional arguments @yieldparam [{Symbol => Bool, Class, Array<String>}] options the options hash as last parameter

# File lib/open_graph_reader/object/dsl.rb, line 32
def self.define_type(name, &processor)
  processors[name] = processor

  define_method(name) do |name, *args|
    options = args.pop if args.last.is_a? Hash
    options ||= {}

    register_property name, options
    register_verticals name, options[:verticals]

    if options[:collection]
      define_collection name, options
    else
      define_single name, options, args, processor
    end
  end
end
processors() click to toggle source

A map from type names to processing blocks.

@api private @return [{Symbol => Proc}]

# File lib/open_graph_reader/object/dsl.rb, line 158
def self.processors
  @processors ||= {}
end

Public Instance Methods

available_properties() click to toggle source

The list of defined properties on this object.

@return [Array<String>]

# File lib/open_graph_reader/object/dsl.rb, line 143
def available_properties
  @available_properties ||= []
end
content(type, *args) click to toggle source

@overload content type, *args, options={}

Set the type for the content attribute

@param [Symbol] type one of the registered types.
@param [Array<Object>] args Additional parameters for the type
@param [Hash] options
@option options [Bool] :downcase (false) Normalize the contents case to lowercase.
# File lib/open_graph_reader/object/dsl.rb, line 129
def content type, *args
  options = args.pop if args.last.is_a? Hash
  options ||= {}

  @content_processor = proc {|value|
    value.downcase! if options[:downcase]
    options[:to] ||= self
    DSL.processors[type].call(value, *args, options)
  }
end
define_collection(name, options) click to toggle source

@api private

# File lib/open_graph_reader/object/dsl.rb, line 67
def define_collection name, options
  define_method("#{name}s") do
    children[name.to_s]
  end

  define_method(name) do
    value = children[name.to_s].first
    # @todo figure out a sane way to distinguish subobject properties
    value.content if value && value.is_a?(Object)
    value || options[:default]
  end
end
define_single(name, options, args, processor) click to toggle source

@api private

# File lib/open_graph_reader/object/dsl.rb, line 81
def define_single name, options, args, processor
  define_method(name) do
    properties[name.to_s] || options[:default]
  end

  define_method("#{name}=") do |value|
    # @todo figure out a sane way to distinguish subobject properties
    unless value.is_a? Object
      value.downcase! if options[:downcase]
      value = processor.call(value, *args, options)
    end
    properties[name.to_s] = value
  end
end
namespace(*names) click to toggle source

@overload namespace

Get the namespace of this object.

@return [String] A colon separated namespace, for example <tt>og:image</tt>.

@overload namespace(*names)

Set the namespace of this object.

@param [Array<#to_s>] *names The individual parts of the namespace as list
@example
  namespace :og, :image
# File lib/open_graph_reader/object/dsl.rb, line 115
def namespace *names
  return @namespace if names.empty?
  @namespace = names.join(":")
  Registry.register @namespace, self
end
register_property(name, options) click to toggle source

@api private

# File lib/open_graph_reader/object/dsl.rb, line 51
def register_property name, options
  available_properties << name.to_s
  required_properties << name.to_s if options[:required]
  Registry.register [namespace, name].join(":"), options[:to] if options[:to]
end
register_verticals(name, assigned_verticals) click to toggle source

@api private

# File lib/open_graph_reader/object/dsl.rb, line 58
def register_verticals name, assigned_verticals
  [*assigned_verticals].each do |vertical|
    vertical = [namespace, vertical].join(".")
    verticals[vertical] << name.to_s
    Registry.verticals << vertical
  end
end
required_properties() click to toggle source

The list of required properties on this object.

@return [Array<String]

# File lib/open_graph_reader/object/dsl.rb, line 150
def required_properties
  @required_properties ||= []
end
verticals() click to toggle source

A map from vertical names to attributes that belong to them.

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

# File lib/open_graph_reader/object/dsl.rb, line 166
def verticals
  @verticals ||= Hash.new {|h, k| h[k] = [] }
end