Class: FilterTable::Table

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, params, filters) ⇒ Table

Returns a new instance of Table.



44
45
46
47
48
49
# File 'lib/utils/filter.rb', line 44

def initialize(resource, params, filters)
  @resource = resource
  @params = params
  @params = [] if @params.nil?
  @filters = filters
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



43
44
45
# File 'lib/utils/filter.rb', line 43

def params
  @params
end

#resourceObject (readonly)

Returns the value of attribute resource.



43
44
45
# File 'lib/utils/filter.rb', line 43

def resource
  @resource
end

Instance Method Details

#entriesObject



78
79
80
81
82
83
# File 'lib/utils/filter.rb', line 78

def entries
  f = @resource.to_s + @filters.to_s + ' one entry'
  @params.map do |line|
    new_entry(line, f)
  end
end

#get_field(field) ⇒ Object



85
86
87
88
89
# File 'lib/utils/filter.rb', line 85

def get_field(field)
  @params.map do |line|
    line[field]
  end
end

#new_entry(*_) ⇒ Object



72
73
74
75
76
# File 'lib/utils/filter.rb', line 72

def new_entry(*_)
  raise "#{self.class} must not be used on its own. It must be inherited "\
       'and the #new_entry method must be implemented. This is an internal '\
       'error and should not happen.'
end

#to_sObject Also known as: inspect



91
92
93
# File 'lib/utils/filter.rb', line 91

def to_s
  @resource.to_s + @filters
end

#where(conditions = {}, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/utils/filter.rb', line 51

def where(conditions = {}, &block)
  return self if !conditions.is_a?(Hash)
  return self if conditions.empty? && !block_given?

  filters = ''
  table = @params
  conditions.each do |field, condition|
    filters += " #{field} == #{condition.inspect}"
    table = filter_lines(table, field, condition)
  end

  if block_given?
    table = table.find_all { |e| new_entry(e, '').instance_eval(&block) }
    src = Trace.new
    src.instance_eval(&block)
    filters += Trace.to_ruby(src)
  end

  self.class.new(@resource, table, @filters + filters)
end