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



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
129
130
131
132
133
134
# File 'lib/language/output.rb', line 102

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



43
44
45
# File 'lib/language/output.rb', line 43

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

#[]=(key, value) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/language/output.rb', line 55

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



35
36
37
# File 'lib/language/output.rb', line 35

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

#inspectObject



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

def inspect
  raw.inspect
end

#merge(other) ⇒ Object



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
95
96
97
98
99
100
# File 'lib/language/output.rb', line 68

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)


39
40
41
# File 'lib/language/output.rb', line 39

def present?
  !nil?
end

#to_rawObject



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

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



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

def to_s
  raw.to_s
end