OSC / ood_appkit

https://osc.github.io/Open-OnDemand/
MIT License
1 stars 2 forks source link

Add guard clause and coercion to Clusters#[] #33

Closed ericfranz closed 7 years ago

ericfranz commented 7 years ago
    # Find object in list from object's id
    # @param id [Object] id of cluster decorator object
    # @return [ClusterDecorator, nil] object if found
    def [](id)
      @clusters.detect { |v| v == id }
    end

Should probably be something like

    # Find object in list from object's id
    # @param id [Object] id of cluster decorator object
    # @return [ClusterDecorator, nil] object if found
    def [](id)
      @clusters.detect { |v| v == id.to_sym }
    rescue NoMethodError
      nil
    end

or

    # Find object in list from object's id
    # @param id [Object] id of cluster decorator object
    # @return [ClusterDecorator, nil] object if found
    def [](id)
      @clusters.detect { |v| v == id.to_sym } if id
    end

That way we stop littering these guard clauses all over our code whenever we want to fetch a cluster from the clusters list.

nickjer commented 7 years ago

The coercion is handled by the object itself:

https://github.com/OSC/ood_appkit/blob/b91a54d59173f222b6fed1fc30be232c508d50b2/lib/ood_appkit/cluster_decorator.rb#L39-L44

That way we stop littering these guard clauses all over our code whenever we want to fetch a cluster from the clusters list.

What code is using guard clauses?

nickjer commented 7 years ago

Clusters is deprecated in this gem.