Class: AdminIt::Filter

Inherits:
Object show all
Extended by:
DataBehavior, DisplayableName, ExtendIt::Base, ExtendIt::Dsl
Includes:
ExtendIt::Callbacks
Defined in:
lib/admin_it/filters/filter.rb

Direct Known Subclasses

EntityFilter, FieldFilter

Constant Summary collapse

REGEXP =
/
  (?<=\A|[,;|])\s*
  (?<full>
    (?<action>[!+\-])?
    (?<name>[a-zA-Z_][a-zA-Z0-9_]*)
    (?:\((?<params>[^)]*)\))?
  )
  \s*(?=[,;|]|\z)
/x
ARGUMENT_REGEXP =
/
  (?<=\A|[,;|])\s*
  (?:
    (?:(?<action>[+\-])|(?:(?<option>[a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*))?
    (?<token>
      (?:'(?:[^\\']|\\.)*')|
      (?:"(?:[^\\"]|\\.)*")|
      (?:[^,;|\s]+)
    )
  )
  \s*(?=[,;|]|\z)
/x

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilter

Returns a new instance of Filter.



96
97
98
# File 'lib/admin_it/filters/filter.rb', line 96

def initialize
  run_callbacks :initialize
end

Class Attribute Details

.filter_nameObject (readonly)

Returns the value of attribute filter_name.



41
42
43
# File 'lib/admin_it/filters/filter.rb', line 41

def filter_name
  @filter_name
end

.resourceObject (readonly)

Returns the value of attribute resource.



41
42
43
# File 'lib/admin_it/filters/filter.rb', line 41

def resource
  @resource
end

Class Method Details

.apply(str, filters, filter_classes) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/admin_it/filters/filter.rb', line 79

def self.apply(str, filters, filter_classes)
  list = str.scan(Filter::REGEXP)
  filters.clear if list.all? { |_, act, _, _| act.nil? || act.empty? }
  list.each do |full, action, name, params|
    name = name.to_sym
    if action == '-'
      filters.delete(name)
    elsif action == '!'
      filters[name].load(params) if filters.key?(name)
    else
      filters[name] = Filter.load(full, filter_classes)
    end
  end
end

.attr_checker(*names) ⇒ Object Originally defined in module ExtendIt::Base

.call_inherited(method_name, *args, base_first: false, &block) ⇒ Object Originally defined in module ExtendIt::Base

.class_attr_reader(*attrs) ⇒ Object Originally defined in module ExtendIt::Base

.create(name, _resource) ⇒ Object



64
65
66
# File 'lib/admin_it/filters/filter.rb', line 64

def self.create(name, _resource)
  create_class(name, _resource)
end

.display_nameObject Originally defined in module DisplayableName

.display_name=(value) ⇒ Object Originally defined in module DisplayableName

.dsl_eval(&block) ⇒ Object Originally defined in module ExtendIt::Dsl

.inherited_class_reader(*names) ⇒ Object Originally defined in module ExtendIt::Base

.load(str, filter_classes) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/admin_it/filters/filter.rb', line 68

def self.load(str, filter_classes)
  m = REGEXP.match(str)
  return nil if m.nil? || m[:action] == '-'
  name = m[:name].to_sym
  filter_class = filter_classes.find { |f| f.filter_name == name }
  return nil if filter_class.nil?
  filter = filter_class.new
  filter.load(m[:params])
  filter
end

.metaclass(&block) ⇒ Object Originally defined in module ExtendIt::Base

Instance Method Details

#apply(collection) ⇒ Object



127
128
129
# File 'lib/admin_it/filters/filter.rb', line 127

def apply(collection)
  collection
end

#dumpObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/admin_it/filters/filter.rb', line 104

def dump
  args = []
  opts = {}
  result = ''
  run_callbacks :save, arguments: { arguments: args, options: opts } do
    result = "#{name}"
    unless args.empty? && opts.empty?
      args.concat(opts.map { |k, v| "#{k}:#{v}" })
      result << "(#{args.join(',')})"
    end
  end
  result
end

#load(str) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/admin_it/filters/filter.rb', line 118

def load(str)
  return if str.nil?
  args = parse_arguments(str)
  unless args.empty?
    opts = args.extract_options!
    run_callbacks :load, arguments: { arguments: args, options: opts }
  end
end

#nameObject



100
101
102
# File 'lib/admin_it/filters/filter.rb', line 100

def name
  @name ||= self.class.filter_name
end

#run_callbacks(*names, arguments: [], original_context: false) ⇒ Object Originally defined in module ExtendIt::Callbacks