lib/hario.rb in hario-0.3.0 vs lib/hario.rb in hario-0.3.1

- old
+ new

@@ -2,14 +2,20 @@ require "hario/behaviours/filter" require "hario/behaviours/pluck" module Hario module Filterable - def search(filters, pluck = nil) + HARIO_APPLY_TYPES = %w( filters pluck ).map(&:to_sym) + + attr_reader :hario_attributes_list + + def search(filters, pluck = []) + pluck = pluck.reject{ |p| p.nil? || p.empty? } + s = all s = s.apply_filters(filters) if filters - s = s.apply_pluck(pluck) if pluck + s = s.apply_pluck(pluck) if pluck.any? s end def apply_filters(filters) fp = FilterParser.new(filters, self) @@ -30,7 +36,24 @@ end def hash_pluck(*keys) pluck(*keys).map{ |vals| Hash[keys.zip(Array(vals))] } end + + def hario_attributes(types, only: nil, except: nil) + @hario_attributes_list ||= {} + Array.wrap(types).each do |t| + raise_if_not_hario_type!(t) + @hario_attributes_list[t] = + { only: Array.wrap(only), except: Array.wrap(except) } + end + end + + private + def raise_if_not_hario_type!(type) + unless HARIO_APPLY_TYPES.include?(type) + raise ArgumentError, "#{type} is not one of " \ + "#{HARIO_APPLY_TYPES.map(&:inspect).join(', ')}" + end + end end end