Class: FancyStruct

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ FancyStruct

Returns a new instance of FancyStruct.



12
13
14
15
16
17
18
# File 'lib/fancystruct.rb', line 12

def initialize(*args)
  if args.size == 1 && args.first.is_a?(Hash)
    self.set(args.first)
  else
    self.class.attribs.each_with_index { |name, i| self.set_attrib(name, args[i]) if args[i] }
  end
end

Class Method Details

.attribs(*attribs) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/fancystruct.rb', line 3

def self.attribs(*attribs)
  @attribs ||= []
  unless attribs.empty?
    self.send :attr_accessor, *attribs
    @attribs += attribs 
  end
  @attribs
end

Instance Method Details

#set(attribs) ⇒ Object



20
21
22
23
# File 'lib/fancystruct.rb', line 20

def set(attribs)
  attribs.each { |name,val| self.set_attrib(name, val) }
  self
end

#set_attrib(name, val) ⇒ Object



25
26
27
# File 'lib/fancystruct.rb', line 25

def set_attrib(name,val)
  self.send(name.to_s+'=', val)
end