File: setting.rb

package info (click to toggle)
mikutter 4.1.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,260 kB
  • sloc: ruby: 20,126; sh: 183; makefile: 19
file content (89 lines) | stat: -rw-r--r-- 1,932 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-

module Plugin::Extract
  def Setting(hash)
    if hash.is_a? Setting
      hash
    else
      Plugin::Extract::Setting.new(hash)
    end
  end

  class Setting < Diva::Model
    register :extract_setting, name: '抽出タブ'

    field.string :name, required: true
    field.int :id, required: true
    field.string :slug, required: true
    field.has :sources, [:string], required: true
    field.uri :sound
    field.bool :popup
    field.string :order
    field.uri :icon

    def initialize(hash)
      hash[:id] ||= Time.now.to_i << 16 | rand(0..0xffff)
      hash[:slug] ||= "extract_#{hash[:id]}"
      hash[:sources] ||= []
      super(hash)
    end

    def slug
      self[:slug].to_sym
    end

    def sources
      (self[:sources] || []).map(&:to_sym)
    end

    def order
      (self[:order] || :modified).to_sym
    end

    def find_ordering_obj
      Plugin.collect(:extract_order).find { |o| o.slug == order }
    end

    def sexp
      self[:sexp]
    end

    # 引数のsourceがsourcesに含まれていれば真を返す
    def using?(source_name)
      sources.include?(source_name.to_sym)
    end

    # 更新イベントを発生させる。
    def notify_update
      Plugin.call(:extract_tab_update, self)
    end

    # この抽出タブを消去する。
    # force: に真を渡すと、確認ダイアログを表示せずに削除する。
    def delete(force: false)
      if force
        Plugin.call(:extract_tab_delete, id)
      else
        Plugin.call(:extract_tab_delete_with_confirm, id)
      end
    end

    def export_to_userconfig
      { name: name,
        id: id,
        slug: slug,
        sources: sources,
        sound: sound ? sound.to_s : nil,
        popup: popup?,
        icon: icon ? icon.to_s : nil,
        order: order,
        uri: uri.to_s,
        sexp: self[:sexp]
      }
    end

    def path
      "/#{id}"
    end
  end
end