class DiasporaFederation::Parsers::BaseParser
BaseParser
is an abstract class which is used for defining parsers for different deserialization methods.
Attributes
Public Class Methods
@param [Class] entity_type
type of DiasporaFederation::Entity
that we want to parse with that parser instance
# File lib/diaspora_federation/parsers/base_parser.rb, line 9 def initialize(entity_type) @entity_type = entity_type end
Public Instance Methods
This method is used to parse input with a serialized object data. It returns a comprehensive data which must be enough to construct a DiasporaFederation::Entity
instance.
Since parser method output is normally passed to a .from_hash method of an entity as arguments using * operator, the parse method must return an array of a size matching the number of arguments of .from_hash method of the entity type we link with @abstract
# File lib/diaspora_federation/parsers/base_parser.rb, line 20 def parse(*) raise NotImplementedError.new("you must override this method when creating your own parser") end
Private Instance Methods
# File lib/diaspora_federation/parsers/base_parser.rb, line 48 def assert_parsability_of(entity_class) return if entity_class == entity_type.entity_name raise InvalidRootNode, "'#{entity_class}' can't be parsed by #{entity_type.name}" end
# File lib/diaspora_federation/parsers/base_parser.rb, line 56 def class_properties entity_type.class_props end
@param [Symbol] type target type to parse @param [String] text data as string @return [String, Boolean, Integer, Time] data
# File lib/diaspora_federation/parsers/base_parser.rb, line 29 def parse_string(type, text) case type when :timestamp begin Time.parse(text).utc rescue ArgumentError nil end when :integer text.to_i if text =~ /\A\d+\z/ when :boolean return true if text =~ /\A(true|t|yes|y|1)\z/i false if text =~ /\A(false|f|no|n|0)\z/i else text end end