Sha256: 0d6515b6a852f7ecf5f25ef93578e566884ec44a82c5d355e81d0f7c034450cb

Contents?: true

Size: 1.7 KB

Versions: 22

Compression:

Stored size: 1.7 KB

Contents

module Reviewed
  module Embeddable

    def self.included(klass)
      klass.extend(Reviewed::Embeddable::ClassMethods)
    end

    class << self

      def embedded_class(name, opts_name=nil)
        name = opts_name ? opts_name : embedded_name(name)
        name.constantize
      end

      def embedded_name(name)
        ["Reviewed", name.singularize.classify].join("::")
      end
    end

    def objectify(data)
      data = objectify_has_many(data)
      data = objectify_has_one(data)
      return data
    end

    def objectify_has_many(json)
      self.class._embedded_many.each do |map|
        key, value = [map.keys[0], map.values[0]]

        if json.has_key?(key)
          json[key] = json[key].map do |obj|
            value.new(obj)
          end
        end
      end
      return json
    end

    def objectify_has_one(json)
      self.class._embedded_one.each do |map|
        key, value = [map.keys[0], map.values[0]]

        if json.has_key?(key)
          json[key] = value.new(json[key])
        end
      end
      return json
    end

    module ClassMethods
      attr_accessor :_embedded_many, :_embedded_one

      def has_many(name, opts={})
        klass = Reviewed::Embeddable.embedded_class(name.to_s, opts[:class_name])
        association = opts[:as] || name
        _embedded_many << { association.to_s => klass }
      end

      def has_one(name, opts={})
        klass = Reviewed::Embeddable.embedded_class(name.to_s, opts[:class_name])
        association = opts[:as] || name
        _embedded_one << { association.to_s => klass }
      end

      def _embedded_many
        @_embedded_many ||= []
      end

      def _embedded_one
        @_embedded_one ||= []
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
reviewed-0.2.2 lib/reviewed/embeddable.rb
reviewed-0.2.1 lib/reviewed/embeddable.rb
reviewed-0.2.0 lib/reviewed/embeddable.rb
reviewed-0.1.4 lib/reviewed/embeddable.rb
reviewed-0.1.32 lib/reviewed/embeddable.rb
reviewed-0.1.31 lib/reviewed/embeddable.rb
reviewed-0.1.30 lib/reviewed/embeddable.rb
reviewed-0.1.29 lib/reviewed/embeddable.rb
reviewed-0.1.28 lib/reviewed/embeddable.rb
reviewed-0.1.27 lib/reviewed/embeddable.rb
reviewed-0.1.26 lib/reviewed/embeddable.rb
reviewed-0.1.25 lib/reviewed/embeddable.rb
reviewed-0.1.24 lib/reviewed/embeddable.rb
reviewed-0.1.23 lib/reviewed/embeddable.rb
reviewed-0.1.22 lib/reviewed/embeddable.rb
reviewed-0.1.21 lib/reviewed/embeddable.rb
reviewed-0.1.2 lib/reviewed/embeddable.rb
reviewed-0.1.1 lib/reviewed/embeddable.rb
reviewed-0.1.0 lib/reviewed/embeddable.rb
reviewed-0.0.9 lib/reviewed/embeddable.rb