Module: Oscal::Serializer::ClassMethods

Defined in:
lib/oscal/serializer.rb

Instance Method Summary collapse

Instance Method Details

#from_h(data) ⇒ Object



30
31
32
# File 'lib/oscal/serializer.rb', line 30

def from_h(data)
  new(*data.values_at(*attribute_names))
end

#from_json(json_string) ⇒ Object



34
35
36
37
# File 'lib/oscal/serializer.rb', line 34

def from_json(json_string)
  data = JSON.parse(json_string)
  from_h(data)
end

#from_xml(xml_element) ⇒ Object

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/oscal/serializer.rb', line 44

def from_xml(xml_element)
  raise NotImplementedError, "#{self}#from_xml not implemented!"
end

#from_yaml(yaml_string) ⇒ Object



39
40
41
42
# File 'lib/oscal/serializer.rb', line 39

def from_yaml(yaml_string)
  data = YAML.safe_load(yaml_string)
  from_h(data)
end

#safe_load_yaml(path) ⇒ Object

Psych >= 4 requires permitted_classes to load such classes github.com/ruby/psych/issues/533



50
51
52
53
54
55
56
57
# File 'lib/oscal/serializer.rb', line 50

def safe_load_yaml(path)
  YAML.load_file(
    path,
    permitted_classes: [::Time, ::Date, ::DateTime],
  )
rescue ArgumentError
  YAML.load_file(path)
end