Module: Mbrao::PublicInterface::ClassMethods

Defined in:
lib/mbrao/parser.rb

Overview

Class methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_localeString

Returns The mbrao default locale.

Returns:

  • (String)

    The mbrao default locale.



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mbrao/parser.rb', line 21

module ClassMethods
  attr_accessor :locale
  attr_accessor :parsing_engine
  attr_accessor :rendering_engine

  # Gets the default locale for mbrao.
  #
  # @return [String] The default locale.
  def locale
    attribute_or_default(@locale, "en")
  end

  # Gets the default parsing engine.
  #
  # @return [String] The default parsing engine.
  def parsing_engine
    attribute_or_default(@parsing_engine, :plain_text, :to_sym)
  end

  # Gets the default rendering engine.
  #
  # @return [String] The default rendering engine.
  def rendering_engine
    attribute_or_default(@rendering_engine, :html_pipeline, :to_sym)
  end

  # Parses a source text.
  #
  # @param content [Object] The content to parse.
  # @param options [Hash] A list of options for parsing.
  # @return [Content] The parsed data.
  def parse(content, options = {})
    instance.parse(content, options)
  end

  # Renders a content.
  #
  # @param content [Content] The content to parse.
  # @param options [Hash] A list of options for renderer.
  # @param context [Hash] A context for rendering.
  # @return [String] The rendered content.
  def render(content, options = {}, context = {})
    instance.render(content, options, context)
  end

  # Returns an object as a JSON compatible hash
  #
  # @param target [Object] The target to serialize.
  # @param keys [Array] The attributes to include in the serialization
  # @param options [Hash] Options to modify behavior of the serialization.
  #   The only supported value are:
  #
  #   * `:exclude`, an array of attributes to skip.
  #   * `:exclude_empty`, if to exclude nil values. Default is `false`.
  # @return [Hash] An hash with all attributes.
  def as_json(target, keys, options = {})
    include_empty = !options[:exclude_empty].to_boolean
    exclude = options[:exclude].ensure_array(nil, true, true, true, :ensure_string)
    keys = keys.ensure_array(nil, true, true, true, :ensure_string)

    (keys - exclude).reduce({}) {|rv, key|
      value = target.send(key)
      value = value.as_json if value && value.respond_to?(:as_json) && !value.is_a?(Symbol)
      rv[key] = value if include_empty || value.present?
      rv
    }.deep_stringify_keys
  end

  # Instantiates a new engine for rendering or parsing.
  #
  # @param cls [String|Symbol|Object] If a `String` or a `Symbol`, then it will be the class of the engine.
  # @param type [Symbol] The type or engine. Can be `:parsing` or `:rendering`.
  # @return [Object] A new engine.
  def create_engine(cls, type = :parsing)
    begin
      type = :parsing if type != :rendering
      ::Lazier.find_class(cls, "::Mbrao::#{type.to_s.classify}Engines::%CLASS%").new
    rescue NameError
      raise Mbrao::Exceptions::UnknownEngine.new
    end
  end

  # Returns a unique (singleton) instance of the parser.
  #
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Parser] The unique (singleton) instance of the parser.
  def instance(force = false)
    @instance = nil if force
    @instance ||= Mbrao::Parser.new
  end

  private
    # Returns an attribute or a default value.
    #
    # @param attr [Object ]The attribute to return.
    # @param default_value [Object] The value to return if `attr` is blank.
    # @param sanitizer [Symbol] An optional method to sanitize the returned value.
    def attribute_or_default(attr, default_value = nil, sanitizer = :ensure_string)
      rv = attr.present? ? attr : default_value
      rv = rv.send(sanitizer) if sanitizer
      rv
    end
end

#localeString

Gets the default locale for mbrao.

Returns:

  • (String)

    The default locale.



29
30
31
# File 'lib/mbrao/parser.rb', line 29

def locale
  @locale
end

#parsing_engineString

Gets the default parsing engine.

Returns:

  • (String)

    The default parsing engine.



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mbrao/parser.rb', line 21

module ClassMethods
  attr_accessor :locale
  attr_accessor :parsing_engine
  attr_accessor :rendering_engine

  # Gets the default locale for mbrao.
  #
  # @return [String] The default locale.
  def locale
    attribute_or_default(@locale, "en")
  end

  # Gets the default parsing engine.
  #
  # @return [String] The default parsing engine.
  def parsing_engine
    attribute_or_default(@parsing_engine, :plain_text, :to_sym)
  end

  # Gets the default rendering engine.
  #
  # @return [String] The default rendering engine.
  def rendering_engine
    attribute_or_default(@rendering_engine, :html_pipeline, :to_sym)
  end

  # Parses a source text.
  #
  # @param content [Object] The content to parse.
  # @param options [Hash] A list of options for parsing.
  # @return [Content] The parsed data.
  def parse(content, options = {})
    instance.parse(content, options)
  end

  # Renders a content.
  #
  # @param content [Content] The content to parse.
  # @param options [Hash] A list of options for renderer.
  # @param context [Hash] A context for rendering.
  # @return [String] The rendered content.
  def render(content, options = {}, context = {})
    instance.render(content, options, context)
  end

  # Returns an object as a JSON compatible hash
  #
  # @param target [Object] The target to serialize.
  # @param keys [Array] The attributes to include in the serialization
  # @param options [Hash] Options to modify behavior of the serialization.
  #   The only supported value are:
  #
  #   * `:exclude`, an array of attributes to skip.
  #   * `:exclude_empty`, if to exclude nil values. Default is `false`.
  # @return [Hash] An hash with all attributes.
  def as_json(target, keys, options = {})
    include_empty = !options[:exclude_empty].to_boolean
    exclude = options[:exclude].ensure_array(nil, true, true, true, :ensure_string)
    keys = keys.ensure_array(nil, true, true, true, :ensure_string)

    (keys - exclude).reduce({}) {|rv, key|
      value = target.send(key)
      value = value.as_json if value && value.respond_to?(:as_json) && !value.is_a?(Symbol)
      rv[key] = value if include_empty || value.present?
      rv
    }.deep_stringify_keys
  end

  # Instantiates a new engine for rendering or parsing.
  #
  # @param cls [String|Symbol|Object] If a `String` or a `Symbol`, then it will be the class of the engine.
  # @param type [Symbol] The type or engine. Can be `:parsing` or `:rendering`.
  # @return [Object] A new engine.
  def create_engine(cls, type = :parsing)
    begin
      type = :parsing if type != :rendering
      ::Lazier.find_class(cls, "::Mbrao::#{type.to_s.classify}Engines::%CLASS%").new
    rescue NameError
      raise Mbrao::Exceptions::UnknownEngine.new
    end
  end

  # Returns a unique (singleton) instance of the parser.
  #
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Parser] The unique (singleton) instance of the parser.
  def instance(force = false)
    @instance = nil if force
    @instance ||= Mbrao::Parser.new
  end

  private
    # Returns an attribute or a default value.
    #
    # @param attr [Object ]The attribute to return.
    # @param default_value [Object] The value to return if `attr` is blank.
    # @param sanitizer [Symbol] An optional method to sanitize the returned value.
    def attribute_or_default(attr, default_value = nil, sanitizer = :ensure_string)
      rv = attr.present? ? attr : default_value
      rv = rv.send(sanitizer) if sanitizer
      rv
    end
end

#rendering_engineString

Gets the default rendering engine.

Returns:

  • (String)

    The default rendering engine.



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mbrao/parser.rb', line 21

module ClassMethods
  attr_accessor :locale
  attr_accessor :parsing_engine
  attr_accessor :rendering_engine

  # Gets the default locale for mbrao.
  #
  # @return [String] The default locale.
  def locale
    attribute_or_default(@locale, "en")
  end

  # Gets the default parsing engine.
  #
  # @return [String] The default parsing engine.
  def parsing_engine
    attribute_or_default(@parsing_engine, :plain_text, :to_sym)
  end

  # Gets the default rendering engine.
  #
  # @return [String] The default rendering engine.
  def rendering_engine
    attribute_or_default(@rendering_engine, :html_pipeline, :to_sym)
  end

  # Parses a source text.
  #
  # @param content [Object] The content to parse.
  # @param options [Hash] A list of options for parsing.
  # @return [Content] The parsed data.
  def parse(content, options = {})
    instance.parse(content, options)
  end

  # Renders a content.
  #
  # @param content [Content] The content to parse.
  # @param options [Hash] A list of options for renderer.
  # @param context [Hash] A context for rendering.
  # @return [String] The rendered content.
  def render(content, options = {}, context = {})
    instance.render(content, options, context)
  end

  # Returns an object as a JSON compatible hash
  #
  # @param target [Object] The target to serialize.
  # @param keys [Array] The attributes to include in the serialization
  # @param options [Hash] Options to modify behavior of the serialization.
  #   The only supported value are:
  #
  #   * `:exclude`, an array of attributes to skip.
  #   * `:exclude_empty`, if to exclude nil values. Default is `false`.
  # @return [Hash] An hash with all attributes.
  def as_json(target, keys, options = {})
    include_empty = !options[:exclude_empty].to_boolean
    exclude = options[:exclude].ensure_array(nil, true, true, true, :ensure_string)
    keys = keys.ensure_array(nil, true, true, true, :ensure_string)

    (keys - exclude).reduce({}) {|rv, key|
      value = target.send(key)
      value = value.as_json if value && value.respond_to?(:as_json) && !value.is_a?(Symbol)
      rv[key] = value if include_empty || value.present?
      rv
    }.deep_stringify_keys
  end

  # Instantiates a new engine for rendering or parsing.
  #
  # @param cls [String|Symbol|Object] If a `String` or a `Symbol`, then it will be the class of the engine.
  # @param type [Symbol] The type or engine. Can be `:parsing` or `:rendering`.
  # @return [Object] A new engine.
  def create_engine(cls, type = :parsing)
    begin
      type = :parsing if type != :rendering
      ::Lazier.find_class(cls, "::Mbrao::#{type.to_s.classify}Engines::%CLASS%").new
    rescue NameError
      raise Mbrao::Exceptions::UnknownEngine.new
    end
  end

  # Returns a unique (singleton) instance of the parser.
  #
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Parser] The unique (singleton) instance of the parser.
  def instance(force = false)
    @instance = nil if force
    @instance ||= Mbrao::Parser.new
  end

  private
    # Returns an attribute or a default value.
    #
    # @param attr [Object ]The attribute to return.
    # @param default_value [Object] The value to return if `attr` is blank.
    # @param sanitizer [Symbol] An optional method to sanitize the returned value.
    def attribute_or_default(attr, default_value = nil, sanitizer = :ensure_string)
      rv = attr.present? ? attr : default_value
      rv = rv.send(sanitizer) if sanitizer
      rv
    end
end

Instance Method Details

#as_json(target, keys, options = {}) ⇒ Hash

Returns an object as a JSON compatible hash

Parameters:

  • target (Object)

    The target to serialize.

  • keys (Array)

    The attributes to include in the serialization

  • options (Hash) (defaults to: {})

    Options to modify behavior of the serialization. The only supported value are:

    • :exclude, an array of attributes to skip.
    • :exclude_empty, if to exclude nil values. Default is false.

Returns:

  • (Hash)

    An hash with all attributes.



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mbrao/parser.rb', line 76

def as_json(target, keys, options = {})
  include_empty = !options[:exclude_empty].to_boolean
  exclude = options[:exclude].ensure_array(nil, true, true, true, :ensure_string)
  keys = keys.ensure_array(nil, true, true, true, :ensure_string)

  (keys - exclude).reduce({}) {|rv, key|
    value = target.send(key)
    value = value.as_json if value && value.respond_to?(:as_json) && !value.is_a?(Symbol)
    rv[key] = value if include_empty || value.present?
    rv
  }.deep_stringify_keys
end

#create_engine(cls, type = :parsing) ⇒ Object

Instantiates a new engine for rendering or parsing.

Parameters:

  • cls (String|Symbol|Object)

    If a String or a Symbol, then it will be the class of the engine.

  • type (Symbol) (defaults to: :parsing)

    The type or engine. Can be :parsing or :rendering.

Returns:

  • (Object)

    A new engine.



94
95
96
97
98
99
100
101
# File 'lib/mbrao/parser.rb', line 94

def create_engine(cls, type = :parsing)
  begin
    type = :parsing if type != :rendering
    ::Lazier.find_class(cls, "::Mbrao::#{type.to_s.classify}Engines::%CLASS%").new
  rescue NameError
    raise Mbrao::Exceptions::UnknownEngine.new
  end
end

#instance(force = false) ⇒ Parser

Returns a unique (singleton) instance of the parser.

Parameters:

  • force (Boolean) (defaults to: false)

    If to force recreation of the instance.

Returns:

  • (Parser)

    The unique (singleton) instance of the parser.



107
108
109
110
# File 'lib/mbrao/parser.rb', line 107

def instance(force = false)
  @instance = nil if force
  @instance ||= Mbrao::Parser.new
end

#parse(content, options = {}) ⇒ Content

Parses a source text.

Parameters:

  • content (Object)

    The content to parse.

  • options (Hash) (defaults to: {})

    A list of options for parsing.

Returns:



52
53
54
# File 'lib/mbrao/parser.rb', line 52

def parse(content, options = {})
  instance.parse(content, options)
end

#render(content, options = {}, context = {}) ⇒ String

Renders a content.

Parameters:

  • content (Content)

    The content to parse.

  • options (Hash) (defaults to: {})

    A list of options for renderer.

  • context (Hash) (defaults to: {})

    A context for rendering.

Returns:

  • (String)

    The rendered content.



62
63
64
# File 'lib/mbrao/parser.rb', line 62

def render(content, options = {}, context = {})
  instance.render(content, options, context)
end