Class: Eco::Language::Models::ParserSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/language/models/parser_serializer.rb

Overview

Basic class to define a parser/serializing framework

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr, dependencies: {}) ⇒ ParserSerializer

Parser/serializer.

Parameters:

  • attr (String, Symbol)

    name of the parsed/serialized.

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

    provisioning of default dependencies that will be required when calling back to the parsing or serializing functions.



14
15
16
17
18
19
# File 'lib/eco/language/models/parser_serializer.rb', line 14

def initialize(attr, dependencies: {})
  @attr         = attr
  @dependencies = dependencies
  @parser       = {}
  @serializer   = {}
end

Instance Attribute Details

#attrString, Symbol (readonly)

the attribute this parser/serializer is linked to.

Returns:

  • (String, Symbol)

    the current value of attr



6
7
8
# File 'lib/eco/language/models/parser_serializer.rb', line 6

def attr
  @attr
end

Instance Method Details

#def_parser(category = :default) {|source_data, dependencies| ... } ⇒ Object

Note:
  1. the block should expect one or two parameters.
  2. the final dependencies is a merge of default dependencies with parse call dependencies.

Defines the parser of the attribute.

Parameters:

  • category (Symbol) (defaults to: :default)

    a way to classify multiple parsers by category.

Yields:

  • (source_data, dependencies)

    user defined parser that returns the parsed value.

Yield Parameters:

  • source_data (Any)

    source data that will be parsed.

  • dependencies (Hash)

    hash with the provisioned dependencies.



29
30
31
32
# File 'lib/eco/language/models/parser_serializer.rb', line 29

def def_parser(category = :default, &block)
  @parser[category.to_sym] = block
  self
end

#def_serializer(category = :default) {|source_data, dependencies| ... } ⇒ Object

Note:
  1. the block should expect one or two parameters.
  2. the final dependencies is a merge of default dependencies with serialize call dependencies.

Defines the serializer of the attribute.

Parameters:

  • category (Symbol) (defaults to: :default)

    a way to classify multiple serializers by category.

Yields:

  • (source_data, dependencies)

    user defined serialiser that returns the serialised value.

Yield Parameters:

  • source_data (Any)

    source data that will be serialised.

  • dependencies (Hash)

    hash with the provisioned dependencies.



42
43
44
45
# File 'lib/eco/language/models/parser_serializer.rb', line 42

def def_serializer(category = :default, &block)
  @serializer[category.to_sym] = block
  self
end

#parse(source, category = :default, dependencies: {}) {|key, value| ... } ⇒ Object

Note:
  • the method depenencies override keys of the default dependencies.

Calls the parser of this attribute by passing source and resolved dependencies.

Parameters:

  • source (Any)

    source data to be parsed.

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

    additional dependencies that should be merged to the default dependencies.

Yields:

  • (key, value)

    the dependency resolver. The block is called is value is a Proc.

Yield Parameters:

  • key (Symbol)

    the dependency key name.

  • value (Any)

    the depedency value.

Yield Returns:

  • value the new value

Raises:

  • (Exception)

    when there is no parser defined.



57
58
59
60
61
62
# File 'lib/eco/language/models/parser_serializer.rb', line 57

def parse(source, category = :default, dependencies: {}, &block)
  raise "There is no parser of type '#{category}' for this attribue '#{attr}'" unless parser_category?(category)

  deps = resolve_dependencies(@dependencies.merge(dependencies), &block)
  call_block(source, deps, attr, &@parser[category.to_sym])
end

#parser_category?(category = :default) ⇒ Boolean

Checks if there's a parser defined for category

Returns:

  • (Boolean)

    true if the parser is defined, and false otherwise



80
81
82
# File 'lib/eco/language/models/parser_serializer.rb', line 80

def parser_category?(category = :default)
  @parser.key?(category.to_sym)
end

#serialize(object, category = :default, dependencies: {}, &block) ⇒ Object

Note:
  • the method depenencies override keys of the default dependencies.

Calls the serializer of this attribute by passing object and resolved dependencies.

Parameters:

  • object (Any)

    source data to be serialized.

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

    additional dependencies that should be merged to the default dependencies.

Raises:

  • (Exception)

    when there is no serializer defined.



70
71
72
73
74
75
76
# File 'lib/eco/language/models/parser_serializer.rb', line 70

def serialize(object, category = :default, dependencies: {}, &block)
  msg = "There is no serializer of type '#{category}' for this attribue '#{attr}'"
  raise msg unless serializer_category?(category)

  deps = resolve_dependencies(@dependencies.merge(dependencies), &block)
  call_block(object, deps, attr, &@serializer[category.to_sym])
end

#serializer_category?(category = :default) ⇒ Boolean

Checks if there's a serializer defined for category

Returns:

  • (Boolean)

    true if the serializer is defined, and false otherwise



86
87
88
# File 'lib/eco/language/models/parser_serializer.rb', line 86

def serializer_category?(category = :default)
  @serializer.key?(category.to_sym)
end