Class: SimpleTk::GetSetHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_tk/get_set_helper.rb

Overview

A helper class to make hash instance variables more accessible.

Instance Method Summary collapse

Constructor Details

#initialize(parent, hash_ivar, auto_symbol = true, getter = nil, setter = nil, key_filter = nil) ⇒ GetSetHelper

Creates a helper for a specific hash in a parent object.

This helper does not support adding items to the connected hash.

Parameters:

parent

The parent object this helper accesses.

hash_ivar

The name (as a symbol) of the instance variable in the parent object to access. (eg - :@data)

auto_symbol

Should names be converted to symbols in the accessors, defaults to true.

getter

The method or proc to call when getting a value. If this is a Symbol then it defines the method name of the retrieved value to call. If this is a Proc then it will be called with the retrieved value as an argument. If this is nil then the retrieved value is returned.

setter

The method or proc to call when setting a value. If this is a Symbol then it defines the method name of the retrieved value to call with the new value. If this ia a Proc then it will be called with the retrieved value and the new value as arguments. If this is nil then the hash value is replaced with the new value.

key_filter

If set to a proc keys are passed to this proc and only kept if the proc returns a true value. Any key rejected by the filter become unavailable via the helper.

GetSetHelper.new(my_obj, :@data)
GetSetHelper.new(my_obj, :@data, true, :value, :value=)
GetSetHelper.new(my_obj, :@data, true, ->(v){ v.value }, ->(i,v){ i.value = v })


36
37
38
39
40
41
42
43
# File 'lib/simple_tk/get_set_helper.rb', line 36

def initialize(parent, hash_ivar, auto_symbol = true, getter = nil, setter = nil, key_filter = nil)
  @parent = parent
  @hash_ivar = hash_ivar
  @auto_symbol = auto_symbol
  @getter = getter
  @setter = setter
  @key_filter = key_filter.is_a?(Proc) ? key_filter : nil
end

Instance Method Details

#[](name) ⇒ Object

Gets the value for the given name.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/simple_tk/get_set_helper.rb', line 58

def [](name)
  name = name.to_sym if @auto_symbol
  h = @parent.instance_variable_get(@hash_ivar)
  if keys.include?(name)
    v = h[name]
    if @getter.is_a?(Symbol)
      v.send @getter
    elsif @getter.is_a?(Proc)
      @getter.call v
    else
      v
    end
  else
    raise IndexError
  end
end

#[]=(name, value) ⇒ Object

Sets the value for the given name.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/simple_tk/get_set_helper.rb', line 77

def []=(name, value)
  name = name.to_sym if @auto_symbol
  h = @parent.instance_variable_get(@hash_ivar)
  if keys.include?(name)
    if @setter.is_a?(Symbol)
      h[name].send @setter, value
    elsif @setter.is_a?(Proc)
      @setter.call h[name], value
    else
      h[name] = value
    end
  else
    raise IndexError
  end
end

#eachObject

Iterates over the hash.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/simple_tk/get_set_helper.rb', line 95

def each
  h = @parent.instance_variable_get(@hash_ivar)
  if block_given?
    keys.each do |k|
      yield k, h[k]
    end
  elsif @key_filter
    h.dup.keep_if{ |k,v| @key_filter.call(k) }.each
  else
    h.each
  end
end

#keysObject

Gets the available keys.



47
48
49
50
51
52
53
54
# File 'lib/simple_tk/get_set_helper.rb', line 47

def keys
  h = @parent.instance_variable_get(@hash_ivar)
  if @key_filter
    h.keys.select &@key_filter
  else
    h.keys.dup
  end
end