Class: Oscal::WithId

Inherits:
BaseClass show all
Includes:
Serializer
Defined in:
lib/oscal/with_id.rb

Constant Summary collapse

KEY =
%i(val)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializer

included, #to_h, #to_json, #to_xml, #to_yaml

Methods inherited from BaseClass

#set_value

Constructor Details

#initialize(options = {}) ⇒ WithId

Returns a new instance of WithId.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/oscal/with_id.rb', line 22

def initialize(options = {})
  klass = self.class

  unless options.is_a? Hash
    options = { KEY.first.to_s => options }
  end

  options.each_pair.each do |key, val|
    key_name = key.gsub("-", "_")

    unless KEY.include?(key_name.to_sym)
      raise UnknownAttributeError.new("Unknown key `#{key}` in #{klass.name}")
    end

    send("#{key_name}=", val)
  end
end

Class Method Details

.wrap(obj) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/oscal/with_id.rb', line 13

def self.wrap(obj)
  return obj if obj.is_a? WithId
  return WithId.new(obj) unless obj.is_a? Array

  obj.map do |x|
    WithId.wrap(x)
  end
end