Class: Mascot::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mascot/resource.rb

Overview

Represents the request path of an asset. There may be multiple resources that point to the same asset. Resources are immutable and may be altered by the resource proxy.

Constant Summary collapse

DEFAULT_FILTER_SCOPE =

Default scope for querying parent/child/sibling resources.

:same

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asset:, node:, ext: "") ⇒ Resource

Returns a new instance of Resource.



17
18
19
20
21
# File 'lib/mascot/resource.rb', line 17

def initialize(asset: , node: , ext: "")
  @asset = asset
  @node = node
  @ext = ext # TODO: Meh, feels dirty but I suppose the thingy has to drop it in.
end

Instance Attribute Details

#assetObject (readonly)

Returns the value of attribute asset.



12
13
14
# File 'lib/mascot/resource.rb', line 12

def asset
  @asset
end

#bodyObject



35
36
37
# File 'lib/mascot/resource.rb', line 35

def body
  @body ||= asset.body
end

#dataObject



31
32
33
# File 'lib/mascot/resource.rb', line 31

def data
  @data ||= asset.data
end

#extObject (readonly)

Returns the value of attribute ext.



12
13
14
# File 'lib/mascot/resource.rb', line 12

def ext
  @ext
end

#nodeObject (readonly)

Returns the value of attribute node.



12
13
14
# File 'lib/mascot/resource.rb', line 12

def node
  @node
end

Instance Method Details

#==(resource) ⇒ Object



55
56
57
# File 'lib/mascot/resource.rb', line 55

def ==(resource)
  resource.request_path == request_path
end

#children(**args) ⇒ Object



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

def children(**args)
  filter_resources(**args){ node.children }
end

#inspectObject



39
40
41
# File 'lib/mascot/resource.rb', line 39

def inspect
  "<#{self.class}:#{object_id} request_path=#{request_path.inspect} asset_path=#{@asset.path.to_s.inspect}>"
end

#parents(**args) ⇒ Object



43
44
45
# File 'lib/mascot/resource.rb', line 43

def parents(**args)
  filter_resources(**args){ node.parents }
end

#request_pathObject



23
24
25
26
27
28
29
# File 'lib/mascot/resource.rb', line 23

def request_path
  return unless node
  # TODO: This `compact` makes me nervous. How can we handle this better?
  lineage = node.parents.reverse.map(&:name).compact
  file_name = [node.name, @ext].join
  File.join("/", *lineage, file_name)
end

#siblings(**args) ⇒ Object



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

def siblings(**args)
  filter_resources(**args){ node.siblings }
end