Class: Pindo::TaskSystem::TaskResources::ResourceInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/module/task/task_resources/resource_instance.rb

Overview

资源实例 - 表示一个具体的资源占用

职责:

  • 包含资源类型和上下文信息

  • 提供资源冲突检查接口

示例:

xcode_resource = ResourceInstance.new(xcode_type, { directory: "/path/to/project" })
network_resource = ResourceInstance.new(network_type, {})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, context = {}) ⇒ ResourceInstance

Returns a new instance of ResourceInstance.

Parameters:

  • type (ResourceType)

    资源类型对象

  • context (Hash) (defaults to: {})

    上下文信息(如 { directory: “/path/to/project” })

Raises:

  • (ArgumentError)


20
21
22
23
24
# File 'lib/pindo/module/task/task_resources/resource_instance.rb', line 20

def initialize(type, context = {})
  raise ArgumentError, "type must be a ResourceType" unless type.is_a?(ResourceType)
  @type = type
  @context = context || {}
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



16
17
18
# File 'lib/pindo/module/task/task_resources/resource_instance.rb', line 16

def context
  @context
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/pindo/module/task/task_resources/resource_instance.rb', line 16

def type
  @type
end

Instance Method Details

#conflicts_with?(other) ⇒ Boolean

检查是否与另一个资源实例冲突

Parameters:

Returns:

  • (Boolean)

    是否冲突



29
30
31
32
33
34
35
# File 'lib/pindo/module/task/task_resources/resource_instance.rb', line 29

def conflicts_with?(other)
  return false unless other.is_a?(ResourceInstance)
  return false unless @type.name == other.type.name  # 不同类型不冲突

  # 委托给资源类型检查冲突逻辑
  @type.conflicts?(self, other)
end

#directoryString?

获取目录路径(如果有)

Returns:

  • (String, nil)


78
79
80
# File 'lib/pindo/module/task/task_resources/resource_instance.rb', line 78

def directory
  @context[:directory]
end

#has_directory?Boolean

检查是否有目录上下文

Returns:

  • (Boolean)


84
85
86
# File 'lib/pindo/module/task/task_resources/resource_instance.rb', line 84

def has_directory?
  !@context[:directory].nil?
end

#inspectString

资源实例的详细描述

Returns:

  • (String)


72
73
74
# File 'lib/pindo/module/task/task_resources/resource_instance.rb', line 72

def inspect
  "#<ResourceInstance type=#{@type.name} strategy=#{@type.strategy} context=#{@context.inspect}>"
end

#matches?(other) ⇒ Boolean

检查是否匹配另一个资源实例(用于释放资源时查找)

Parameters:

Returns:

  • (Boolean)

    是否匹配



40
41
42
43
44
45
46
# File 'lib/pindo/module/task/task_resources/resource_instance.rb', line 40

def matches?(other)
  return false unless other.is_a?(ResourceInstance)
  return false unless @type.name == other.type.name

  # 上下文必须完全匹配
  @context == other.context
end

#matches_spec?(spec) ⇒ Boolean

检查是否匹配资源规格(Hash 格式)

Parameters:

  • spec (Hash)

    资源规格,如 { type: :xcode, directory: “/path” }

Returns:

  • (Boolean)

    是否匹配



51
52
53
54
55
56
57
58
# File 'lib/pindo/module/task/task_resources/resource_instance.rb', line 51

def matches_spec?(spec)
  return false unless spec.is_a?(Hash)
  return false unless spec[:type] == @type.name

  # 提取上下文(除了 :type 之外的所有键)
  spec_context = spec.reject { |k, _| k == :type }
  @context == spec_context
end

#to_sString

资源实例的字符串表示

Returns:

  • (String)


62
63
64
65
66
67
68
# File 'lib/pindo/module/task/task_resources/resource_instance.rb', line 62

def to_s
  if @context.empty?
    "#{@type.name}"
  else
    "#{@type.name}(#{@context.inspect})"
  end
end