Class: ConfigStruct::Struct

Inherits:
Struct
  • Object
show all
Defined in:
lib/configurable/config_struct.rb

Overview

A beefed-up version of Ruby’s built-in Struct (and in fact a subclass of it). An instance of a ConfigStruct::Struct is created just as you would create a normal struct:

A = ConfigStruct::Struct.new(:a, :b)

You can then create an instance of this struct using hash parameters or ordered parameters:

a = A.new(:a => 1, :b => 2)     # => #<struct A a=1, b=2>

You can pass values in order simultaneously with hash parameters. Values passed as hash parameters override those passed as normal parameters:

a = A.new(1, 2, :a => 3)        # => #<struct A a=3, b=2>

ConfigStruct::Structs also get deeply copied, that is, any member of a ConfigStruct::Struct instance that is also an instance of a ConfigStruct::Struct will get copied recursively:

a = A.new(1, 2)         # => #<struct A a=1, b=2>
b = A.new(3, a)         # => #<struct A a=3, b=#<struct A a=1, b=2>>
c = b.dup
b.object_id == c.object_id      # => false
b.b.object_id == c.b.object_id  # => false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(*args) ⇒ Object

:nodoc:



42
43
44
45
# File 'lib/configurable/config_struct.rb', line 42

def self.new(*args) #:nodoc:
  klass = super
  klass.send(:include, HashInitializer)
end

Instance Method Details

#initialize_copy(orig) ⇒ Object

Recursively copies members of the original struct that are also instances of ConfigStruct::Struct.



49
50
51
52
53
54
# File 'lib/configurable/config_struct.rb', line 49

def initialize_copy(orig)
  super
  for member in members
    self[member] = self[member].dup if self[member].is_a? Struct
  end
end

#replace(*args) ⇒ Object

Replaces the current values with the given values. Values can be given in order:

A = ConfigStruct::Struct.new(:a, :b)
a = A.new(1, 2)
a.replace(3, 4)               # => #<struct A a=3, b=4>

…or as hash parameters:

a.replace(:a => 3, :b => 4)   # => #<struct A a=3, b=4>

As when creating new instances, values given as hash parameters override those given as positional parameters:

a.replace(3, 4, :a => 5)      # => #<struct A a=5, b=4>

Any members not given values will be set to nil.



74
75
76
77
78
79
80
81
82
# File 'lib/configurable/config_struct.rb', line 74

def replace(*args)
  hash_args = args.extract_options!
  for member, v in members.zip(args)
    replace_member!(member, v)
  end
  for k, v in hash_args
    replace_member!(k, v)
  end
end

#to_args(box = true) ⇒ Object

Puts config values into an array suitable for expanding into a parameter list. This is for convenience in recursive traversals of a struct instance.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/configurable/config_struct.rb', line 133

def to_args(box = true)
  args = each_pair.inject({}) do |hsh, pair|
    k, v = pair
    hsh.tap do |h|
      next if v.nil?
      h[k.to_sym] = if v.is_a? Struct
                      v.to_args(false)
                    else
                      v
                    end
    end
  end
  box ? args.to_args : args
end

#to_hashObject

Converts the struct to a hash with string keys. This allows you, for example, to trivially serialize the struct as YAML.

A = ConfigStruct::Struct.new(:a, :b)
A.new(1, 2).to_hash           # => {"a"=>1, "b"=>2}


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/configurable/config_struct.rb', line 117

def to_hash
  members.inject({}) do |hsh, k|
    v = self[k]
    hsh.tap do |h|
      h[k] = if v.is_a? Struct
               v.to_hash
             else
               v
             end
    end
  end
end

#update(*args) ⇒ Object Also known as: merge!

Updates the current values from the given values. Values can be given in order:

A = ConfigStruct::Struct.new(:a, :b)
a = A.new(1, 2)               # => #<struct A a=1, b=2>
a.update(3)                   # => #<struct A a=3, b=2>

…or as hash parameters:

a.update(:a => 3)             # => #<struct A a=3, b=2>

As when creating new instances, values given as hash parameters override those given as positional parameters:

a.replace(3, :a => 5)         # => #<struct A a=5, b=2>


100
101
102
103
104
105
106
107
108
# File 'lib/configurable/config_struct.rb', line 100

def update(*args)
  hash_args = args.extract_options!
  args.each_with_index do |v, i|
    update_member!(i, v)
  end
  for k, v in hash_args
    update_member!(k, v)
  end
end