Class: JekyllPageBoilerplate::Tags
- Inherits:
-
Object
- Object
- JekyllPageBoilerplate::Tags
show all
- Defined in:
- lib/jekyll_page_boilerplate/tags.rb
Constant Summary
collapse
- FILE_DATE_FORMATE =
'%Y-%m-%d'
- FILL_SCAN =
/\{{2}\s{0,}([^\{\}\.\s]+)\s{0,}\}{2}/
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args, **params) ⇒ Tags
Returns a new instance of Tags.
11
12
13
14
|
# File 'lib/jekyll_page_boilerplate/tags.rb', line 11
def initialize *args, **params
@tags = {}
add(*args, params)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key) ⇒ Object
67
68
69
|
# File 'lib/jekyll_page_boilerplate/tags.rb', line 67
def method_missing key
@tags[key.to_s]
end
|
Class Method Details
.[](*args) ⇒ Object
7
8
9
|
# File 'lib/jekyll_page_boilerplate/tags.rb', line 7
def self.[] *args
self.new *args
end
|
Instance Method Details
#[](key) ⇒ Object
27
28
29
30
|
# File 'lib/jekyll_page_boilerplate/tags.rb', line 27
def [] key
key = key.to_s
@tags[key] ||= fetch(*key.split(/\s*=\s*|\s*,\s*/))
end
|
#[]=(key, val) ⇒ Object
16
17
18
|
# File 'lib/jekyll_page_boilerplate/tags.rb', line 16
def []= key, val
@tags[key.to_s] = val
end
|
#add(*args, **params) ⇒ Object
20
21
22
23
24
25
|
# File 'lib/jekyll_page_boilerplate/tags.rb', line 20
def add *args, **params
args.map! {|h| h.transform_keys(&:to_s).compact }
params.transform_keys!(&:to_s).compact!
@tags.merge!(*args, params)
self
end
|
#fetch(key, *params) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/jekyll_page_boilerplate/tags.rb', line 51
def fetch key, *params
case key
when 'safe'
safe(params.join(','))
when 'time'
Time.now.to_s
when 'date'
Time.now.strftime(FILE_DATE_FORMATE)
when 'random'
SecureRandom.hex(*params.map(&:to_i))
else
''
end
end
|
#fill(*keys, safe: false) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/jekyll_page_boilerplate/tags.rb', line 32
def fill *keys, safe: false
keys.map!(&:to_s)
keys.each do |k|
@tags[k].scan(FILL_SCAN).flatten.uniq.each do |tag|
@tags[k].gsub! /\{{2}\s{0,}#{tag.to_s}\s{0,}\}{2}/, self[tag].to_s
end
end
if safe
keys.each do |k|
@tags[k] = safe(k)
end
end
self
end
|
#safe(key) ⇒ Object
47
48
49
|
# File 'lib/jekyll_page_boilerplate/tags.rb', line 47
def safe key
self[key].to_s.downcase.gsub(/[^0-9a-z\.\-\/]+/, '-')
end
|