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



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
129
130
# File 'lib/language/output.rb', line 98

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



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

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

#[]=(key, value) ⇒ Object



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

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



31
32
33
# File 'lib/language/output.rb', line 31

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

#inspectObject



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

def inspect
  raw.inspect
end

#merge(other) ⇒ Object



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
95
96
# File 'lib/language/output.rb', line 64

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

#present?Boolean

Returns:

  • (Boolean)


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

def present?
  @raw != nil
end

#to_rawObject



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

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



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

def to_s
  raw.to_s
end