Module: Mbrao::ParserInterface::ClassMethods

Defined in:
lib/mbrao/parser_interface.rb

Overview

Class methods.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (String) locale

Gets the default locale for mbrao.

Returns:

  • (String)

    The 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
124
125
126
127
128
129
130
131
# File 'lib/mbrao/parser_interface.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(no_duplicates: true, compact: true, flatten: true, sanitizer: :ensure_string)
    keys = keys.ensure_array(no_duplicates: true, compact: true, flatten: true, sanitizer: :ensure_string)

    map_to_json(target, (keys - exclude), include_empty)
  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)
    type = :parsing if type != :rendering
    ::Lazier.find_class(cls, "::Mbrao::#{type.to_s.classify}Engines::%CLASS%").new
  rescue NameError
    raise Mbrao::Exceptions::UnknownEngine
  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

  # :nodoc:
  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

  # :nodoc:
  def map_to_json(target, keys, include_empty)
    json = keys.reduce({}) do |rv, key|
      value = get_json_field(target, key)
      rv[key] = value if include_empty || value.present?
      rv
    end

    json.deep_stringify_keys
  end

  # :nodoc:
  def get_json_field(target, method)
    value = target.send(method)
    value = value.as_json if value && value.respond_to?(:as_json) && !value.is_a?(Symbol)
    value
  end
end

- (String) parsing_engine

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
124
125
126
127
128
129
130
131
# File 'lib/mbrao/parser_interface.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(no_duplicates: true, compact: true, flatten: true, sanitizer: :ensure_string)
    keys = keys.ensure_array(no_duplicates: true, compact: true, flatten: true, sanitizer: :ensure_string)

    map_to_json(target, (keys - exclude), include_empty)
  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)
    type = :parsing if type != :rendering
    ::Lazier.find_class(cls, "::Mbrao::#{type.to_s.classify}Engines::%CLASS%").new
  rescue NameError
    raise Mbrao::Exceptions::UnknownEngine
  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

  # :nodoc:
  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

  # :nodoc:
  def map_to_json(target, keys, include_empty)
    json = keys.reduce({}) do |rv, key|
      value = get_json_field(target, key)
      rv[key] = value if include_empty || value.present?
      rv
    end

    json.deep_stringify_keys
  end

  # :nodoc:
  def get_json_field(target, method)
    value = target.send(method)
    value = value.as_json if value && value.respond_to?(:as_json) && !value.is_a?(Symbol)
    value
  end
end

- (String) rendering_engine

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
124
125
126
127
128
129
130
131
# File 'lib/mbrao/parser_interface.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(no_duplicates: true, compact: true, flatten: true, sanitizer: :ensure_string)
    keys = keys.ensure_array(no_duplicates: true, compact: true, flatten: true, sanitizer: :ensure_string)

    map_to_json(target, (keys - exclude), include_empty)
  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)
    type = :parsing if type != :rendering
    ::Lazier.find_class(cls, "::Mbrao::#{type.to_s.classify}Engines::%CLASS%").new
  rescue NameError
    raise Mbrao::Exceptions::UnknownEngine
  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

  # :nodoc:
  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

  # :nodoc:
  def map_to_json(target, keys, include_empty)
    json = keys.reduce({}) do |rv, key|
      value = get_json_field(target, key)
      rv[key] = value if include_empty || value.present?
      rv
    end

    json.deep_stringify_keys
  end

  # :nodoc:
  def get_json_field(target, method)
    value = target.send(method)
    value = value.as_json if value && value.respond_to?(:as_json) && !value.is_a?(Symbol)
    value
  end
end

Instance Method Details

- (Hash) as_json(target, keys, options = {})

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
# File 'lib/mbrao/parser_interface.rb', line 76

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

  map_to_json(target, (keys - exclude), include_empty)
end

- (Object) create_engine(cls, type = :parsing)

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.



89
90
91
92
93
94
# File 'lib/mbrao/parser_interface.rb', line 89

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

- (Parser) instance(force = false)

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.



100
101
102
103
# File 'lib/mbrao/parser_interface.rb', line 100

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

- (Content) parse(content, options = {})

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_interface.rb', line 52

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

- (String) render(content, options = {}, context = {})

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_interface.rb', line 62

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