Sha256: f510b00ecd2bd2d9d347caf06389d5bee08c9ab352915794bec3a31a7c0c03d4

Contents?: true

Size: 1.06 KB

Versions: 12

Compression:

Stored size: 1.06 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)
        {rel: name, href: href(parent)}
      end

      private

      def relation_name(parent)
        relation = parent.class.relations.find do |key|
          key.resource_class == self.resource_class
        end
        relation.name or fail
      end

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

      def href(parent)
        id_key = parent.class.id_key.name
        has? ?
          "#{RestModel::Configuration.host}/#{parent.class.resource_name}/#{parent.send(id_key)}/#{relation_name(parent)}"
        : "#{RestModel::Configuration.host}/#{relation_name(parent).to_s.pluralize}/#{parent.send(id_key)}/#{parent.class.resource_name}"
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rest_model-0.1.11 lib/rest_model/key/relation/response.rb
rest_model-0.1.10 lib/rest_model/key/relation/response.rb
rest_model-0.1.9 lib/rest_model/key/relation/response.rb
rest_model-0.1.8 lib/rest_model/key/relation/response.rb
rest_model-0.1.7 lib/rest_model/key/relation/response.rb
rest_model-0.1.6 lib/rest_model/key/relation/response.rb
rest_model-0.1.5 lib/rest_model/key/relation/response.rb
rest_model-0.1.4 lib/rest_model/key/relation/response.rb
rest_model-0.1.3 lib/rest_model/key/relation/response.rb
rest_model-0.1.2 lib/rest_model/key/relation/response.rb
rest_model-0.1.1 lib/rest_model/key/relation/response.rb
rest_model-0.1.0 lib/rest_model/key/relation/response.rb