Class: R18n::FilterList

Inherits:
Object
  • Object
show all
Defined in:
lib/r18n-core/filter_list.rb

Overview

Superclass for ‘GlobalFilterList` and `CustomFilterList` with filters processing.

Direct Known Subclasses

CustomFilterList, GlobalFilterList

Instance Method Summary collapse

Instance Method Details

#active(_type) ⇒ Object

List of enable active filters.



84
85
86
# File 'lib/r18n-core/filter_list.rb', line 84

def active(_type)
  []
end

#all(_type) ⇒ Object

List of enable filters.



89
90
91
# File 'lib/r18n-core/filter_list.rb', line 89

def all(_type)
  []
end

#enabled(filters_type, type) ⇒ Object

‘Array` of enabled filters with `filters_type` for `type`.



68
69
70
71
72
73
74
75
76
# File 'lib/r18n-core/filter_list.rb', line 68

def enabled(filters_type, type)
  if filters_type == :passive
    passive(type)
  elsif filters_type == :active
    active(type)
  else
    all(type)
  end
end

#passive(_type) ⇒ Object

List of enable passive filters.



79
80
81
# File 'lib/r18n-core/filter_list.rb', line 79

def passive(_type)
  []
end

#process(filters_type, type, value, locale, path, params) ⇒ Object

Process ‘value` by filters in `enabled`.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/r18n-core/filter_list.rb', line 25

def process(filters_type, type, value, locale, path, params)
  config = { locale: locale, path: path }

  enabled(filters_type, type).each do |filter|
    value = filter.call(value, config, *params)
  end

  if value.is_a? String
    value = TranslatedString.new(value, locale, path)
    process_string(filters_type, value, config, params)
  else
    value
  end
end

#process_string(filters_type, value, config, params) ⇒ Object

Process ‘value` by global filters in `enabled`.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/r18n-core/filter_list.rb', line 53

def process_string(filters_type, value, config, params)
  config = { locale: value.locale, path: config } if config.is_a? String

  enabled(filters_type, String).each do |f|
    value = f.call(value, config, *params)
  end

  if value.class == String
    TranslatedString.new(value, config[:locale], config[:path], self)
  else
    value
  end
end

#process_typed(filters_type, typed_value, params) ⇒ Object

Shortcut to process ‘R18n::Typed`.



41
42
43
44
45
46
47
48
49
50
# File 'lib/r18n-core/filter_list.rb', line 41

def process_typed(filters_type, typed_value, params)
  process(
    filters_type,
    typed_value.type,
    typed_value.value,
    typed_value.locale,
    typed_value.path,
    params
  )
end