Sha256: 2c4df329f4cab3bb5501566e795ca660f9bc0ce30ede0ff14edcd9bfec70eff6

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

class RestModel
  class Relation
    module Response
      def to_resource(parent)
        included = parent.__send__(name)
        return {} unless visible?(parent) and included

        {name => resource_from_included(included)}
      end

      def to_relation(parent)
        href = href(parent)
        href ? {rel: name, href: href} : nil
      end

      private

      def resource_from_included(included)
        options = {root: false}
        one? ? included.resource(options) : included.map {|r| r.resource(options)}
      end

      def href(parent)
        if options[:href]
          parent.instance_eval(&options[:href])
        elsif parent.class.id_key.nil?
          nil
        else
          host = "#{RestModel::Configuration.host}"

          has? ? "#{host}/#{parent.class.resource_name}/#{resource_id(parent)}/#{name}"
               : "#{host}/#{name.to_s.pluralize}/#{resource_id(parent)}"
        end
      end

      def resource_id(parent)
        if has?
          parent.send(parent.class.id_key.name)
        else
          options[:resource_id] ? parent.instance_eval(&options[:resource_id])
                                : parent.__send__("#{self.name}_id")
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rest_model-0.1.16 lib/rest_model/key/relation/response.rb