Sha256: 39db4942abb0e96e9aac41e90b865757f8b995209483cf9200e180033882abcb

Contents?: true

Size: 687 Bytes

Versions: 1

Compression:

Stored size: 687 Bytes

Contents

module Supa
  module Representable
    def self.included(base)
      base.send :include, InstanceMethods
      base.extend ClassMethods
    end

    module InstanceMethods
      def initialize(representee)
        @representee = representee
      end

      def to_hash
        Supa::Builder.new(representee, representer: self, tree: {}).tap do |builder|
          builder.instance_exec(&self.class.definition)
        end.to_hash
      end

      def to_json
        Oj.dump(to_hash, mode: :compat)
      end

      attr_reader :representee
    end

    module ClassMethods
      def define(&block)
        @definition = block
      end

      attr_reader :definition
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
supa-0.5.0 lib/supa/representable.rb