Class: FilterArray

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/filter_array.rb

Overview

copyright: 2015, Chef Software, Inc. author: Stephan Renatus

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ FilterArray

Returns a new instance of FilterArray.



9
10
11
# File 'lib/utils/filter_array.rb', line 9

def initialize(rules)
  @rules = rules
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



24
25
26
# File 'lib/utils/filter_array.rb', line 24

def method_missing(meth, *args)
  field(meth, args[0])
end

Instance Attribute Details

#rulesObject Also known as: content

Returns the value of attribute rules.



6
7
8
# File 'lib/utils/filter_array.rb', line 6

def rules
  @rules
end

Instance Method Details

#field(key, value = nil) ⇒ Object

allows for chaining .field(‘arch’, ‘b32’).field(‘key’, ‘access’).rules



15
16
17
18
19
20
21
22
# File 'lib/utils/filter_array.rb', line 15

def field(key, value = nil)
  if value
    res = rules.find_all { |r| r[key.to_sym] == value }
    FilterArray.new(res)
  else
    rules.map { |h| h[key.to_sym] }.uniq
  end
end