Class: Bruhl::Object
- Inherits:
-
Object
- Object
- Bruhl::Object
- Defined in:
- lib/bruhl.rb
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #empty? ⇒ Boolean
- #fetch(key, default = nil) ⇒ Object
- #fetch_all(key) ⇒ Object
-
#initialize(contents, tag = nil) ⇒ Object
constructor
A new instance of Object.
- #inspect ⇒ Object
- #only(key) ⇒ Object
- #size ⇒ Object
- #to_s ⇒ Object
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
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
144 145 146 |
# File 'lib/bruhl.rb', line 144 def contents @contents end |
#tag ⇒ Object (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
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 |
#inspect ⇒ Object
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 |
#size ⇒ Object
154 155 156 |
# File 'lib/bruhl.rb', line 154 def size @contents.size end |
#to_s ⇒ Object
197 198 199 200 |
# File 'lib/bruhl.rb', line 197 def to_s str = contents.map(&:to_s).join(', ') '{' + (@tag ? "#{@tag}: #{str}" : str) + '}' end |