File: object.rb

package info (click to toggle)
ruby-open-graph-reader 0.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,980 kB
  • ctags: 133
  • sloc: ruby: 1,505; xml: 22; makefile: 2
file content (96 lines) | stat: -rw-r--r-- 2,640 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require "open_graph_reader/object/registry"
require "open_graph_reader/object/dsl"
require "open_graph_reader/object/dsl/types"

module OpenGraphReader
  # 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
  module Object
    # @private
    def self.included base
      base.extend DSL
    end

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

    # Regular properties on this object
    #
    # @api private
    # @return [{String => String, Object}]
    attr_reader :properties

    # Properties on this object that are arrays.
    #
    # @api private
    # @return [{String => Array<String, Object>}]
    attr_reader :children

    # Create a new object. If your class overrides this don't forget to call <tt>super</tt>.
    def initialize
      @properties = {}
      @children = Hash.new {|h, k| h[k] = [] }
    end

    # Whether this object has the given property
    #
    # @param [#to_s] name
    # @return [Bool]
    def property? name
      self.class.available_properties.include? name.to_s
    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
    def content= value
      value = self.class.content_processor.call(value)
      @content = value
    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]
    def [] name
      raise UndefinedPropertyError, "Undefined property #{name} on #{inspect}" unless property? name
      public_send name.to_s
    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.
    def []= name, value
      if property?(name)
        public_send "#{name}=", value
      elsif OpenGraphReader.config.strict
        raise UndefinedPropertyError, "Undefined property #{name} on #{inspect}"
      end
    end

    # Returns {#content} if available.
    #
    # @return [String]
    def to_s
      content || super
    end
  end
end