Class: Universa::Binder

Inherits:
RemoteAdapter show all
Defined in:
lib/universa/binder.rb

Overview

Adapter for Universa Binder class which behaves like a ruby hash.

Constant Summary collapse

LOCAL_METHODS =
Set.new(%i[to_hash to_ary [] []= keys values each each_key each_with_index size map to_s])

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RemoteAdapter

#__getobj__, #__setobj__, #initialize, #inspect, invoke_static, remote_class, remote_class_name, remote_field, static_method

Constructor Details

This class inherits a constructor from Universa::RemoteAdapter

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Internal use only. Call remote method as needed. This is where all the magick comes from: it call remote get/set method



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/universa/binder.rb', line 40

def method_missing(method_name, *args, &block)
  if respond_to_missing?(method_name, true)
    super
  else
    if method_name[-1] == '_'
      __getobj__.set(method_name[0..-1], args[0])
      args[0]
    else
      __getobj__.get(method_name)
    end
  end
end

Class Method Details

.of(*args) ⇒ Object

Create hew Binder from any hash. Keys will be converted to strings.



23
24
25
# File 'lib/universa/binder.rb', line 23

def self.of *args
  invoke_static "of", *args
end

Instance Method Details

#[](key) ⇒ Object

Get object by key.

Parameters:

  • key (Object)

    key.to_s will be used (so use Symbols or Strings freely)

Returns:

  • (Object)

    or nil



18
19
20
# File 'lib/universa/binder.rb', line 18

def [](key)
  __getobj__.get(key.to_s)
end

#[]=(key, value) ⇒ Object

Set object for a key

Parameters:

  • key (Object)

    key.to_s will be used (so use Symbols or Strings freely)

  • value (Object)


11
12
13
# File 'lib/universa/binder.rb', line 11

def []=(key, value)
  __getobj__.set(key.to_s, value)
end

#each {|key, value| ... } ⇒ Object

Enumerates all binder entries with a required block

Yields:

  • (key, value)

    pairs



66
67
68
# File 'lib/universa/binder.rb', line 66

def each &block
  keys.each {|k| block.call [k, __getobj__.get(k)]}
end

#keysObject

Retrieve binder keys



28
29
30
# File 'lib/universa/binder.rb', line 28

def keys
  __getobj__.keySet()
end

#map {|key, value| ... } ⇒ Object

Returns an array of values returned by the block.

Yields:

  • (key, value)

    pairs.

Returns:

  • an array of values returned by the block



72
73
74
# File 'lib/universa/binder.rb', line 72

def map(&block)
  keys.map {|k| block.call [k, __getobj__.get(k)]}
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

# Internal use only. Allow processing remote commands as local calls

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/universa/binder.rb', line 33

def respond_to_missing?(method_name, include_private = false)
  l = method_name[-1]
  LOCAL_METHODS.include?(method_name) || l == '!' || l == '?'
end

#to_aArray(Array(String,Object))

Returns array of [key,value] pairs.

Returns:

  • (Array(Array(String,Object)))

    array of [key,value] pairs.



60
61
62
# File 'lib/universa/binder.rb', line 60

def to_a
  map {|x| x}
end

#to_hObject

converts to a regular ruby hash



82
83
84
# File 'lib/universa/binder.rb', line 82

def to_h
  to_a.to_h
end

#to_sObject



55
56
57
# File 'lib/universa/binder.rb', line 55

def to_s
  to_h.to_s
end