Class: Sandwich::Model::DefinitionScanner

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/sandwich/model/definition_scanner.rb

Instance Method Summary collapse

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_pairObject



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_pairsObject



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_separatorsObject



58
59
60
# File 'lib/sandwich/model/definition_scanner.rb', line 58

def attribute_separators
  "(?:#{@attribute_separators.join('|')})"
end

#attribute_valueObject



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

#attributesObject

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

#determinerObject 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

Returns:

  • (Boolean)


78
79
80
# File 'lib/sandwich/model/definition_scanner.rb', line 78

def has_attribute_separator?
  check_until(/#{attribute_separators}/)
end

#modelObject 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_definitionObject



12
13
14
# File 'lib/sandwich/model/definition_scanner.rb', line 12

def model_definition
  @model_definition ||= [determiner, model, attributes]
end

#raw_attributesObject



54
55
56
# File 'lib/sandwich/model/definition_scanner.rb', line 54

def raw_attributes
  @raw_attributes
end

#whitespaceObject



68
69
70
# File 'lib/sandwich/model/definition_scanner.rb', line 68

def whitespace
  skip(/\s*/)
end

#wordsObject 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