Class: Bruhl::Object

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents, tag = nil) ⇒ Object

Returns a new instance of Object.



145
146
147
148
# File 'lib/bruhl.rb', line 145

def initialize(contents, tag = nil)
  @contents = contents
  @tag = tag ? tag.strip : nil
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



144
145
146
# File 'lib/bruhl.rb', line 144

def contents
  @contents
end

#tagObject (readonly)

Returns the value of attribute tag.



144
145
146
# File 'lib/bruhl.rb', line 144

def tag
  @tag
end

Instance Method Details

#==(other) ⇒ Object



187
188
189
190
191
# File 'lib/bruhl.rb', line 187

def ==(other)
  other.is_a?(self.class)       &&
    @contents == other.contents &&
    @tag      == other.tag
end

#empty?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/bruhl.rb', line 150

def empty?
  @contents.empty?
end

#fetch(key, default = nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/bruhl.rb', line 165

def fetch(key, default = nil)
  results = fetch_all(key)
  if results.size > 1
    fail("Found too many #{key} sections!")
  elsif results.empty?
    default
  else
    results.first
  end
end

#fetch_all(key) ⇒ Object



158
159
160
161
162
163
# File 'lib/bruhl.rb', line 158

def fetch_all(key)
  @contents.select do |elem|
    next unless elem.is_a?(Relation)
    elem.left.to_s == key
  end.map(&:right)
end

#inspectObject



193
194
195
# File 'lib/bruhl.rb', line 193

def inspect
  to_s
end

#only(key) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/bruhl.rb', line 176

def only(key)
  results = fetch_all(key)
  if results.size > 1
    fail("Found too many #{key} sections!")
  elsif results.empty?
    fail("Found zero #{key} sections!")
  else
    results.first
  end
end

#sizeObject



154
155
156
# File 'lib/bruhl.rb', line 154

def size
  @contents.size
end

#to_sObject



197
198
199
200
# File 'lib/bruhl.rb', line 197

def to_s
  str = contents.map(&:to_s).join(', ')
  '{' + (@tag ? "#{@tag}: #{str}" : str) + '}'
end