Class: Poly

Inherits:
Object show all
Defined in:
lib/poly.rb

Instance Method Summary collapse

Constructor Details

#initialize(table = {}) ⇒ Poly

attr_reader :table



10
11
12
13
14
# File 'lib/poly.rb', line 10

def initialize(table = {})
  @table = table.tap { |ret|
    pp [__LINE__, 'Poly.initialize()', 'table', table.to_yaml] if $pd
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/poly.rb', line 42

def method_missing(name, *args)
  pt = nil
  if name.to_s.end_with?('=')
    # Setter method
    attribute = name.to_s.chomp('=').to_sym
    value = args.first
    if respond_to?("set_#{attribute}")
      pt = 'send set_'
      send("set_#{attribute}", value)
    else
      pt = 'table set'
      @table[attribute] = value
    end
  elsif respond_to?("get_#{name}")
    pt = 'send get_'
    # Getter method
    send("get_#{name}")
  elsif @table.respond_to?(name)
    pt = 'send name'
    @table.send(name, *args)
  else
    pt = 'table read'
    @table[name.to_sym]
  end.tap { |ret|
    pp([__LINE__,
        "Poly.method_missing #{name} #{args.map(&:to_s).join(' ')}", pt, '->', ret]) if $pd
  }
end

Instance Method Details

#[](key) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/poly.rb', line 78

def [](key)
  if respond_to?("get_#{key}")
    send("get_#{key}")
  else
    @table[key.to_sym]
  end.tap { |ret| pp([__LINE__, "Poly.[] #{key}", '->', ret]) if $pd }
end

#[]=(key, value) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/poly.rb', line 86

def []=(key, value)
  if respond_to?("set_#{key}")
    send("set_#{key}", value)
  else
    @table[key.to_sym] = value
  end.tap { |ret|
    pp([__LINE__, "Poly.[]= #{key} #{value}", '->',
        ret]) if $pd
  }
end

#fetch(key, *args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/poly.rb', line 16

def fetch(key, *args)
  key_sym = key.to_sym
  if respond_to?("get_#{key}")
    send("get_#{key}")
  elsif @table.key?(key_sym)
    @table[key_sym]
  elsif block_given?
    yield key_sym
  elsif args.count.positive?
    # binding.irb
    args.first
  else
    binding.irb
    raise KeyError, "key not found: #{key}"
  end.tap { |ret|
    pp([__LINE__, "Poly.fetch #{key} #{args}", '->',
        ret]) if $pd
  }
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/poly.rb', line 36

def key?(name)
  @table.key?(name.to_sym).tap { |ret|
    pp([__LINE__, "Poly.key? #{name}", '->', ret]) if $pd
  }
end

#merge(*args) ⇒ Object

for export to Prompt library def merge(*args)

Proc.new { |x| @table.merge x }

end



101
102
103
104
105
106
107
# File 'lib/poly.rb', line 101

def merge(*args)
  # pp caller
  # binding.irb
  @table.merge(*args).tap { |ret|
    pp([__LINE__, "Poly.merge", '->', ret]) if $pd
  }
end

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

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/poly.rb', line 71

def respond_to_missing?(name, include_private = false)
  # name.to_s.end_with?('=') || @table.key?(name.to_sym) || @table.respond_to?(name) || super
  (name.to_s.end_with?('=') || @table.key?(name.to_sym) || @table.respond_to?(name) || super).tap { |ret|
    pp([__LINE__, "Poly.respond_to_missing? #{name}", '->', ret]) if $pd
  }
end

#to_hObject

for export to Prompt library



110
111
112
# File 'lib/poly.rb', line 110

def to_h
  @table.tap { |ret| pp([__LINE__, "Poly.to_h", '->', ret]) if $pd }
end

#to_yamlObject



114
115
116
117
118
# File 'lib/poly.rb', line 114

def to_yaml
  @table.to_yaml.tap { |ret|
    pp([__LINE__, "Poly.to_yaml", '->', ret]) if $pd
  }
end