Class: Recursive_Open_Struct

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

Overview

Recursive_Open_Struct provides a convenient interface to a hierarchy of configuration parameters. You do not need to define the accessors, they are created automatically on demand.

Have a look at this example:

ac = Recursive_Open_Struct.new
ac.window.name = "SuperDuper"
ac.app.version = "2.1.3"
ac.this.is.automatically.created = "blabla"

After you have created all of your configuration parameters, to prevent typos when using the parameters, the structure can be closed:

ac.close

After closing,

ac.widnow.name = "UberSuperDuper"

You get the usual NoMethodError, because ‘widnow’ does not exist.

Instance Method Summary collapse

Constructor Details

#initializeRecursive_Open_Struct

Create a new Recursive_Open_Struct.



27
28
29
30
# File 'lib/Recursive_Open_Struct.rb', line 27

def initialize
  @methods = Hash.new
  @open = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params) ⇒ Object

automatically add parameters



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/Recursive_Open_Struct.rb', line 33

def method_missing(method, *params) # :nodoc:
  # setting or getting?
  is_setting = !params.empty?

  key = method.id2name
  # remove trailing =
  key.chop! if is_setting

  # if structure is closed, disable hierarchy creation
  super unless @methods.has_key?(key) || @open

  if is_setting
    # assigning a new value
    if @methods[key].class == Recursive_Open_Struct
      raise TypeError, "overwriting previously created hierarchy entry '#{key}' not allowed", caller(1)
    end
    @methods[key] = *params
  else
    # no param: create new Recursive_Open_Struct object, if nothing is set.
    unless @methods.has_key?(key)
      @methods[key] = Recursive_Open_Struct.new
    end
  end
  @methods[key]
end

Instance Method Details

#[](key) ⇒ Object

An alternative way to access the value of an attribute

s = Recursive_Open_Struct.new
s.name = "Hugo"
s["name"] # "Hugo"


63
64
65
# File 'lib/Recursive_Open_Struct.rb', line 63

def [](key)
  @methods[key]
end

#[]=(key, value) ⇒ Object

An alternative way to set the value of an attribute

s = Recursive_Open_Struct.new
s["name"] = "Hugo"
s.name # "Hugo"


71
72
73
# File 'lib/Recursive_Open_Struct.rb', line 71

def []=(key, value)
  self.send((key+"=").to_sym, value)
end

#attrsObject

call-seq:

attrs() -> an_array

Return a sorted array of attribute names, similar to #methods.

s = Recursive_Open_Struct.new
s.name = "martinus"
s.age = 25
s.attrs # returns ["age", "name"]


84
85
86
# File 'lib/Recursive_Open_Struct.rb', line 84

def attrs
  @methods.keys.sort
end

#closeObject

After calling #close, no further modification of the configuration hierarchy is allowed. This is not as strict as #freeze, because you are still allowed to modify data.

s = Recursive_Open_Struct.new
s.name = "Hugo"
s.close
s.name = "martinus" # does still work
s.age = 25 # raises NoMethodError


97
98
99
# File 'lib/Recursive_Open_Struct.rb', line 97

def close
  do_set_open_status(false)
end

#eachObject

call-seq:

each() { |elem| ... }

Iterates through all elements of the Recursive_Open_Struct in alphabetic order.



124
125
126
127
128
# File 'lib/Recursive_Open_Struct.rb', line 124

def each
  attrs.each do |attr|
    yield @methods[attr]
  end
end

#open?Boolean

call-seq:

open?() -> boolean

Return whether the structure is still open or not.

s = Recursive_Open_Struct.new
s.open? # returns true
s.close
s.open? # returns false

Returns:

  • (Boolean)


116
117
118
# File 'lib/Recursive_Open_Struct.rb', line 116

def open?
  @open
end

#re_openObject

Reopens a Recursive_Open_Struct which was closed with #close earlier. After this call, it is possible to modify the structure again.



103
104
105
# File 'lib/Recursive_Open_Struct.rb', line 103

def re_open
  do_set_open_status(true)
end