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.



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

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.



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

def params
  @params
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

Instance Method Details

#entriesObject



120
121
122
123
124
125
# File 'lib/utils/filter.rb', line 120

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



127
128
129
130
131
# File 'lib/utils/filter.rb', line 127

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

#new_entry(*_) ⇒ Object



114
115
116
117
118
# File 'lib/utils/filter.rb', line 114

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



133
134
135
# File 'lib/utils/filter.rb', line 133

def to_s
  @resource.to_s + @filters
end

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/utils/filter.rb', line 93

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