Module: OpenGraphReader::Object::DSL

Defined in:
lib/open_graph_reader/object/dsl.rb,
lib/open_graph_reader/object/dsl/types.rb

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_type(name) {|value, *args, options| ... } ⇒ Object

Defines a new DSL method for modeling a new type

Yields:

  • convert and validate

Yield Parameters:

  • value (::Object)

    the value to be converted and validated

  • *args (Array<::Object>)

    any additional arguments

  • options ({Symbol => Bool, Class, Array<String>})

    the options hash as last parameter



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/open_graph_reader/object/dsl.rb', line 31

def self.define_type(name, &processor)
  processors[name] = processor

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

    Registry.register [@namespace, name].join(':'), options[:to] if options[:to]

    if options[:verticals]
      options[:verticals].each do |vertical|
        verticals[[@namespace, vertical].join('.')] << name
      end
    end

    if options[:collection]
      define_method("#{name}s") do
        children[name.to_s]
      end

      define_method(name) do
        # TODO raise if required
        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
    else
      define_method(name) do
        # TODO raise if required
        properties[name.to_s] || options[:default]
      end

      define_method("#{name}=") do |value|
        # TODO: figure out a sane way to distinguish subobject properties
        value = processor.call(value, *args, options) unless value.is_a? Object
        properties[name.to_s] = value
      end
    end
  end
end

.processors{Symbol => Proc}

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.

A map from type names to processing blocks.

Returns:

  • ({Symbol => Proc})


109
110
111
# File 'lib/open_graph_reader/object/dsl.rb', line 109

def self.processors
  @processors ||= {}
end

Instance Method Details

#available_propertiesArray<String>

The list of defined properties on this object.

Returns:

  • (Array<String>)


101
102
103
# File 'lib/open_graph_reader/object/dsl.rb', line 101

def available_properties
  @available_properties ||= []
end

#boolean(name, options = {}) ⇒ Object

Parameters:

  • name (Symbol)

    the name of the property in the current namespace

  • options ({Symbol => Bool, Class, Array<String>}) (defaults to: {})

    additional options

Options Hash (options):

  • :required (Bool) — default: false

    Make the property required.

  • :collection (Bool) — default: false

    This property can occur multiple times.

  • :to (Class)

    This property maps to the given object (optional). belongs to the given verticals of the object (optional).

  • :verticials (Array<String>)

    This property



53
54
55
56
57
58
59
# File 'lib/open_graph_reader/object/dsl/types.rb', line 53

define_type :boolean do |value|
  {'true' => true, 'false' => false, '1' => true, '0' => false}[value].tap {|bool|
    if bool.nil?
      raise InvalidObjectError, "Boolean expected, but was #{value.inspect}"
    end
  }
end

#content(type) ⇒ Object

Set the type for the content attribute

Parameters:

  • type (Symbol)

    one of the registered types.



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

def content type
  @content_processor = DSL.processors[type]
end

#content_processorProc

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.

The processor for the content attribute.

Returns:

  • (Proc)


117
118
119
# File 'lib/open_graph_reader/object/dsl.rb', line 117

def content_processor
  @content_processor || proc {|value| value }
end

#datetime(name, options = {}) ⇒ Object

Parameters:

  • name (Symbol)

    the name of the property in the current namespace

  • options ({Symbol => Bool, Class, Array<String>}) (defaults to: {})

    additional options

Options Hash (options):

  • :required (Bool) — default: false

    Make the property required.

  • :collection (Bool) — default: false

    This property can occur multiple times.

  • :to (Class)

    This property maps to the given object (optional). belongs to the given verticals of the object (optional).

  • :verticials (Array<String>)

    This property



44
45
46
47
48
49
50
# File 'lib/open_graph_reader/object/dsl/types.rb', line 44

define_type :datetime do |value|
  begin
    DateTime.iso8601 value
  rescue ArgumentError => e
    raise InvalidObjectError, "ISO8601 datetime expected, but was #{value.inspect}"
  end
end

#enum(name, allowed, options = {}) ⇒ Object

Parameters:

  • allowed (Array<String>)

    the list of allowed values

  • name (Symbol)

    the name of the property in the current namespace

  • options ({Symbol => Bool, Class, Array<String>}) (defaults to: {})

    additional options

Options Hash (options):

  • :required (Bool) — default: false

    Make the property required.

  • :collection (Bool) — default: false

    This property can occur multiple times.

  • :to (Class)

    This property maps to the given object (optional). belongs to the given verticals of the object (optional).

  • :verticials (Array<String>)

    This property



26
27
28
29
30
31
32
# File 'lib/open_graph_reader/object/dsl/types.rb', line 26

define_type_with_args :enum do |value, allowed|
  unless allowed.include? value
    raise InvalidObjectError, "Expected one of #{allowed.inspect} but was #{value.inspect}"
  end

  value.to_s
end

#float(name, options = {}) ⇒ Object

Parameters:

  • name (Symbol)

    the name of the property in the current namespace

  • options ({Symbol => Bool, Class, Array<String>}) (defaults to: {})

    additional options

Options Hash (options):

  • :required (Bool) — default: false

    Make the property required.

  • :collection (Bool) — default: false

    This property can occur multiple times.

  • :to (Class)

    This property maps to the given object (optional). belongs to the given verticals of the object (optional).

  • :verticials (Array<String>)

    This property



62
63
64
65
66
67
68
# File 'lib/open_graph_reader/object/dsl/types.rb', line 62

define_type :float do |value|
  begin
    Float(value)
  rescue ArgumentError => e
    raise InvalidObjectError, "Float expected, but was #{value.inspect}"
  end
end

#integer(name, options = {}) ⇒ Object

Parameters:

  • name (Symbol)

    the name of the property in the current namespace

  • options ({Symbol => Bool, Class, Array<String>}) (defaults to: {})

    additional options

Options Hash (options):

  • :required (Bool) — default: false

    Make the property required.

  • :collection (Bool) — default: false

    This property can occur multiple times.

  • :to (Class)

    This property maps to the given object (optional). belongs to the given verticals of the object (optional).

  • :verticials (Array<String>)

    This property



35
36
37
38
39
40
41
# File 'lib/open_graph_reader/object/dsl/types.rb', line 35

define_type :integer do  |value|
  begin
    Integer(value)
  rescue  ArgumentError => e
    raise InvalidObjectError, "Integer expected, but was #{value.inspect}"
  end
end

#namespaceString #namespace(*names) ⇒ Object

Overloads:

  • #namespaceString

    Get the namespace of this object.

    Returns:

    • (String)

      A colon separated namespace, for example og:image.

  • #namespace(*names) ⇒ Object

    Set the namespace of this object.

    Examples:

    namespace :og, :image

    Parameters:

    • *names (Array<#to_s>)

      The individual parts of the namespace as list



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

def namespace *names
  return @namespace if names.empty?
  @namespace = names.join(':')
  Registry.register @namespace, self
end

#string(name, options = {}) ⇒ Object

Parameters:

  • name (Symbol)

    the name of the property in the current namespace

  • options ({Symbol => Bool, Class, Array<String>}) (defaults to: {})

    additional options

Options Hash (options):

  • :required (Bool) — default: false

    Make the property required.

  • :collection (Bool) — default: false

    This property can occur multiple times.

  • :to (Class)

    This property maps to the given object (optional). belongs to the given verticals of the object (optional).

  • :verticials (Array<String>)

    This property



9
10
11
# File 'lib/open_graph_reader/object/dsl/types.rb', line 9

define_type :string do |value|
  value.to_s
end

#url(name, options = {}) ⇒ Object

Parameters:

  • name (Symbol)

    the name of the property in the current namespace

  • options ({Symbol => Bool, Class, Array<String>}) (defaults to: {})

    additional options

Options Hash (options):

  • :required (Bool) — default: false

    Make the property required.

  • :collection (Bool) — default: false

    This property can occur multiple times.

  • :to (Class)

    This property maps to the given object (optional). belongs to the given verticals of the object (optional).

  • :verticials (Array<String>)

    This property



14
15
16
17
18
19
20
# File 'lib/open_graph_reader/object/dsl/types.rb', line 14

define_type :url do |value|
  value.to_s.tap {|value|
    unless value.start_with?('http://') || value.start_with?('https://')
      raise InvalidObjectError, "URL #{value.inspect} does not start with http:// or https://"
    end
  }
end

#verticals{String => Array<Strin>}

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.

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

Returns:

  • ({String => Array<Strin>})


125
126
127
# File 'lib/open_graph_reader/object/dsl.rb', line 125

def verticals
  @verticals ||= Hash.new {|h, k| h[k] = [] }
end