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



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

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

#==(other) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/language/output.rb', line 64

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

#cloneObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

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

#inspectObject



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

def inspect
  raw.inspect
end

#merge(other) ⇒ Object



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
101
102
103
# File 'lib/language/output.rb', line 75

def merge(other)
  case @raw
  when NilClass
    @raw = other.raw
  when String
    case other.raw
    when String
      @raw += other.raw
    when Array, 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, 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)


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

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

#to_rawObject



34
35
36
37
38
39
40
41
42
# File 'lib/language/output.rb', line 34

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



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

def to_s
  raw.to_s
end