Sha256: 4584f40115c6736038da9bf04d9ccca124e05e655b9e07314fc5853959a251b3

Contents?: true

Size: 680 Bytes

Versions: 2

Compression:

Stored size: 680 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(representer: self, context: representee, tree: {}).tap do |builder|
          builder.instance_exec(&self.class.definition)
        end.to_hash
      end

      def to_json
        to_hash.to_json
      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.2.3 lib/supa/representable.rb
supa-0.2.2 lib/supa/representable.rb