plexus / yaks

Ruby library for building hypermedia APIs
http://rubygems.org/gems/yaks
MIT License
236 stars 26 forks source link

options on attribute #113

Closed hallgren closed 8 years ago

hallgren commented 8 years ago

Add option to the attribute method to make the attribute optional.

attribute :breadcrumb, if: ->{ object.breadcrumb } do
  object.breadcrumb
end
plexus commented 8 years ago

For a minute there I thought you had written a pull request :) I think it's a good suggestion. If anyone feels like implementing it I'm happy to provide some mentoring.

hallgren commented 8 years ago

I talked to @bjoska about this and he wasn´t sure this was the intention when the options are set to nil in attribute.rb.

module Yaks
  class Mapper
    class Attribute
      include Attribs.new(:name, :block)
      include Util

      def self.create(name, _options = nil, &block) <--
        new(name: name, block: block)
      end

      def add_to_resource(resource, mapper, _context)
        if block
          attribute = Resolve(block, mapper)
        else
          attribute = mapper.load_attribute(name)
        end
        resource.merge_attributes(name => attribute)
      end
    end
  end
end

I´ll try to create a PR for this now when you say its ok :)

plexus commented 8 years ago

The reason _options is nil there and not {} is that it's an unused parameter (hence the underscore name), and using {} will cause mutant to complain.

bjonord commented 8 years ago

The reason _options is nil there and not {} is that it's an unused parameter (hence the underscore name)

That is what I said. :smile:

and using {} will cause mutant to complain.

I think Morgan got around this. :smile:

hallgren commented 8 years ago

I looked how link.rb worked and was lucky.