Class: I18n::Transformers::Collection
- Inherits:
-
Object
- Object
- I18n::Transformers::Collection
show all
- Defined in:
- lib/i18n/transformers/collection.rb,
lib/i18n/transformers/collection/base.rb,
lib/i18n/transformers/collection/generic.rb,
lib/i18n/transformers/collection/markdown.rb
Defined Under Namespace
Classes: Base, Generic, Markdown
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Collection.
4
5
6
|
# File 'lib/i18n/transformers/collection.rb', line 4
def initialize
@collection = []
end
|
Instance Method Details
#all ⇒ Object
8
9
10
|
# File 'lib/i18n/transformers/collection.rb', line 8
def all
@collection
end
|
#find(name_or_obj, index: false) ⇒ Object
12
13
14
15
16
17
|
# File 'lib/i18n/transformers/collection.rb', line 12
def find(name_or_obj, index: false)
name = name_or_obj.respond_to?(:name) ? name_or_obj.name : name_or_obj.to_s
method = index ? :index : :find
all.send(method) { |item| item.name == name }
end
|
#insert(transformer, at: nil, before: nil, after: nil) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/i18n/transformers/collection.rb', line 34
def insert(transformer, at: nil, before: nil, after: nil)
unless transformer.respond_to?(:transform)
raise ArgumentError, 'passed object need to response to transform method'
end
if [at, before, after].count(&:nil?) < 2
raise ArgumentError, 'before, after and at cannot be used at the same time'
end
index = at || find(before || after, index: true)
if index
raise ArgumentError, 'invalid index' if index < 0 || index > all.size
@collection.insert(after ? index + 1 : index, transformer)
else
@collection << transformer
end
transformer
end
|
#register(name = nil, options = {}, &block) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/i18n/transformers/collection.rb', line 19
def register(name = nil, options = {}, &block)
name, options = nil, name if name.is_a?(Hash)
position = options. :before, :after, :at
transformer = if name.respond_to?(:transform)
name
else
klass = name.is_a?(Symbol) ? collection_class_for(name) : name
klass = I18n::Transformers::Collection::Generic if !klass || name.is_a?(String)
klass.new(options.merge(name: name), &block)
end
insert(transformer, position)
end
|
#reset ⇒ Object
55
56
57
58
|
# File 'lib/i18n/transformers/collection.rb', line 55
def reset
@collection = []
self
end
|