Sha256: 92dab74452c0718c28757fede9b1e91ca1843245fa1000a128272c7fd7ad0622

Contents?: true

Size: 620 Bytes

Versions: 1

Compression:

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

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