Class: Kanrisuru::Remote::Fstab::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/kanrisuru/remote/fstab/options.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, opts) ⇒ Options

Returns a new instance of Options.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kanrisuru/remote/fstab/options.rb', line 7

def initialize(type, opts)
  @type = type
  @valid = false

  if opts.instance_of?(String)
    @opts = parse_opts(opts)
  elsif opts.instance_of?(Hash)
    @opts = opts.transform_keys(&:to_s)
  else
    raise ArgumentError, 'Invalid option type'
  end

  validate_opts!
end

Class Method Details

.option_exists?(value, type = nil) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
# File 'lib/kanrisuru/remote/fstab/options.rb', line 72

def self.option_exists?(value, type = nil)
  value = value.to_sym
  type = type ? type.to_sym : nil

  common = Kanrisuru::Util::FsMountOpts[:common]
  fs_opts = Kanrisuru::Util::FsMountOpts[type]

  common.key?(value) ||
    fs_opts&.key?(value)
end

.valid_option?(value, field, type = nil) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/kanrisuru/remote/fstab/options.rb', line 83

def self.valid_option?(value, field, type = nil)
  value = value.to_sym
  type = type ? type.to_sym : nil

  common = Kanrisuru::Util::FsMountOpts[:common]
  fs_opts = Kanrisuru::Util::FsMountOpts[type]

  if common.key?(value)
    case common[value]
    when 'boolean'
      [true, false].include?(field)
    when 'value'
      field.instance_of?(String) || field.instance_of?(Float) || field.instance_of?(Integer)
    else
      false
    end
  elsif fs_opts&.key?(value)
    case fs_opts[value]
    when 'boolean'
      [true, false].include?(field)
    when 'value'
      field.instance_of?(String) || field.instance_of?(Float) || field.instance_of?(Integer)
    else
      false
    end
  else
    raise ArgumentError, 'Invalid option'
  end
end

Instance Method Details

#[](option) ⇒ Object



27
28
29
# File 'lib/kanrisuru/remote/fstab/options.rb', line 27

def [](option)
  @opts[option]
end

#[]=(option, value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kanrisuru/remote/fstab/options.rb', line 31

def []=(option, value)
  option = option.to_s

  unless Kanrisuru::Remote::Fstab::Options.option_exists?(option, @type)
    raise ArgumentError,
          "Invalid option: #{option} for #{@type} file system."
  end

  unless Kanrisuru::Remote::Fstab::Options.valid_option?(option, value, @type)
    raise ArgumentError,
          "Invalid option value: #{value} for #{option} on #{@type} file system."
  end

  @opts[option] = value
end

#inspectObject



22
23
24
25
# File 'lib/kanrisuru/remote/fstab/options.rb', line 22

def inspect
  format('<Kanrisuru::Remote::Fstab::Options:0x%<object_id>s @opts=%<opts>s @type=%<type>s>',
         object_id: object_id, opts: @opts, type: @type)
end

#to_hObject



68
69
70
# File 'lib/kanrisuru/remote/fstab/options.rb', line 68

def to_h
  @opts
end

#to_sObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kanrisuru/remote/fstab/options.rb', line 47

def to_s
  string = ''
  opts_length = @opts.length

  @opts.each_with_index do |(key, value), index|
    append_comma = true

    if value == true
      string += key.to_s
    elsif value.instance_of?(String) || value.instance_of?(Integer) || value.instance_of?(Float)
      string += "#{key}=#{value}"
    else
      append_comma = false
    end

    string += ',' if append_comma && index < opts_length - 1
  end

  string
end