Class: Aperitiiif::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/aperitiiif/record.rb

Overview

to do

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

#custom_metadataObject



42
43
44
45
46
47
48
49
# File 'lib/aperitiiif/record.rb', line 42

def 
  .map do |key|
    {
      'label' => key.to_s.delete_prefix(CUSTOM_METADATA_PREFIX),
      'value' => @hash[key]
    }
  end
end

#custom_metadata_keysObject



38
39
40
# File 'lib/aperitiiif/record.rb', line 38

def 
  @hash.to_h.keys.select { |key| key.to_s.start_with?(CUSTOM_METADATA_PREFIX) } || []
end

#descriptionObject



35
# File 'lib/aperitiiif/record.rb', line 35

def description = @hash.fetch 'description', ''

#idObject



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

#labelObject



33
# File 'lib/aperitiiif/record.rb', line 33

def label       = @hash.fetch 'label', id

#logoObject



34
# File 'lib/aperitiiif/record.rb', line 34

def         = @hash.fetch 'logo', ''

#respond_to_missing?(method, _args) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/aperitiiif/record.rb', line 23

def respond_to_missing?(method, _args)
  DELEGATE.include?(method) or super
end

#sourceObject



36
# File 'lib/aperitiiif/record.rb', line 36

def source      = @hash.fetch 'source', ''