Sha256: 272d8160b599a298a876caf8d7c65e6780e537ee9f351b4da4a09a57ec5e544b

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 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 resource_id(parent).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.class.id_key ? parent.send(parent.class.id_key.name) : nil
        else
          options[:resource_id] ? parent.instance_eval(&options[:resource_id])
                                : parent.__send__("#{self.name}_id")
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rest_model-0.1.20 lib/rest_model/key/relation/response.rb
rest_model-0.1.19 lib/rest_model/key/relation/response.rb
rest_model-0.1.18 lib/rest_model/key/relation/response.rb
rest_model-0.1.17 lib/rest_model/key/relation/response.rb