Class: Krikri::Parser::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/krikri/parser.rb

Overview

A generic parser value.

Interface to a single value node which can access typed data values (e.g. String, DateTime, etc…) parsed from a string, and provides access to child nodes and attributes.

Direct Known Subclasses

JsonParser::Value, XmlParser::Value

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



120
121
122
123
# File 'lib/krikri/parser.rb', line 120

def method_missing(name, *args, &block)
  return attribute(name) if attribute?(name)
  super
end

Instance Method Details

#[](name_exp) ⇒ Krikri::Parser::ValueArray

Property accessor interface. Passes a name expression (‘name_exp`) to the local implementation of #get_child_nodes.

The given name expression must follow the pattern:

name [| name ...]

The optional “|” is a short-circuit operator that will return the property or element in the document for the first matching part of the phrase.

Examples:

va = value['title']
va = value['this|that']  # gives value for "this" if both defined

Parameters:

  • name (String)

    An expression of named properties to access

Returns:



62
63
64
65
66
67
68
# File 'lib/krikri/parser.rb', line 62

def [](name_exp)
  name_exp.strip.split(/\s*\|\s*/).each do |n|
    result = get_child_nodes(n)
    return result unless result.empty?
  end
  Krikri::Parser::ValueArray.new([])
end

#attribute?(name) ⇒ Boolean

Queries whether ‘name` is an attribute of this node

Parameters:

  • name (#to_sym)

    an attribute name to query

Returns:

  • (Boolean)

    true if ‘name` is an attribute of the current node



112
113
114
115
116
117
118
# File 'lib/krikri/parser.rb', line 112

def attribute?(name)
  begin
    attributes.include?(name)
  rescue NotImplementedError
    false
  end
end

#attributesArray<Symbol>

This method is abstract.

Returns a list of attributes accessible on the node.

Returns:

  • (Array<Symbol>)

    a list of attributes accessible on the node

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/krikri/parser.rb', line 104

def attributes
  raise NotImplementedError
end

#child?(name) ⇒ Boolean

Queries whether ‘name` is a subproperty of this node

Parameters:

  • name (#to_sym)

    a named property to query

Returns:

  • (Boolean)

    true if ‘name` is a subproperty of the current node



74
75
76
# File 'lib/krikri/parser.rb', line 74

def child?(name)
  children.include?(name)
end

#childrenArray<Symbol>

This method is abstract.

Returns a list of subproperties that can be passed back to #[] to access child nodes.

Returns:

  • (Array<Symbol>)

    a list of subproperties that can be passed back to #[] to access child nodes

Raises:

  • (NotImplementedError)


82
83
84
# File 'lib/krikri/parser.rb', line 82

def children
  raise NotImplementedError
end

#respond_to_missing(method) ⇒ Object



125
126
127
# File 'lib/krikri/parser.rb', line 125

def respond_to_missing(method, *)
  attribute?(method) || super
end

#value<#to_s>

This method is abstract.

Returns typed value for the property.

Returns:

  • (<#to_s>)

    typed value for the property

Raises:

  • (NotImplementedError)


89
90
91
# File 'lib/krikri/parser.rb', line 89

def value
  raise NotImplementedError
end

#values?Boolean

This method is abstract.

Returns true if this node has typed values accessible with #values.

Returns:

  • (Boolean)

    true if this node has typed values accessible with #values

Raises:

  • (NotImplementedError)


97
98
99
# File 'lib/krikri/parser.rb', line 97

def values?
  raise NotImplementedError
end