Sha256: d9ac17a57e4411ca10c5b7bc13c2b8fbe825d8415a7921cdb69a24e26c2cda0b

Contents?: true

Size: 913 Bytes

Versions: 11

Compression:

Stored size: 913 Bytes

Contents

module MotionPrime
  module FilterMixin
    def filter_array(data, find_options = {}, sort_options = nil)
      data = data.select do |entity|
        find_options.all? do |field, value|
          if value.is_a?(Array)
            value.include?(entity.info[field].to_s)
          else
            entity.info[field] == value
          end
        end
      end if find_options.present?

      data.sort! do |a, b|
        left_part = []
        right_part = []

        sort_options[:sort].each do |(k,v)|
          left = a.send(k)
          right = b.send(k)
          if left.class != right.class
            left = left.to_s
            right = right.to_s
          end
          left, right = right, left if v.to_s == 'desc'
          left_part << left
          right_part << right
        end
        left_part <=> right_part
      end if sort_options.try(:[], :sort).present?
      data
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
motion-prime-1.0.7 motion-prime/models/_filter_mixin.rb
motion-prime-1.0.6 motion-prime/models/_filter_mixin.rb
motion-prime-1.0.5 motion-prime/models/_filter_mixin.rb
motion-prime-1.0.4 motion-prime/models/_filter_mixin.rb
motion-prime-1.0.3 motion-prime/models/_filter_mixin.rb
motion-prime-1.0.2 motion-prime/models/_filter_mixin.rb
motion-prime-1.0.1 motion-prime/models/_filter_mixin.rb
motion-prime-1.0.0 motion-prime/models/_filter_mixin.rb
motion-prime-0.9.9.2 motion-prime/models/_filter_mixin.rb
motion-prime-0.9.9.1 motion-prime/models/_filter_mixin.rb
motion-prime-0.9.9 motion-prime/models/_filter_mixin.rb