Class: MPW::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/mpw/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Item

Returns a new instance of Item.

Parameters:

  • options (Hash)

    the option :host is required



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mpw/item.rb', line 35

def initialize(**options)
  if !options.key?(:host) || options[:host].to_s.empty?
    raise I18n.t('error.update.host_empty')
  end

  if !options.key?(:id) || options[:id].to_s.empty? || !options.key?(:created) || options[:created].to_s.empty?
    @id = generate_id
    @created = Time.now.to_i
  else
    @id = options[:id]
    @created   = options[:created]
    @last_edit = options[:last_edit]
    options[:no_update_last_edit] = true
  end

  update(options)
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



30
31
32
# File 'lib/mpw/item.rb', line 30

def comment
  @comment
end

#createdObject

Returns the value of attribute created.



32
33
34
# File 'lib/mpw/item.rb', line 32

def created
  @created
end

#groupObject

Returns the value of attribute group.



24
25
26
# File 'lib/mpw/item.rb', line 24

def group
  @group
end

#hostObject

Returns the value of attribute host.



25
26
27
# File 'lib/mpw/item.rb', line 25

def host
  @host
end

#idObject

Returns the value of attribute id.



23
24
25
# File 'lib/mpw/item.rb', line 23

def id
  @id
end

#last_editObject

Returns the value of attribute last_edit.



31
32
33
# File 'lib/mpw/item.rb', line 31

def last_edit
  @last_edit
end

#otpObject

Returns the value of attribute otp.



29
30
31
# File 'lib/mpw/item.rb', line 29

def otp
  @otp
end

#portObject

Returns the value of attribute port.



28
29
30
# File 'lib/mpw/item.rb', line 28

def port
  @port
end

#protocolObject

Returns the value of attribute protocol.



26
27
28
# File 'lib/mpw/item.rb', line 26

def protocol
  @protocol
end

#userObject

Returns the value of attribute user.



27
28
29
# File 'lib/mpw/item.rb', line 27

def user
  @user
end

Instance Method Details

#deleteObject

Delete all data



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mpw/item.rb', line 71

def delete
  @id        = nil
  @group     = nil
  @host      = nil
  @protocol  = nil
  @user      = nil
  @port      = nil
  @otp       = nil
  @comment   = nil
  @created   = nil
  @last_edit = nil
end

#empty?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/mpw/item.rb', line 95

def empty?
  @id.to_s.empty?
end

#nil?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/mpw/item.rb', line 99

def nil?
  false
end

#update(**options) ⇒ Object

Update the item

Parameters:

  • options (Hash)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mpw/item.rb', line 55

def update(**options)
  if options.key?(:host) && options[:host].to_s.empty?
    raise I18n.t('error.update.host_empty')
  end

  @group     = options[:group]      if options.key?(:group)
  @host      = options[:host]       if options.key?(:host)
  @protocol  = options[:protocol]   if options.key?(:protocol)
  @user      = options[:user]       if options.key?(:user)
  @port      = options[:port].to_i  if options.key?(:port) && !options[:port].to_s.empty?
  @otp       = options[:otp]        if options.key?(:otp)
  @comment   = options[:comment]    if options.key?(:comment)
  @last_edit = Time.now.to_i        unless options.key?(:no_update_last_edit)
end

#urlString

Return data on url format

Returns:

  • (String)

    an url



86
87
88
89
90
91
92
93
# File 'lib/mpw/item.rb', line 86

def url
  url = ''
  url += "#{@protocol}://" if @protocol
  url += @host
  url += ":#{@port}" if @port

  url
end