Class: Aperitiiif::Record
- Inherits:
-
Object
- Object
- Aperitiiif::Record
show all
- Defined in:
- lib/aperitiiif/record.rb
Overview
Constant Summary
collapse
- DELEGATE =
%i[puts p].freeze
- CUSTOM_METADATA_PREFIX =
'meta.'
Instance Method Summary
collapse
Constructor Details
#initialize(hash, defaults = {}) ⇒ Record
Returns a new instance of Record.
12
13
14
15
|
# File 'lib/aperitiiif/record.rb', line 12
def initialize(hash, defaults = {})
@hash = defaults.merge(hash)
@id = id
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
17
18
19
20
21
|
# File 'lib/aperitiiif/record.rb', line 17
def method_missing(method, *args, &block)
return super if DELEGATE.include? method
@hash.fetch method.to_s
end
|
Instance Method Details
42
43
44
45
46
47
48
49
|
# File 'lib/aperitiiif/record.rb', line 42
def custom_metadata
custom_metadata_keys.map do |key|
{
'label' => key.to_s.delete_prefix(CUSTOM_METADATA_PREFIX),
'value' => @hash[key]
}
end
end
|
38
39
40
|
# File 'lib/aperitiiif/record.rb', line 38
def custom_metadata_keys
@hash.to_h.keys.select { |key| key.to_s.start_with?(CUSTOM_METADATA_PREFIX) } || []
end
|
#description ⇒ Object
35
|
# File 'lib/aperitiiif/record.rb', line 35
def description = @hash.fetch 'description', ''
|
#id ⇒ Object
27
28
29
30
31
|
# File 'lib/aperitiiif/record.rb', line 27
def id
@hash.fetch 'id'
rescue KeyError
raise Aperitiiif::Error, "Record has no 'id'\n#{@hash.inspect}"
end
|
#label ⇒ Object
33
|
# File 'lib/aperitiiif/record.rb', line 33
def label = @hash.fetch 'label', id
|
#logo ⇒ Object
34
|
# File 'lib/aperitiiif/record.rb', line 34
def logo = @hash.fetch 'logo', ''
|
#respond_to_missing?(method, _args) ⇒ Boolean
23
24
25
|
# File 'lib/aperitiiif/record.rb', line 23
def respond_to_missing?(method, _args)
DELEGATE.include?(method) or super
end
|
#source ⇒ Object
36
|
# File 'lib/aperitiiif/record.rb', line 36
def source = @hash.fetch 'source', ''
|