Module: Oscal::Serializer::ClassMethods

Defined in:
lib/oscal/serializer.rb

Instance Method Summary collapse

Instance Method Details

#from_h(data) ⇒ Object



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

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

#from_json(json_string) ⇒ Object



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

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

#from_xml(xml_element) ⇒ Object

Raises:

  • (NotImplementedError)


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

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

#from_yaml(yaml_string) ⇒ Object



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

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



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

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