Module: KodiClient::Extensions::Creatable

Included in:
Types::Addons::AddonDependency, Types::Addons::AddonDetails, Types::Addons::AddonExtraInfo, Types::Addons::GetAddonReturned, Types::Addons::GetAddonsReturned, Types::Application::PropertyValue, Types::Application::Version, Types::Audio::AudioArtistRole, Types::Audio::AudioContributor, Types::Audio::AudioDetailsAlbum, Types::Audio::AudioDetailsArtist, Types::Audio::AudioDetailsRole, Types::Audio::AudioDetailsSong, Types::Audio::AvailableArt, Types::Audio::Genre, Types::Audio::GetAlbumsReturned, Types::Audio::GetArtistsReturned, Types::Audio::GetGenresReturned, Types::Audio::GetRolesReturned, Types::Audio::GetSongsReturned, Types::Audio::GetSourcesReturned, Types::Audio::PropertyValue, Types::Favourites::DetailsFavourite, Types::Favourites::GetFavouriteReturned, Types::Files::FileLabel, Types::Files::GetDirectoryReturned, Types::Files::GetSourcesReturned, Types::Files::PrepareDownloadReturned, Types::GUI::PropertyValue, Types::GUI::StereoscopyMode, Types::Global::GlobalTime, Types::Global::IdLabel, Types::Global::IdName, Types::Library::AudioDetailsSource, Types::Library::LibraryDetailsGenre, Types::List::ListItemAll, Types::List::ListItemFile, Types::List::ListLimitsReturned, Types::Media::MediaArtwork, Types::Player::AudioStream, Types::Player::Player, Types::Player::PlayerPositionTime, Types::Player::PlayerViewMode, Types::Player::PropertyValue, Types::Player::SeekReturned, Types::Player::Subtitle, Types::Player::VideoStream, Types::Profiles::DetailsProfile, Types::Profiles::GetProfilesReturned, Types::Profiles::ProfilePassword, Types::System::PropertyValue, Types::Video::StreamDetails, Types::Video::VideoCast, Types::Video::VideoResume
Defined in:
lib/kodi_client/extensions/creatable.rb

Overview

creates a create and create_list method by hash

Defined Under Namespace

Classes: CreateMap

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.arr_to_mapping(arr) ⇒ Object



26
27
28
# File 'lib/kodi_client/extensions/creatable.rb', line 26

def self.arr_to_mapping(arr)
  Creatable::CreateMap.new(arr[1], arr[2].nil? ? false : arr[2])
end

.extract_field_from_hash(field, hash, mapping) ⇒ Object

extracts the given field from the given hash and optionally applies mapping if given

Parameters:

  • field (String)

    the field name

  • hash (Hash, nil)

    the hash where to extract the field from

  • mapping (Hash<String, CreateMap>)

    optional mapping for complex types

Returns:

  • the extracted object or nil, if the hash is empty, or [] if the expected value was a list and was nil



84
85
86
87
88
89
90
91
92
93
# File 'lib/kodi_client/extensions/creatable.rb', line 84

def self.extract_field_from_hash(field, hash, mapping)
  map = mapping[field]
  return nil if hash[field].nil? && map.nil?

  return hash[field] if map.nil?

  return map.type.create_list(hash[field]) if map.is_list

  map.type.create(hash[field])
end

.hash_to_arr(hash, fields, mapping = {}) ⇒ Array

takes a hash and a list of strings and an optional mapping and creates a new array with the created objects of the hash and fields. If no mapping is provided, the value of this field is returned, otherwise the mapping is used

Parameters:

  • hash (Hash)

    the given hash

  • fields (Array<String>)

    the fields to extract

  • mapping (Hash<String>, Array<Array<String>>) (defaults to: {})

    optional mapping if a field contains a complex type

Returns:

  • (Array)

    an array containing the values of hash in the given field order



68
69
70
71
72
73
74
75
76
77
# File 'lib/kodi_client/extensions/creatable.rb', line 68

def self.hash_to_arr(hash, fields, mapping = {})
  return nil if hash.nil?

  mapping = mapping.map { |it| arr_to_mapping(it) } unless mapping.is_a?(Hash)

  fields.map do |it|
    field = it.to_s.gsub('_', '')
    extract_field_from_hash(field, hash, mapping)
  end
end

Instance Method Details

#attr_accessor(*args) ⇒ Object



95
96
97
98
# File 'lib/kodi_client/extensions/creatable.rb', line 95

def attr_accessor(*args)
  attr_reader(*args)
  attr_writer(*args)
end

#attr_reader(*args) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/kodi_client/extensions/creatable.rb', line 100

def attr_reader(*args)
  @kodi_fields = args.map { |it| it.to_s.gsub('_', '') }

  args.each do |it|
    inst_variable_name = "@#{it}".to_sym
    define_method it do
      instance_variable_get(inst_variable_name)
    end
  end
end

#attr_writer(*args) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/kodi_client/extensions/creatable.rb', line 111

def attr_writer(*args)
  args.each do |it|
    inst_variable_name = "@#{it}".to_sym
    define_method "#{it}=" do |new_value|
      instance_variable_set(inst_variable_name, new_value)
    end
  end
end

#create(hash) ⇒ Object

creates a Creatable using the provided hash and the attr_reader fields, or if given #fields_to_map instead The parameters will be passed to initialize the object in the same order as they are passed to attr_reader or #fields_to_map

Parameters:

  • hash (Hash, nil)

    the given hash to convert to an object

Returns:

  • the created object, or nil if the given hash is nil



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kodi_client/extensions/creatable.rb', line 42

def create(hash)
  return nil if hash.nil?

  if @fields_to_map.nil? || @fields_to_map.empty?
    fields = @kodi_fields
    return nil if @kodi_fields.none? { |it| !hash[it.to_s.gsub('_', '')].nil? }
  else
    fields = @fields_to_map
  end

  mapping = @type_mapping.nil? ? {} : @type_mapping
  args = fields.map do |it|
    field = it.to_s.gsub('_', '')
    Creatable.extract_field_from_hash(field, hash, mapping)
  end

  new(*args)
end

#create_list(hash) ⇒ Array

expects the given hash is a list of hashes and calls #create on each element

Parameters:

  • hash (Array<Hash>)

    given list of hashes

Returns:

  • (Array)

    empty if hash is nil, otherwise array containing the elements



33
34
35
# File 'lib/kodi_client/extensions/creatable.rb', line 33

def create_list(hash)
  hash.nil? ? [] : hash.map { |it| create(it) }.reject(&:nil?)
end

#fields_to_map(fields) ⇒ Object

sets the fields to fetch from the given hash for the #create method. This is optional and only to use, if more fields should be fetched than are provided using attr_reader

Parameters:

  • fields (Array<String>)

    the fields given as a string array



11
12
13
# File 'lib/kodi_client/extensions/creatable.rb', line 11

def fields_to_map(fields)
  @fields_to_map = fields
end

#type_mapping(*args) ⇒ Object

defines mappings in case an attribute in the hash is not primitive and requires further processing. The argument is an Array of Arrays. Each array contains [‘field_name’, [type_to_map_to], [opt if field is a list]] Media::ArtWork.create_list will be called

Parameters:

  • args (Array<Array>)

    mappings. Example mapping [‘art’, Media::ArtWork, true], in this case



19
20
21
22
23
24
# File 'lib/kodi_client/extensions/creatable.rb', line 19

def type_mapping(*args)
  return if args.nil?

  @type_mapping = {}
  args.map { |it| @type_mapping[it[0]] = Creatable.arr_to_mapping(it) }
end