Class: Eco::Language::Models::ParserSerializer
- Defined in:
- lib/eco/language/models/parser_serializer.rb
Overview
Basic class to define a parser/serializing framework
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attr ⇒ String, Symbol
readonly
the attribute this parser/serializer is linked to.
Instance Method Summary collapse
-
#def_parser(category = :default) {|source_data, dependencies| ... } ⇒ Object
Defines the parser of the attribute.
-
#def_serializer(category = :default) {|source_data, dependencies| ... } ⇒ Object
Defines the serializer of the attribute.
-
#initialize(attr, dependencies: {}) ⇒ ParserSerializer
constructor
Parser/serializer.
-
#parse(source, category = :default, dependencies: {}) {|key, value| ... } ⇒ Object
Calls the
parser
of this attribute by passingsource
and resolved dependencies. -
#parser_category?(category = :default) ⇒ Boolean
Checks if there's a
parser
defined forcategory
. -
#serialize(object, category = :default, dependencies: {}, &block) ⇒ Object
Calls the
serializer
of this attribute by passingobject
and resolved dependencies. -
#serializer_category?(category = :default) ⇒ Boolean
Checks if there's a
serializer
defined forcategory
.
Constructor Details
#initialize(attr, dependencies: {}) ⇒ ParserSerializer
Parser/serializer.
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
#attr ⇒ String, Symbol (readonly)
the attribute this parser/serializer is linked to.
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
- the block should expect one or two parameters.
- the final dependencies is a merge of default dependencies with
parse
call dependencies.
Defines the parser of the attribute.
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
- the block should expect one or two parameters.
- the final dependencies is a merge of default dependencies with
serialize
call dependencies.
Defines the serializer of the attribute.
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
- the method depenencies override keys of the default dependencies.
Calls the parser
of this attribute by passing source
and resolved dependencies.
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
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
- the method depenencies override keys of the default dependencies.
Calls the serializer
of this attribute by passing object
and resolved dependencies.
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
86 87 88 |
# File 'lib/eco/language/models/parser_serializer.rb', line 86 def serializer_category?(category = :default) @serializer.key?(category.to_sym) end |