softlayer / softlayer-ruby

http://softlayer.github.io/softlayer-ruby/
MIT License
54 stars 35 forks source link

Filter for multiple criteria #77

Closed huskercane closed 9 years ago

huskercane commented 9 years ago

Hello,

We are using 3.* version of API. We converted all our code to new filter code. It works great.

I am not sure how we filter for multiple criteria, like give me all objects that fit this and that criteria.

We used to have this filter before object_filter = { 'itemPrices' => { 'categories' => { 'id' => { 'operation' => global_ipv4_cat_code } }, 'item' => { 'capacity' => { 'operation' => quantity } } } }

How do we implement this with new API?

I tried this object_filter = SoftLayer::ObjectFilter.new do |filter| filter.accept('itemPrices.categories.id').when_it(is(global_ipv4_cat_code))

    # this does not work
    filter.accept('item.capacity').when_it(is(quantity))
  end

but it does not work, I get a list of all quantities, not just one quantity.

I am sure others will be looking for same information, it will be helpful to add it do code comment and documentation.

SLsthompson commented 9 years ago

I may not have time to look at it in detail until tomorrow and for that I apologize.

Off the top of my head (without trying to write the code) at the very least you should be able to create an empty object filter and then use set_criteria_for_key_path on each of your keys (itemPrices.categories.id and item.capacity in the sample you gave). I believe the criteria argument could simply the hashes for each of those key paths (e.g. { 'operation' => global_ipv4_cat_code } for itemPrices.categories.id).

Once you have an object filter you can call it's "to_h" method. The result should be identical to the working hash-style filter you were using before.

huskercane commented 9 years ago

Thank You

  object_filter.set_criteria_for_key_path('itemPrices.categories.id', { 'operation' => global_ipv4_cat_code })
  object_filter.set_criteria_for_key_path('itemPrices.item.capacity', { 'operation' => quantity })
  puts object_filter.to_h

works

also did

  object_filter = SoftLayer::ObjectFilter.new do |filter|
    filter.accept('itemPrices.categories.id').when_it(is(global_ipv4_cat_code))
    filter.accept('itemPrices.item.capacity').when_it(is(quantity))
  end

I had a bug in code, I needed itemPrices before item.capacity

Thank You for help. it will be good to add example to code comment and documentation