Class: Sandwich::Model::DefinitionScanner
- Inherits:
-
StringScanner
- Object
- StringScanner
- Sandwich::Model::DefinitionScanner
- Defined in:
- lib/sandwich/model/definition_scanner.rb
Instance Method Summary collapse
- #attribute_pair ⇒ Object
- #attribute_pairs ⇒ Object
- #attribute_separators ⇒ Object
- #attribute_value ⇒ Object
-
#attributes ⇒ Object
assumes the scan pointer is past the model name.
- #determiner ⇒ Object (also: #raw_determiner)
- #has_attribute_separator? ⇒ Boolean
-
#initialize(str, attribute_separators) ⇒ DefinitionScanner
constructor
A new instance of DefinitionScanner.
- #model ⇒ Object (also: #raw_model)
- #model_definition ⇒ Object
- #raw_attributes ⇒ Object
- #whitespace ⇒ Object
- #words ⇒ Object (also: #attribute_name)
Constructor Details
#initialize(str, attribute_separators) ⇒ DefinitionScanner
Returns a new instance of DefinitionScanner.
6 7 8 9 10 |
# File 'lib/sandwich/model/definition_scanner.rb', line 6 def initialize(str, attribute_separators) super @attribute_separators = attribute_separators model_definition end |
Instance Method Details
#attribute_pair ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/sandwich/model/definition_scanner.rb', line 94 def attribute_pair w = attribute_name v = attribute_value if w.nil? || v.nil? nil else [w, v] end end |
#attribute_pairs ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/sandwich/model/definition_scanner.rb', line 82 def attribute_pairs result = [] while p = attribute_pair result << p scan(/\s*(?:and|,)\s*/) end Hash[*result.flatten] end |
#attribute_separators ⇒ Object
58 59 60 |
# File 'lib/sandwich/model/definition_scanner.rb', line 58 def attribute_separators "(?:#{@attribute_separators.join('|')})" end |
#attribute_value ⇒ Object
72 73 74 75 76 |
# File 'lib/sandwich/model/definition_scanner.rb', line 72 def attribute_value skip(/\s*"/) scan(/[^"]+/).tap { |val| skip(/(?:"|\s+)/) }.to_s end |
#attributes ⇒ Object
assumes the scan pointer is past the model name
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sandwich/model/definition_scanner.rb', line 39 def attributes whitespace @raw_attributes = rest.strip if has_attribute_separator? skip(/#{attribute_separators}/) whitespace attribute_pairs else attribute_value end.tap { whitespace } end |
#determiner ⇒ Object Also known as: raw_determiner
16 17 18 19 20 21 |
# File 'lib/sandwich/model/definition_scanner.rb', line 16 def determiner @determiner ||= begin whitespace scan(/\b(?:an?|\d+|the|no)\b/).tap { whitespace } end end |
#has_attribute_separator? ⇒ Boolean
78 79 80 |
# File 'lib/sandwich/model/definition_scanner.rb', line 78 def has_attribute_separator? check_until(/#{attribute_separators}/) end |
#model ⇒ Object Also known as: raw_model
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sandwich/model/definition_scanner.rb', line 25 def model @model ||= begin whitespace @model = if has_attribute_separator? scan_until(/(?=\s+#{attribute_separators})/) else words end.tap { whitespace } end end |
#model_definition ⇒ Object
12 13 14 |
# File 'lib/sandwich/model/definition_scanner.rb', line 12 def model_definition @model_definition ||= [determiner, model, attributes] end |
#raw_attributes ⇒ Object
54 55 56 |
# File 'lib/sandwich/model/definition_scanner.rb', line 54 def raw_attributes @raw_attributes end |
#whitespace ⇒ Object
68 69 70 |
# File 'lib/sandwich/model/definition_scanner.rb', line 68 def whitespace skip(/\s*/) end |
#words ⇒ Object Also known as: attribute_name
62 63 64 |
# File 'lib/sandwich/model/definition_scanner.rb', line 62 def words if w = scan(/\s*(?:\w+ ?)+/) then w.strip end end |