Class: DataContainer

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

Defined Under Namespace

Classes: AttributeError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ DataContainer

Returns a new instance of DataContainer.



14
15
16
# File 'lib/data_container.rb', line 14

def initialize(*args)
  super    # Instance of Struct/new class constructor
end

Class Method Details

.new(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/data_container.rb', line 4

def self.new(*args)
  if args.size == 1 && args.first.is_a?(Hash)    # actually given a hash, not an array of values
    new_class = super(*args.first.keys)
    new_class.new(*args.first.values)
  else
    new_class = super(*args)   # Struct constructor
    new_class.new              # send back an INSTANCE, not a class
  end
end

Instance Method Details

#get(ivar) ⇒ Object



18
19
20
# File 'lib/data_container.rb', line 18

def get(ivar)
  do_if_included(ivar) { send(ivar) }
end

#include?(var) ⇒ Boolean

Returns:

  • (Boolean)


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

def include?(var)
  data.include?(var.to_sym)
end

#nameObject



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

def name
  'DataContainer'
end

#populate_from_hash(hash) ⇒ Object



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

def populate_from_hash(hash)
  hash.each { |var, val| set(var, val) }
end

#set(ivar, value) ⇒ Object



22
23
24
# File 'lib/data_container.rb', line 22

def set(ivar, value)
  do_if_included(ivar) { send("#{ivar}=", value) }
end

#to_sObject Also known as: inspect



26
27
28
# File 'lib/data_container.rb', line 26

def to_s
  "#<#{name} " + variable_value_pairs_string.join(', ') + '>'
end