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.



7
8
9
# File 'lib/language/output.rb', line 7

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

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

Class Method Details

.from_raw(raw) ⇒ Object



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

def self.from_raw(raw)
  if raw.is_a?(Array)
    new(raw.map { |element| from_raw(element) })
  elsif raw.is_a?(Hash)
    new(raw.transform_values { |value| from_raw(value) })
  else
    new(raw)
  end
end

Instance Method Details

#<<(other) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/language/output.rb', line 115

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 += other.raw
    when Array
      @raw = [other]
    when Hash
      @raw = [other]
    end
  when Array
    @raw << other
  when Hash
    case other.raw
    when String
      nil
    when Array
      nil
    when Hash
      @raw.merge!(other.raw)
    end
  end
end

#==(other) ⇒ Object



56
57
58
# File 'lib/language/output.rb', line 56

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

#[]=(key, value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/language/output.rb', line 68

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



48
49
50
# File 'lib/language/output.rb', line 48

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

#empty?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/language/output.rb', line 25

def empty?
  case @raw
  when NilClass
    true
  when String
    raw.empty?
  when Array
    raw.empty?
  when Hash
    raw.empty?
  end
end

#inspectObject



64
65
66
# File 'lib/language/output.rb', line 64

def inspect
  raw.inspect
end

#merge(other) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/language/output.rb', line 81

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

#nil?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/language/output.rb', line 21

def nil?
  raw.nil?
end

#present?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/language/output.rb', line 52

def present?
  !nil? && !empty?
end

#to_rawObject



38
39
40
41
42
43
44
45
46
# File 'lib/language/output.rb', line 38

def to_raw
  if raw.is_a?(Array)
    raw.map(&:to_raw)
  elsif raw.is_a?(Hash)
    raw.transform_values(&:to_raw)
  else
    raw
  end
end

#to_sObject



60
61
62
# File 'lib/language/output.rb', line 60

def to_s
  raw.to_s
end