Class: StreamdeckProf::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/streamdeck_prof/context.rb

Instance Method Summary collapse

Constructor Details

#initialize(profiles_dir: default_profiles_dir) ⇒ Context

Returns a new instance of Context.



9
10
11
# File 'lib/streamdeck_prof/context.rb', line 9

def initialize(profiles_dir: default_profiles_dir)
  @profiles_dir = profiles_dir
end

Instance Method Details

#default_profileObject



27
28
29
# File 'lib/streamdeck_prof/context.rb', line 27

def default_profile
  profile_for_application("*")
end

#new_profile(type: :xl) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/streamdeck_prof/context.rb', line 35

def new_profile(type: :xl)
  manifest = StreamdeckProf::DeviceData.const_get(type.upcase)[:empty_profile]
  uuid = SecureRandom.uuid.upcase
  profile_path = File.join(@profiles_dir, "#{uuid}.sdProfile")
  manifest_path = File.join(profile_path, "manifest.json")

  Dir.mkdir(profile_path)
  File.write(manifest_path, JSON.dump(manifest))

  @profiles = nil

  Profile.new(profile_path)
end

#profile_by_name(name) ⇒ Object



31
32
33
# File 'lib/streamdeck_prof/context.rb', line 31

def profile_by_name(name)
  profiles.find { |p| p.name == name }
end

#profile_for_application(application, exact: true) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/streamdeck_prof/context.rb', line 19

def profile_for_application(application, exact: true)
  if exact
    profiles.find { |p| p.app_identifier == application }
  else
    profiles.find { |p| p.app_identifier&.include?(application) }
  end
end

#profilesObject



13
14
15
16
17
# File 'lib/streamdeck_prof/context.rb', line 13

def profiles
  @profiles ||= Dir.glob(File.join(@profiles_dir, "*.sdProfile"))
                   .map { |profile_path| Profile.new(profile_path) }
                   .freeze
end