Module: DSON::Value

Included in:
ArrayValue, FalseValue, HashValue, NilValue, NumericValue, ObjectValue, StringValue, TrueValue
Defined in:
lib/DSON/value.rb,
lib/DSON/value/nil_value.rb,
lib/DSON/value/hash_value.rb,
lib/DSON/value/true_value.rb,
lib/DSON/value/array_value.rb,
lib/DSON/value/false_value.rb,
lib/DSON/value/object_value.rb,
lib/DSON/value/string_value.rb,
lib/DSON/value/numeric_value.rb

Defined Under Namespace

Classes: ArrayValue, FalseValue, HashValue, NilValue, NumericValue, ObjectValue, StringValue, TrueValue

Constant Summary collapse

SPACE =
%q( )

Class Method Summary collapse

Class Method Details

.handle_next(options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/DSON/value.rb', line 33

def self.handle_next(options)
  word_array = options[:word_array]

  fail 'An error has occurred, this could be either user error or a bug. Please check your DSON is valid, and if it is, please raise a GitHub issue' if word_array.empty?

  first_word = word_array.shift

  if first_word == 'such'
    return HashValue.so_parse(
      word_array: word_array,
      parent_hash: {}
    )
  end
  if first_word == 'so'
    return ArrayValue.so_parse(
      word_array: word_array,
      parent_array: []
    )
  end
  return TrueValue.so_parse           if first_word == 'yes'
  return FalseValue.so_parse          if first_word == 'no'
  return NilValue.so_parse            if first_word == 'empty'
  return NumericValue.so_parse(first_word) unless first_word.start_with? '"'

  StringValue.so_parse(first_word)
end

.new(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/DSON/value.rb', line 16

def self.new(value)
  return HashValue.new(value)    if value.respond_to? :keys
  return ArrayValue.new(value)   if value.respond_to? :each
  return NilValue.instance       if value.nil?
  return TrueValue.instance      if value.is_a? TrueClass
  return FalseValue.instance     if value.is_a? FalseClass
  return NumericValue.new(value) if value.is_a? Integer or value.is_a? Float
  return StringValue.new(value)  if value.is_a? String
  ObjectValue.new(value)
end

.remove_first_and_last_elements(array) ⇒ Object



69
70
71
72
# File 'lib/DSON/value.rb', line 69

def self.remove_first_and_last_elements(array)
  array.pop
  array.shift
end

.remove_first_and_last_words(string) ⇒ Object

Class methods can’t be accessed from subclasses if protected… Find better way if possible



62
63
64
65
66
67
# File 'lib/DSON/value.rb', line 62

def self.remove_first_and_last_words(string)
  non_whitespace_elements = string.split(' ')
  non_whitespace_elements.pop
  non_whitespace_elements.shift
  non_whitespace_elements.join(' ')
end

.so_parse(dson_string) ⇒ Object



27
28
29
30
31
# File 'lib/DSON/value.rb', line 27

def self.so_parse(dson_string)
  handle_next(
    word_array: dson_string.scan(/(?:"(?:\\.|[^"])*"|[^" ,!\?])+|\.(?!\d)+/),
  )
end