Sha256: 1457677f6fb7734c59ac1324969b84b847be29c229318bc6d50cf0c47715f70b

Contents?: true

Size: 687 Bytes

Versions: 2

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: :strict)
      end

      attr_reader :representee
    end

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

      attr_reader :definition
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
supa-0.5.2 lib/supa/representable.rb
supa-0.5.1 lib/supa/representable.rb