Class: PopCap::TagStruct

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pop_cap/tag_struct.rb

Overview

Public: This class is a simple implementation of a Ruby struct like object. It has a custom #to_s method & creates getter methods.

new - This method takes a hash. It raises an error if anything else is provided.

Examples

ts = TagStruct.new({artist: 'Artist', date: 1984})
ts.artist => 'Artist'
ts.date => 1984

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ TagStruct

Returns a new instance of TagStruct.

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/pop_cap/tag_struct.rb', line 16

def initialize(hash)
  raise(ArgumentError, argument_error_message) unless hash.kind_of?(Hash)
  @hash = hash
  define_instance_methods
end

Instance Method Details

#each(&block) ⇒ Object

Public: This method is a custom each iterator for tags.



36
37
38
39
40
# File 'lib/pop_cap/tag_struct.rb', line 36

def each(&block)
  @hash.each do |elem|
    block_given? ? block.call(elem) : elem
  end
end

#to_sObject

Public: This method shows the class name & hash values as a string.

Examples

ts = TagStruct.new({artist: 'Artist', date: 1984})
puts ts
#=> '#<PopCap::TagStruct artist: Artist, date: 1984>'


29
30
31
32
# File 'lib/pop_cap/tag_struct.rb', line 29

def to_s
  methods = @hash.map { |key,val| %(#{key}: #{val}) }.join(', ')
  "#<#{self.class.name} " + methods + '>'
end