Class: LSVplus::BaseContainer

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

Direct Known Subclasses

File, Record

Defined Under Namespace

Classes: MissingAttribute, UnknownAttribute

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ BaseContainer

Returns a new instance of BaseContainer.



25
26
27
28
# File 'lib/lsv_plus/base_container.rb', line 25

def initialize(attributes)
  validate attributes
  set_attributes attributes
end

Instance Method Details

#set_attributes(attributes) ⇒ Object



34
35
36
37
38
39
# File 'lib/lsv_plus/base_container.rb', line 34

def set_attributes(attributes)
  attributes.each do |key, value|
    raise UnknownAttribute, key unless self.class::ATTRIBUTES.include?(key)
    instance_variable_set("@#{key}", value)
  end
end

#validate(attributes) ⇒ Object



30
31
32
# File 'lib/lsv_plus/base_container.rb', line 30

def validate(attributes)
  validate_presence_of_required_attributes(attributes)
end

#validate_presence_of_required_attributes(attributes) ⇒ Object



41
42
43
44
45
# File 'lib/lsv_plus/base_container.rb', line 41

def validate_presence_of_required_attributes(attributes)
  self.class::ATTRIBUTES.each do |attribute|
    raise MissingAttribute, attribute unless attributes.key?(attribute)
  end
end