lib/almodovar/resource_presenter/link.rb in almodovar-0.7.0 vs lib/almodovar/resource_presenter/link.rb in almodovar-0.8.0

- old
+ new

@@ -1,103 +1,103 @@ module Almodovar - - class ResourcePresenter::Link - - attr_reader :rel, :href, :expand_resource, :expand_args - - def initialize(*args) - @rel, @href, @expand_resource, @expand_args = args - end - - def to_xml(options = {}) - XmlSerializer.new(self, options.merge(:skip_instruct => true)).to_xml - end - - def as_json(options) - JsonSerializer.new(self, options).as_json - end - - def resource - resource_collection? ? resource_collection : single_resource - end - - def resource_collection? - expand_args.is_a?(Array) - end - - def resource_collection - ResourcePresenter::Collection.new(expand_resource, expand_args) - end - - def single_resource - expand_resource.new(*[expand_args].compact) - end - - def expand_resource? - expand_resource.present? - end - - class Serializer + class ResourcePresenter + class Link - attr_reader :link, :options + attr_reader :rel, :href, :expand_resource, :expand_args - def initialize(link, options) - @link = link - @options = options + def initialize(*args) + @rel, @href, @expand_resource, @expand_args = args end - def expands? - link.expand_resource? && - Array(options[:expand]).include?(link.rel) && - !Array(options[:dont_expand]).include?(link.href) + def to_xml(options = {}) + XmlSerializer.new(self, options.merge(:skip_instruct => true)).to_xml end - def dont_expand_link! - (options[:dont_expand] ||= []) << link.href + def as_json(options) + JsonSerializer.new(self, options).as_json end - end - - class XmlSerializer < Serializer + def resource + resource_collection? ? resource_collection : single_resource + end - def to_xml - builder.link :rel => link.rel, :href => link.href, &expand_block + def resource_collection? + expand_args.is_a?(Array) end - private + def resource_collection + ResourcePresenter::Collection.new(expand_resource, expand_args) + end - def builder - options[:builder] + def single_resource + expand_resource.new(*[expand_args].compact) end - def expand_block - Proc.new { expand_resource } if expands? + def expand_resource? + expand_resource.present? end - - def expand_resource - dont_expand_link! - link.resource.to_xml(options) - end - end - - class JsonSerializer < Serializer - - def as_json - ActiveSupport::OrderedHash.new.tap do |message| - message["#{link.rel}_link"] = link.href - message[link.rel] = expand_resource if expands? + class Serializer + + attr_reader :link, :options + + def initialize(link, options) + @link = link + @options = options end + + def expands? + link.expand_resource? && + Array(options[:expand]).include?(link.rel) && + !Array(options[:dont_expand]).include?(link.href) + end + + def dont_expand_link! + (options[:dont_expand] ||= []) << link.href + end + end - private - - def expand_resource - dont_expand_link! - link.resource.as_json(options) + class XmlSerializer < Serializer + + def to_xml + builder.link :rel => link.rel, :href => link.href, &expand_block + end + + private + + def builder + options[:builder] + end + + def expand_block + Proc.new { expand_resource } if expands? + end + + def expand_resource + dont_expand_link! + link.resource.to_xml(options) + end + end + + class JsonSerializer < Serializer + + def as_json + ActiveSupport::OrderedHash.new.tap do |message| + message["#{link.rel}_link"] = link.href + message[link.rel] = expand_resource if expands? + end + end + + private + + def expand_resource + dont_expand_link! + link.resource.as_json(options) + end + end + end - end - -end \ No newline at end of file +end