Class: Klenod::Build::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/klenod/build/asset.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logical_name, content_hash, output_path, source_path, bytes, content_type, metadata, generator: nil, writer: nil, queue: nil, queue_kind: :cpu) ⇒ Asset

Returns a new instance of Asset.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/klenod/build/asset.rb', line 12

def initialize(logical_name, content_hash, output_path, source_path, bytes, content_type, , generator: nil, writer: nil, queue: nil, queue_kind: :cpu)
  @logical_name = logical_name
  @content_hash = content_hash
  @output_path = output_path
  @source_path = source_path
  @bytes = bytes
  @content_type = content_type
  @metadata = 
  @generator = generator
  @writer = writer
  @queue = queue
  @queue_kind = queue_kind
  @disk_path = nil
  @task = nil
  @mutex = Mutex.new
  @state = (bytes || (source_path && !generator && !writer)) ? :ready : :pending
  @error = nil
end

Instance Attribute Details

#content_hashObject (readonly)

Returns the value of attribute content_hash.



10
11
12
# File 'lib/klenod/build/asset.rb', line 10

def content_hash
  @content_hash
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



10
11
12
# File 'lib/klenod/build/asset.rb', line 10

def content_type
  @content_type
end

#errorObject (readonly)

Returns the value of attribute error.



10
11
12
# File 'lib/klenod/build/asset.rb', line 10

def error
  @error
end

#logical_nameObject (readonly)

Returns the value of attribute logical_name.



10
11
12
# File 'lib/klenod/build/asset.rb', line 10

def logical_name
  @logical_name
end

#metadataObject (readonly)

Returns the value of attribute metadata.



10
11
12
# File 'lib/klenod/build/asset.rb', line 10

def 
  @metadata
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



10
11
12
# File 'lib/klenod/build/asset.rb', line 10

def output_path
  @output_path
end

#queue_kindObject (readonly)

Returns the value of attribute queue_kind.



10
11
12
# File 'lib/klenod/build/asset.rb', line 10

def queue_kind
  @queue_kind
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



10
11
12
# File 'lib/klenod/build/asset.rb', line 10

def source_path
  @source_path
end

Class Method Details

.generated(logical_name, content_hash, output_path, source_path, content_type, metadata, writer: nil, queue: nil, queue_kind: :cpu, &generator) ⇒ Object



31
32
33
# File 'lib/klenod/build/asset.rb', line 31

def self.generated(logical_name, content_hash, output_path, source_path, content_type, , writer: nil, queue: nil, queue_kind: :cpu, &generator)
  new(logical_name, content_hash, output_path, source_path, nil, content_type, , generator: generator, writer: writer, queue: queue, queue_kind: queue_kind)
end

Instance Method Details

#bytesObject



35
36
37
38
# File 'lib/klenod/build/asset.rb', line 35

def bytes
  wait
  @bytes || File.binread(@disk_path || @source_path)
end

#failed?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/klenod/build/asset.rb', line 63

def failed?
  @state == :failed
end

#generated?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/klenod/build/asset.rb', line 71

def generated?
  !static?
end

#pending?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/klenod/build/asset.rb', line 55

def pending?
  @state == :pending
end

#ready?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/klenod/build/asset.rb', line 51

def ready?
  @state == :ready
end

#running?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/klenod/build/asset.rb', line 59

def running?
  @state == :running
end

#static?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/klenod/build/asset.rb', line 67

def static?
  @generator.nil? && @writer.nil?
end

#waitObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/klenod/build/asset.rb', line 40

def wait
  return self if ready?
  raise error if failed?

  task = start_generation
  task ? wait_for_task(task) : generate_now_or_queued
  raise error if failed?

  self
end

#write_to(path, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/klenod/build/asset.rb', line 75

def write_to(path, &block)
  path = path.to_s

  return skip_existing(path) if File.file?(path)

  task = start_write(path, &block)
  task ? wait_for_task(task) : write_now_or_queued(path, &block)
  raise error if failed?

  :written
end