Class: Ukiryu::Models::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/ukiryu/models/interface.rb

Overview

Interface model representing a pure contract for tool implementations.

An Interface defines WHAT actions a tool must provide, without specifying HOW those actions are implemented. It’s the “interface” in interface-centric design.

Examples:

Interface for gzip compression

interface = Interface.new(
  name: :gzip,
  version: "1.0",
  display_name: "Gzip Compression",
  actions: [
    { name: :compress, signature: { inputs: [...], output: {...} } },
    { name: :decompress, signature: { input: {...} } }
  ],
  aliases: [:gzip, :gunzip, :gzcat]
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, version:, actions:, display_name: nil, aliases: []) ⇒ Interface

Returns a new instance of Interface.

Parameters:

  • name (Symbol)

    Interface identifier

  • version (String)

    Interface version

  • display_name (String, nil) (defaults to: nil)

    Human-readable name

  • actions (Array<Hash>)

    Action contracts

  • aliases (Array<Symbol>) (defaults to: [])

    Alternative names



35
36
37
38
39
40
41
42
# File 'lib/ukiryu/models/interface.rb', line 35

def initialize(name:, version:, actions:, display_name: nil, aliases: [])
  @name = name
  @version = version
  @display_name = display_name || name.to_s.capitalize
  @actions = actions
  @aliases = aliases
  freeze
end

Instance Attribute Details

#actionsArray<Hash>

Action contracts (signatures)

Returns:

  • (Array<Hash>)

    the current value of actions



27
28
29
# File 'lib/ukiryu/models/interface.rb', line 27

def actions
  @actions
end

#aliasesArray<Symbol>

Alternative tool names

Returns:

  • (Array<Symbol>)

    the current value of aliases



27
28
29
# File 'lib/ukiryu/models/interface.rb', line 27

def aliases
  @aliases
end

#display_nameString

Human-readable name

Returns:

  • (String)

    the current value of display_name



27
28
29
# File 'lib/ukiryu/models/interface.rb', line 27

def display_name
  @display_name
end

#nameSymbol

Interface identifier

Returns:

  • (Symbol)

    the current value of name



27
28
29
# File 'lib/ukiryu/models/interface.rb', line 27

def name
  @name
end

#versionString

Interface version (for contract evolution)

Returns:

  • (String)

    the current value of version



27
28
29
# File 'lib/ukiryu/models/interface.rb', line 27

def version
  @version
end

Class Method Details

.from_hash(data) ⇒ Interface

Create interface from hash

Parameters:

  • data (Hash)

    Interface data

Returns:



94
95
96
97
98
99
100
101
102
# File 'lib/ukiryu/models/interface.rb', line 94

def self.from_hash(data)
  new(
    name: data[:name],
    version: data[:version],
    display_name: data[:display_name],
    actions: data[:actions],
    aliases: Array(data[:aliases] || []).map(&:to_sym)
  )
end

.from_yaml(path) ⇒ Interface

Load interface from YAML file

Parameters:

  • path (String)

    Path to interface YAML file

Returns:



64
65
66
67
68
69
# File 'lib/ukiryu/models/interface.rb', line 64

def self.from_yaml(path)
  require 'psych'
  data = Psych.safe_load_file(path,
                              permitted_classes: [Symbol, String, Integer, Array, Hash, TrueClass, FalseClass])
  from_hash(symbolize_keys(data))
end

.symbolize_keys(hash) ⇒ Hash

Symbolize hash keys recursively

Parameters:

  • hash (Hash)

    hash with string keys

Returns:

  • (Hash)

    hash with symbolized keys



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ukiryu/models/interface.rb', line 75

def self.symbolize_keys(hash)
  return hash unless hash.is_a?(Hash)

  hash.transform_keys(&:to_sym).transform_values do |v|
    case v
    when Hash
      symbolize_keys(v)
    when Array
      v.map { |item| item.is_a?(Hash) ? symbolize_keys(item) : item }
    else
      v
    end
  end
end

Instance Method Details

#action(action_name) ⇒ Hash?

Get an action by name

Parameters:

  • action_name (Symbol)

    Action name

Returns:

  • (Hash, nil)

    Action contract or nil



48
49
50
# File 'lib/ukiryu/models/interface.rb', line 48

def action(action_name)
  @actions.find { |a| a[:name] == action_name }
end

#action?(action_name) ⇒ Boolean

Check if interface has an action

Parameters:

  • action_name (Symbol)

    Action name

Returns:

  • (Boolean)

    true if action exists



56
57
58
# File 'lib/ukiryu/models/interface.rb', line 56

def action?(action_name)
  !action(action_name).nil?
end

#inspectString

Inspect representation

Returns:

  • (String)


114
115
116
# File 'lib/ukiryu/models/interface.rb', line 114

def inspect
  "#<Ukiryu::Models::Interface #{self} actions=#{@actions.length}>"
end

#to_sString

String representation

Returns:

  • (String)


107
108
109
# File 'lib/ukiryu/models/interface.rb', line 107

def to_s
  "#{@name}/#{@version}"
end