Class: Mayu::Resources::Resource

Inherits:
Module
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mayu/resources/resource.rb

Defined Under Namespace

Classes: Wrapper

Constant Summary collapse

Impl =
T.type_alias { T.untyped }
MarshalFormat =
T.type_alias do
  [String, String, T.nilable(String), Resources::Types::Base]
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry:, path:) ⇒ Resource

Returns a new instance of Resource.



54
55
56
57
58
59
60
61
# File 'lib/mayu/resources/resource.rb', line 54

def initialize(registry:, path:)
  @registry = registry
  @path = path
  @path_hash = T.let(Digest::SHA256.hexdigest(path), String)
  @type = T.let(nil, T.untyped)
  @wrapper = T.let(Wrapper.new(nil), T.untyped)
  @content_hash = T.let(nil, T.nilable(String))
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



47
48
49
# File 'lib/mayu/resources/resource.rb', line 47

def path
  @path
end

#path_hashObject (readonly)

Returns the value of attribute path_hash.



49
50
51
# File 'lib/mayu/resources/resource.rb', line 49

def path_hash
  @path_hash
end

#registryObject (readonly)

Returns the value of attribute registry.



45
46
47
# File 'lib/mayu/resources/resource.rb', line 45

def registry
  @registry
end

#wrapperObject (readonly)

Returns the value of attribute wrapper.



51
52
53
# File 'lib/mayu/resources/resource.rb', line 51

def wrapper
  @wrapper
end

Instance Method Details

#absolute_pathObject



101
# File 'lib/mayu/resources/resource.rb', line 101

def absolute_path = @registry.absolute_path(@path)

#app_rootObject



98
# File 'lib/mayu/resources/resource.rb', line 98

def app_root = @registry.root

#assetsObject



84
85
86
# File 'lib/mayu/resources/resource.rb', line 84

def assets
  self.type&.assets || []
end

#calculate_content_hashObject



79
80
81
# File 'lib/mayu/resources/resource.rb', line 79

def calculate_content_hash
  Digest::SHA256.file(absolute_path).digest
end

#content_hashObject



74
75
76
# File 'lib/mayu/resources/resource.rb', line 74

def content_hash
  @content_hash ||= calculate_content_hash
end

#exists?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/mayu/resources/resource.rb', line 69

def exists?
  File.exist?(absolute_path)
end

#generate_assets(assets_dir) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/mayu/resources/resource.rb', line 89

def generate_assets(assets_dir)
  if type = self.type
    type.generate_assets(assets_dir)
  else
    []
  end
end

#import(path) ⇒ Object



122
123
124
125
126
# File 'lib/mayu/resources/resource.rb', line 122

def import(path)
  resource = self.registry.load_resource(path, File.dirname(self.path))
  self.registry.dependency_graph.add_dependency(self.path, resource.path)
  resource.type
end

#load_typeObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mayu/resources/resource.rb', line 109

def load_type
  @content_hash = nil
  @wrapper.__replace_impl!(
    @type =
      if exists?
        Types.for_path(self.path).new(self)
      else
        Types::Nil.new(self)
      end
  )
end

#marshal_dumpObject



134
135
136
137
138
139
140
# File 'lib/mayu/resources/resource.rb', line 134

def marshal_dump
  if exists?
    [path, path_hash, content_hash, type]
  else
    [path, path_hash, nil, type]
  end
end

#marshal_load(args) ⇒ Object



143
144
145
146
147
# File 'lib/mayu/resources/resource.rb', line 143

def marshal_load(args)
  @path, @path_hash, @content_hash, @type = args
  @type.instance_variable_set(:@resource, self)
  @wrapper = Wrapper.new(@type)
end

#read(encoding: "binary") ⇒ Object



64
65
66
# File 'lib/mayu/resources/resource.rb', line 64

def read(encoding: "binary")
  File.read(absolute_path)
end

#typeObject



104
105
106
# File 'lib/mayu/resources/resource.rb', line 104

def type
  @type || load_type
end