Class: ParserAction::Ruby

Inherits:
Abstract show all
Defined in:
lib/ejson/parser_action/ruby.rb

Direct Known Subclasses

BSON, Wrap

Constant Summary collapse

UNICODE_ESCAPER =
proc { |s|
  format('\u%04<char>X', char: s.codepoints[0])
}

Instance Method Summary collapse

Methods included from AbstractMongo

#make_bin_data, #make_db_ref, #make_max_key, #make_min_key, #make_number_decimal, #make_object_id, #make_timestamp, #make_undefined

Instance Method Details

#make_array(_input, _starts_at, _ends_at, elements) ⇒ Object



55
56
57
58
59
# File 'lib/ejson/parser_action/ruby.rb', line 55

def make_array(_input, _starts_at, _ends_at, elements)
  list = [elements[0]]
  elements[1].each { |el| list << el.value }
  list
end

#make_date(_input, _starts_at, _ends_at, elements) ⇒ Object

Makes ruby date from mongo Data type



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ejson/parser_action/ruby.rb', line 85

def make_date(_input, _starts_at, _ends_at, elements)
  value = elements.first

  case value
  when String
    _date_from_string(value)
  when Numeric
    Time.at(value)
  else
    raise ArgumentError,
          "Internal parser error. Unknown date atom type #{value.class}"
  end
end

#make_empty_array(_input, _starts_at, _ends_at, _elements) ⇒ Object



61
62
63
# File 'lib/ejson/parser_action/ruby.rb', line 61

def make_empty_array(_input, _starts_at, _ends_at, _elements)
  []
end

#make_empty_object(_input, _starts_at, _ends_at, _elements) ⇒ Object



31
32
33
# File 'lib/ejson/parser_action/ruby.rb', line 31

def make_empty_object(_input, _starts_at, _ends_at, _elements)
  {}
end

#make_false(_input, _starts_at, _ends_at) ⇒ Object



80
81
82
# File 'lib/ejson/parser_action/ruby.rb', line 80

def make_false(_input, _starts_at, _ends_at)
  false
end

#make_identifier(input, starts_at, ends_at, _elements) ⇒ Object



40
41
42
# File 'lib/ejson/parser_action/ruby.rb', line 40

def make_identifier(input, starts_at, ends_at, _elements)
  input[starts_at...ends_at]
end

#make_null(_input, _starts_at, _ends_at) ⇒ Object



72
73
74
# File 'lib/ejson/parser_action/ruby.rb', line 72

def make_null(_input, _starts_at, _ends_at)
  nil
end

#make_number(input, starts_at, ends_at, _elements) ⇒ Object



65
66
67
68
69
70
# File 'lib/ejson/parser_action/ruby.rb', line 65

def make_number(input, starts_at, ends_at, _elements)
  string = input[starts_at...ends_at]
  string.downcase!

  _number_from_string(string)
end

#make_number_as_string(input, starts_at, ends_at, _elements) ⇒ Object



48
49
50
51
52
53
# File 'lib/ejson/parser_action/ruby.rb', line 48

def make_number_as_string(input, starts_at, ends_at, _elements)
  string = input[starts_at...ends_at]
  string.downcase!

  string
end

#make_number_long(_input, _starts_at, _ends_at, elements) ⇒ Object



103
104
105
# File 'lib/ejson/parser_action/ruby.rb', line 103

def make_number_long(_input, _starts_at, _ends_at, elements)
  elements.first
end

#make_numeric_string(_input, _starts_at, _ends_at, elements) ⇒ Object



44
45
46
# File 'lib/ejson/parser_action/ruby.rb', line 44

def make_numeric_string(_input, _starts_at, _ends_at, elements)
  elements.first
end

#make_object(_input, _starts_at, _ends_at, elements) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ejson/parser_action/ruby.rb', line 14

def make_object(_input, _starts_at, _ends_at, elements)
  first_pair = elements.first
  other_pairs = elements.last

  object = { first_pair[0] => first_pair[1] }
  other_pairs.each do |element|
    pair = element.pair
    object[pair.first] = pair.last
  end

  object
end

#make_pair(_input, _starts_at, _ends_at, elements) ⇒ Object



27
28
29
# File 'lib/ejson/parser_action/ruby.rb', line 27

def make_pair(_input, _starts_at, _ends_at, elements)
  elements
end

#make_regexp(_input, _starts_at, _ends_at, elements) ⇒ Object



99
100
101
# File 'lib/ejson/parser_action/ruby.rb', line 99

def make_regexp(_input, _starts_at, _ends_at, elements)
  Regexp.new(elements.first.text, elements.last.text)
end

#make_root(_input, _starts_at, _ends_at, elements) ⇒ Object



10
11
12
# File 'lib/ejson/parser_action/ruby.rb', line 10

def make_root(_input, _starts_at, _ends_at, elements)
  elements.first
end

#make_string(_input, _starts_at, _ends_at, elements) ⇒ Object



35
36
37
38
# File 'lib/ejson/parser_action/ruby.rb', line 35

def make_string(_input, _starts_at, _ends_at, elements)
  quoted_string = %("#{elements.first.text}")
  quoted_string.encode('ASCII', fallback: UNICODE_ESCAPER).undump
end

#make_true(_input, _starts_at, _ends_at) ⇒ Object



76
77
78
# File 'lib/ejson/parser_action/ruby.rb', line 76

def make_true(_input, _starts_at, _ends_at)
  true
end