Class: Ukiryu::Models::Interface
- Inherits:
-
Object
- Object
- Ukiryu::Models::Interface
- 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.
Instance Attribute Summary collapse
-
#actions ⇒ Array<Hash>
Action contracts (signatures).
-
#aliases ⇒ Array<Symbol>
Alternative tool names.
-
#display_name ⇒ String
Human-readable name.
-
#name ⇒ Symbol
Interface identifier.
-
#version ⇒ String
Interface version (for contract evolution).
Class Method Summary collapse
-
.from_hash(data) ⇒ Interface
Create interface from hash.
-
.from_yaml(path) ⇒ Interface
Load interface from YAML file.
-
.symbolize_keys(hash) ⇒ Hash
Symbolize hash keys recursively.
Instance Method Summary collapse
-
#action(action_name) ⇒ Hash?
Get an action by name.
-
#action?(action_name) ⇒ Boolean
Check if interface has an action.
-
#initialize(name:, version:, actions:, display_name: nil, aliases: []) ⇒ Interface
constructor
A new instance of Interface.
-
#inspect ⇒ String
Inspect representation.
-
#to_s ⇒ String
String representation.
Constructor Details
#initialize(name:, version:, actions:, display_name: nil, aliases: []) ⇒ Interface
Returns a new instance of Interface.
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
#actions ⇒ Array<Hash>
Action contracts (signatures)
27 28 29 |
# File 'lib/ukiryu/models/interface.rb', line 27 def actions @actions end |
#aliases ⇒ Array<Symbol>
Alternative tool names
27 28 29 |
# File 'lib/ukiryu/models/interface.rb', line 27 def aliases @aliases end |
#display_name ⇒ String
Human-readable name
27 28 29 |
# File 'lib/ukiryu/models/interface.rb', line 27 def display_name @display_name end |
#name ⇒ Symbol
Interface identifier
27 28 29 |
# File 'lib/ukiryu/models/interface.rb', line 27 def name @name end |
#version ⇒ String
Interface version (for contract evolution)
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
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
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
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
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
56 57 58 |
# File 'lib/ukiryu/models/interface.rb', line 56 def action?(action_name) !action(action_name).nil? end |
#inspect ⇒ String
Inspect representation
114 115 116 |
# File 'lib/ukiryu/models/interface.rb', line 114 def inspect "#<Ukiryu::Models::Interface #{self} actions=#{@actions.length}>" end |
#to_s ⇒ String
String representation
107 108 109 |
# File 'lib/ukiryu/models/interface.rb', line 107 def to_s "#{@name}/#{@version}" end |