Class: Versionomy::Format::Base
- Inherits:
-
Object
- Object
- Versionomy::Format::Base
- Defined in:
- lib/versionomy/format/base.rb
Overview
The base format.
This format doesn’t actually do anything useful. It causes all strings to parse to the schema’s default value, and unparses all values to the empty string. Instead, the purpose here is to define the API for a format.
All formats must define the methods schema, parse, and unparse. It is also recommended that formats define the === method, though this is not strictly required. Finally, formats may optionally implement uparse_for_serialize.
Formats need not extend this base class, as long as they duck-type these methods.
Direct Known Subclasses
Instance Method Summary collapse
-
#===(obj_) ⇒ Object
Determine whether the given value uses this format.
-
#initialize(schema_) ⇒ Base
constructor
Create an instance of this base format, with the given schema.
-
#inspect ⇒ Object
:nodoc:.
-
#parse(string_, params_ = nil) ⇒ Object
Parse the given string and return a value.
-
#schema ⇒ Object
Returns the schema understood by this format.
-
#to_s ⇒ Object
:nodoc:.
-
#unparse(value_, params_ = nil) ⇒ Object
Unparse the given value and return a string.
-
#unparse_for_serialization(value_) ⇒ Object
An optional method that does unparsing especially for serialization.
Constructor Details
#initialize(schema_) ⇒ Base
Create an instance of this base format, with the given schema.
63 64 65 |
# File 'lib/versionomy/format/base.rb', line 63 def initialize(schema_) @_schema = schema_ end |
Instance Method Details
#===(obj_) ⇒ Object
Determine whether the given value uses this format.
132 133 134 135 136 137 138 |
# File 'lib/versionomy/format/base.rb', line 132 def ===(obj_) if obj_.kind_of?(Value) obj_.format == self else obj_ == self end end |
#inspect ⇒ Object
:nodoc:
68 69 70 |
# File 'lib/versionomy/format/base.rb', line 68 def inspect # :nodoc: "#<#{self.class}:0x#{object_id.to_s(16)} schema=#{@_schema.inspect}>" end |
#parse(string_, params_ = nil) ⇒ Object
Parse the given string and return a value.
The optional parameter hash can be used to pass parameters to the parser to affect its behavior. The exact parameters supported are defined by the format.
90 91 92 |
# File 'lib/versionomy/format/base.rb', line 90 def parse(string_, params_=nil) Value.new([], self) end |
#schema ⇒ Object
Returns the schema understood by this format.
79 80 81 |
# File 'lib/versionomy/format/base.rb', line 79 def schema @_schema end |
#to_s ⇒ Object
:nodoc:
72 73 74 |
# File 'lib/versionomy/format/base.rb', line 72 def to_s # :nodoc: inspect end |
#unparse(value_, params_ = nil) ⇒ Object
Unparse the given value and return a string.
The optional parameter hash can be used to pass parameters to the unparser to affect its behavior. The exact parameters supported are defined by the format.
101 102 103 |
# File 'lib/versionomy/format/base.rb', line 101 def unparse(value_, params_=nil) '' end |
#unparse_for_serialization(value_) ⇒ Object
An optional method that does unparsing especially for serialization. Implement this if normal unparsing is “lossy” and doesn’t guarantee reconstruction of the version number. This method should attempt to unparse in such a way that the entire version value can be reconstructed from the unparsed string. Serialization routines will first attempt to call this method to unparse for serialization. If this method is not present, the normal unparse method will be used.
Return either the unparsed string, or an array consisting of the unparsed string and a hash of parse params to pass to the parser when the string is to be reconstructed. You may also either return nil or raise Versionomy::Errors::UnparseError if the unparsing cannot be done satisfactorily for serialization. In this case, serialization will be done using the raw value data rather than an unparsed string.
This default implementation just turns around and calls unparse. Thus it is equivalent to the method not being present at all.
125 126 127 |
# File 'lib/versionomy/format/base.rb', line 125 def unparse_for_serialization(value_) unparse(value_) end |