Class: ActiveScaffold::DataStructures::Filter

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_scaffold/data_structures/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type) ⇒ Filter

Returns a new instance of Filter.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
# File 'lib/active_scaffold/data_structures/filter.rb', line 9

def initialize(name, type)
  raise ArgumentError, 'Filter name must use only word characters (a-zA-Z0-9_)' unless name.match?(/\A\w+\z/)

  @label = @name = name.to_sym
  @type = type
  @options = []
  @weight = 0
end

Instance Attribute Details

#css_classObject

Returns the value of attribute css_class.



7
8
9
# File 'lib/active_scaffold/data_structures/filter.rb', line 7

def css_class
  @css_class
end

#default_optionObject

Returns the value of attribute default_option.



5
6
7
# File 'lib/active_scaffold/data_structures/filter.rb', line 5

def default_option
  @default_option
end

#descriptionObject



68
69
70
71
72
73
74
75
# File 'lib/active_scaffold/data_structures/filter.rb', line 68

def description
  case @description
  when Symbol
    ActiveScaffold::Registry.cache(:translations, @description) { as_(@description) }
  else
    @description
  end
end

#labelObject



59
60
61
62
63
64
65
66
# File 'lib/active_scaffold/data_structures/filter.rb', line 59

def label(*)
  case @label
  when Symbol
    ActiveScaffold::Registry.cache(:translations, @label) { as_(@label) }
  else
    @label
  end
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/active_scaffold/data_structures/filter.rb', line 5

def name
  @name
end

#security_methodObject

Returns the value of attribute security_method.



7
8
9
# File 'lib/active_scaffold/data_structures/filter.rb', line 7

def security_method
  @security_method
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/active_scaffold/data_structures/filter.rb', line 7

def type
  @type
end

#weightObject

Returns the value of attribute weight.



7
8
9
# File 'lib/active_scaffold/data_structures/filter.rb', line 7

def weight
  @weight
end

Instance Method Details

#[](option_name) ⇒ Object

finds a FilterOption by matching the name



42
43
44
# File 'lib/active_scaffold/data_structures/filter.rb', line 42

def [](option_name)
  @options.find { |option| option.name.to_s == option_name.to_s }
end

#add(name, options = {}) ⇒ Object Also known as: <<

adds a FilterOption, creating one from the arguments if need be

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_scaffold/data_structures/filter.rb', line 19

def add(name, options = {})
  if name.is_a?(ActiveScaffold::DataStructures::FilterOption)
    option = name
    name = option.name
  end
  existing = self[name]
  raise ArgumentError, "there is a filter option with '#{name}' name" if existing

  option ||= ActiveScaffold::DataStructures::FilterOption.new(@name, name, options)
  @default_option ||= option.name
  @options << option
  self
end

#delete(option_name) ⇒ Object



46
47
48
# File 'lib/active_scaffold/data_structures/filter.rb', line 46

def delete(option_name)
  @options.delete self[option_name]
end

#eachObject

iterates over the links, possibly by type



51
52
53
# File 'lib/active_scaffold/data_structures/filter.rb', line 51

def each(&)
  @options.each(&)
end

#empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/active_scaffold/data_structures/filter.rb', line 55

def empty?
  @options.empty?
end