Method: FilterTable::Factory#connect

Defined in:
lib/utils/filter.rb

#connect(resource, table_accessor) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/utils/filter.rb', line 142

def connect(resource, table_accessor)
  # create the table structure
  connectors = @connectors
  struct_fields = connectors.values.map(&:field_name)
  connector_blocks = connectors.map do |method, c|
    [method.to_sym, create_connector(c)]
  end

  # the struct to hold single items from the #entries method
  entry_struct = Struct.new(*struct_fields.map(&:to_sym)) do
    attr_accessor :__filter
    def to_s # rubocop:disable Lint/NestedMethodDefinition
      @__filter || super
    end
  end unless struct_fields.empty?

  # the main filter table
  table = Class.new(Table) {
    connector_blocks.each do |x|
      define_method x[0], &x[1]
    end

    define_method :new_entry do |hashmap, filter = ''|
      return entry_struct.new if hashmap.nil?
      res = entry_struct.new(*struct_fields.map { |x| hashmap[x] })
      res.__filter = filter
      res
    end
  }

  # define all access methods with the parent resource
  accessors = @accessors + @connectors.keys
  accessors.each do |method_name|
    resource.send(:define_method, method_name.to_sym) do |*args, &block|
      filter = table.new(self, method(table_accessor).call, ' with')
      filter.method(method_name.to_sym).call(*args, &block)
    end
  end
end