Sha256: 74d6cbe294b5e262076be8181e1587b09816fb5bb6576d9073347b8e2827f36b

Contents?: true

Size: 650 Bytes

Versions: 4

Compression:

Stored size: 650 Bytes

Contents

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

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

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

      def to_json
        to_hash.to_json
      end

      private
      attr_reader :object
    end

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

      attr_reader :definition
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
supa-0.1.4 lib/supa/representable.rb
supa-0.1.3 lib/supa/representable.rb
supa-0.1.2 lib/supa/representable.rb
supa-0.1.1 lib/supa/representable.rb