Class: ViteRuby::Build

Inherits:
Struct
  • Object
show all
Defined in:
lib/vite_ruby/build.rb

Overview

Internal: Value object with information about the last build.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_digestObject

Returns the value of attribute current_digest

Returns:

  • (Object)

    the current value of current_digest



6
7
8
# File 'lib/vite_ruby/build.rb', line 6

def current_digest
  @current_digest
end

#digestObject

Returns the value of attribute digest

Returns:

  • (Object)

    the current value of digest



6
7
8
# File 'lib/vite_ruby/build.rb', line 6

def digest
  @digest
end

#successObject

Returns the value of attribute success

Returns:

  • (Object)

    the current value of success



6
7
8
# File 'lib/vite_ruby/build.rb', line 6

def success
  @success
end

#timestampObject

Returns the value of attribute timestamp

Returns:

  • (Object)

    the current value of timestamp



6
7
8
# File 'lib/vite_ruby/build.rb', line 6

def timestamp
  @timestamp
end

#vite_rubyObject

Returns the value of attribute vite_ruby

Returns:

  • (Object)

    the current value of vite_ruby



6
7
8
# File 'lib/vite_ruby/build.rb', line 6

def vite_ruby
  @vite_ruby
end

Class Method Details

.from_previous(attrs, current_digest) ⇒ Object

Internal: Combines information from a previous build with the current digest.



8
9
10
11
12
13
14
15
16
# File 'lib/vite_ruby/build.rb', line 8

def self.from_previous(attrs, current_digest)
  new(
    attrs['success'],
    attrs['timestamp'] || 'never',
    attrs['vite_ruby'] || 'unknown',
    attrs['digest'] || 'none',
    current_digest,
  )
end

Instance Method Details

#fresh?Boolean

Internal: A build is considered fresh if watched files have not changed, or the last failed build happened recently.

Returns:

  • (Boolean)


26
27
28
# File 'lib/vite_ruby/build.rb', line 26

def fresh?
  !stale?
end

#retry_failed?Boolean

Internal: To avoid cascading build failures, if the last build failed and it happened within a short time window, a new build should not be triggered.

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/vite_ruby/build.rb', line 32

def retry_failed?
  !success && Time.parse(timestamp) + 3 < Time.now # 3 seconds
rescue ArgumentError
  true
end

#stale?Boolean

Internal: A build is considered stale when watched files have changed since the last build, or when a certain time has ellapsed in case of failure.

Returns:

  • (Boolean)


20
21
22
# File 'lib/vite_ruby/build.rb', line 20

def stale?
  digest != current_digest || retry_failed? || vite_ruby != ViteRuby::VERSION
end

#to_json(*_args) ⇒ Object

Internal: Returns a JSON string with the metadata of the build.



49
50
51
# File 'lib/vite_ruby/build.rb', line 49

def to_json(*_args)
  JSON.pretty_generate(to_h)
end

#with_result(success) ⇒ Object

Internal: Returns a new build with the specified result.



39
40
41
42
43
44
45
46
# File 'lib/vite_ruby/build.rb', line 39

def with_result(success)
  self.class.new(
    success,
    Time.now.strftime('%F %T'),
    ViteRuby::VERSION,
    current_digest,
  )
end