Class: Language::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/language/output.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw = nil) ⇒ Output

Returns a new instance of Output.



5
6
7
# File 'lib/language/output.rb', line 5

def initialize(raw = nil)
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



3
4
5
# File 'lib/language/output.rb', line 3

def raw
  @raw
end

Class Method Details

.from_raw(raw) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/language/output.rb', line 9

def self.from_raw(raw)
  if raw.is_a?(Array)
    new(raw.map { |element| from_raw(element) })
  elsif raw.is_a?(Hash)
    new(raw.map { |key, value| [key, from_raw(value)] }.to_h)
  else
    new(raw)
  end
end

Instance Method Details

#<<(other) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/language/output.rb', line 96

def <<(other)
  case @raw
  when NilClass
    case other.raw
    when String
      @raw = [other]
    when Array
      @raw = [other]
    when Hash
      @raw = [other]
    end
  when String
    case other.raw
    when String
      @raw = @raw + other.raw
    when Array
      @raw = [other]
    when Hash
      @raw = [other]
    end
  when Array
    @raw << other
  when Hash
    case other.raw
    when String
      return
    when Array
      return
    when Hash
      @raw.merge!(other.raw)
    end
  end
end

#==(other) ⇒ Object



37
38
39
# File 'lib/language/output.rb', line 37

def ==(other)
  other.is_a?(Output) ? raw == other.raw : raw == other
end

#[]=(key, value) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/language/output.rb', line 49

def []=(key, value)
  case @raw
  when NilClass
    @raw = { key => value }
  when String
    @raw = { key => value }
  when Array
    @raw << Output.new({ key => value })
  when Hash
    @raw[key] = value
  end
end

#cloneObject



29
30
31
# File 'lib/language/output.rb', line 29

def clone
  Output.new(@raw.clone)
end

#inspectObject



45
46
47
# File 'lib/language/output.rb', line 45

def inspect
  raw.inspect
end

#merge(other) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/language/output.rb', line 62

def merge(other)
  case @raw
  when NilClass
    @raw = other.raw
  when String
    case other.raw
    when String
      @raw = @raw + other.raw
    when Array
      @raw = other.raw
    when Hash
      @raw = other.raw
    end
  when Array
    case other.raw
    when String
      return
    when Array
      @raw = @raw + other.raw
    when Hash
      @raw << other
    end
  when Hash
    case other.raw
    when String
      return
    when Array
      return
    when Hash
      @raw.merge!(other.raw)
    end
  end
end

#present?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/language/output.rb', line 33

def present?
  @raw != nil
end

#to_rawObject



19
20
21
22
23
24
25
26
27
# File 'lib/language/output.rb', line 19

def to_raw
  if raw.is_a?(Array)
    raw.map(&:to_raw)
  elsif raw.is_a?(Hash)
    raw.map { |key, value| [key, value.to_raw] }.to_h
  else
    raw
  end
end

#to_sObject



41
42
43
# File 'lib/language/output.rb', line 41

def to_s
  raw.to_s
end