Sha256: 7ba1f79bae8e0fcd0199cf72309957660b67881094403acaef46fa2a0336708e

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'yaml'

module Oscal
  module Serializer
    def to_h
      instance_variables.each_with_object({}) do |var, hash|
        var_name = var.to_s.delete('@')
        hash[var_name] = instance_variable_get(var)
      end
    end

    def to_json(*args)
      to_h.to_json(*args)
    end

    def to_yaml
      to_h.to_yaml
    end

    def to_xml(builder)
      raise NotImplementedError, "#{self.class}#to_xml not implemented!"
    end

    def self.included(klass)
      klass.extend(ClassMethods)
    end

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

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

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

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

      private

      def attribute_names
        @attribute_names ||= []
      end

      def attr_serializable(*attrs)
        attrs.each do |attr|
          attribute_names << attr.to_sym
          define_method(attr) { instance_variable_get("@#{attr}") }
          define_method("#{attr}=") { |value| instance_variable_set("@#{attr}", value) }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oscal-0.1.0 lib/oscal/serializer.rb