Class: FilterTable::Factory
- Inherits:
-
Object
- Object
- FilterTable::Factory
- Defined in:
- lib/utils/filter.rb
Defined Under Namespace
Classes: Connector
Instance Method Summary collapse
- #add(method_name, opts = {}, &block) ⇒ Object
- #add_accessor(method_name) ⇒ Object
- #connect(resource, table_accessor) ⇒ Object
-
#initialize ⇒ Factory
constructor
A new instance of Factory.
Constructor Details
#initialize ⇒ Factory
Returns a new instance of Factory.
138 139 140 141 |
# File 'lib/utils/filter.rb', line 138 def initialize @accessors = [] @connectors = {} end |
Instance Method Details
#add(method_name, opts = {}, &block) ⇒ Object
191 192 193 194 195 196 197 198 199 |
# File 'lib/utils/filter.rb', line 191 def add(method_name, opts = {}, &block) if method_name.nil? throw RuntimeError, "Called filter.add for resource #{@resource} with method name nil!" end @connectors[method_name.to_sym] = Connector.new(opts[:field] || method_name, block, opts) self end |
#add_accessor(method_name) ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/utils/filter.rb', line 183 def add_accessor(method_name) if method_name.nil? throw RuntimeError, "Called filter.add_delegator for resource #{@resource} with method name nil!" end @accessors.push(method_name) self end |
#connect(resource, table_accessor) ⇒ Object
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 181 |
# File 'lib/utils/filter.rb', line 143 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 |