Class: HtmlMeta::Data
- Inherits:
-
Object
- Object
- HtmlMeta::Data
- Defined in:
- lib/html_meta/data.rb
Instance Method Summary collapse
- #data(scope = :default) ⇒ Object
- #get(scope, key = nil) ⇒ Object
-
#initialize ⇒ Data
constructor
A new instance of Data.
- #meta(key, value = nil, options = {}) ⇒ Object
- #open_graph(key, value = nil, options = {}) ⇒ Object (also: #og)
- #reset ⇒ Object
- #set(scope, key, value = nil, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ Data
Returns a new instance of Data.
5 6 7 |
# File 'lib/html_meta/data.rb', line 5 def initialize @data = {} end |
Instance Method Details
#data(scope = :default) ⇒ Object
13 14 15 |
# File 'lib/html_meta/data.rb', line 13 def data(scope = :default) @data[scope] ||= {} end |
#get(scope, key = nil) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/html_meta/data.rb', line 17 def get(scope, key = nil) if key.nil? data(scope) else data(scope).fetch(key) { nil } end end |
#meta(key, value = nil, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/html_meta/data.rb', line 36 def (key, value = nil, = {}) if value.nil? return get(:default, key) else if key == :title && unless .key?(:separator) [:separator] = HtmlMeta.configuration.title_separator end end set(:default, key, value, ) end end |
#open_graph(key, value = nil, options = {}) ⇒ Object Also known as: og
49 50 51 52 53 54 55 56 |
# File 'lib/html_meta/data.rb', line 49 def open_graph(key, value = nil, = {}) if value.nil? return get(:og, key) else #we are setting a value set(:og, key, value, ) end end |
#reset ⇒ Object
9 10 11 |
# File 'lib/html_meta/data.rb', line 9 def reset @data = {} end |
#set(scope, key, value = nil, options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/html_meta/data.rb', line 25 def set(scope, key, value = nil, = {}) separator = .fetch(:separator, ' ') value = Array(value) if .fetch(:prepend, false) value << data(scope).fetch(key, '') elsif .fetch(:append, false) value.insert(0, data(scope).fetch(key, '')) end data(scope)[key] = value.compact.join(separator) end |