Class: Module
- Defined in:
- lib/puppet/external/event-loop/better-definers.rb,
lib/puppet/external/event-loop/better-definers.rb
Instance Method Summary collapse
-
#define_accessors(*names) ⇒ Object
We don’t need a singular alias for ‘define_accessors’, because it always defines at least two methods.
- #define_guarded_writers(*names, &block) ⇒ Object
- #define_hard_aliases(name_pairs) ⇒ Object
- #define_methods(*names, &body) ⇒ Object
- #define_opposite_accessors(name_pairs) ⇒ Object
- #define_opposite_readers(name_pairs) ⇒ Object
- #define_opposite_writers(name_pairs) ⇒ Object
- #define_private_method(name, &body) ⇒ Object
- #define_private_methods(*names, &body) ⇒ Object
- #define_protected_method(name, &body) ⇒ Object
- #define_protected_methods(*names, &body) ⇒ Object
- #define_reader_with_opposite(name_pair, &body) ⇒ Object
-
#define_readers(*names) ⇒ Object
This method lets you define predicates like :foo?, which will be defined to return the value of @foo.
- #define_soft_aliases(name_pairs) ⇒ Object
- #define_writer_with_opposite(name_pair, &body) ⇒ Object
-
#define_writers(*names, &body) ⇒ Object
If you pass a predicate symbol :foo? to this method, it’ll first define a regular writer method :foo, without a question mark.
-
#guard_writers(*names, &predicate) ⇒ Object
Guard each of the specified attributes by replacing the writer method with a proxy that asks the supplied block before proceeding with the change.
- #writer_defined?(name) ⇒ Boolean
Instance Method Details
#define_accessors(*names) ⇒ Object
We don’t need a singular alias for ‘define_accessors’, because it always defines at least two methods.
125 126 127 128 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 125 def define_accessors (*names) define_readers(*names) define_writers(*names) end |
#define_guarded_writers(*names, &block) ⇒ Object
244 245 246 247 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 244 def define_guarded_writers (*names, &block) define_writers(*names) guard_writers(*names, &block) end |
#define_hard_aliases(name_pairs) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 58 def define_hard_aliases (name_pairs) for new_aliases, existing_name in name_pairs do new_aliases.kind_of? Array or new_aliases = [new_aliases] for new_alias in new_aliases do alias_method(new_alias, existing_name) end end end |
#define_methods(*names, &body) ⇒ Object
172 173 174 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 172 def define_methods (*names, &body) names.each { |name| define_method(name, &body) } end |
#define_opposite_accessors(name_pairs) ⇒ Object
153 154 155 156 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 153 def define_opposite_accessors (name_pairs) define_opposite_readers name_pairs define_opposite_writers name_pairs end |
#define_opposite_readers(name_pairs) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 130 def define_opposite_readers (name_pairs) name_pairs.collect! { |k, v| [k.to_sym, v.to_sym] } for opposite_name, name in name_pairs do define_reader(name) unless method_defined?(name) class_eval %{def #{opposite_name} ; not #{name} end} end end |
#define_opposite_writers(name_pairs) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 138 def define_opposite_writers (name_pairs) name_pairs.collect! { |k, v| [k.to_sym, v.to_sym] } for opposite_name, name in name_pairs do define_writer(name) unless writer_defined?(name) class_eval %{def #{opposite_name.writer} x self.#{name.writer} !x end} class_eval %{def #{opposite_name.imperative} self.#{name.writer} false end} end end |
#define_private_method(name, &body) ⇒ Object
186 187 188 189 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 186 def define_private_method (name, &body) define_method(name, &body) private name end |
#define_private_methods(*names, &body) ⇒ Object
176 177 178 179 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 176 def define_private_methods (*names, &body) define_methods(*names, &body) names.each { |name| private name } end |
#define_protected_method(name, &body) ⇒ Object
191 192 193 194 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 191 def define_protected_method (name, &body) define_method(name, &body) protected name end |
#define_protected_methods(*names, &body) ⇒ Object
181 182 183 184 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 181 def define_protected_methods (*names, &body) define_methods(*names, &body) names.each { |name| protected name } end |
#define_reader_with_opposite(name_pair, &body) ⇒ Object
158 159 160 161 162 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 158 def define_reader_with_opposite (name_pair, &body) name, opposite_name = name_pair.flatten.collect { |x| x.to_sym } define_method(name, &body) define_opposite_reader(opposite_name => name) end |
#define_readers(*names) ⇒ Object
This method lets you define predicates like :foo?, which will be defined to return the value of @foo.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 83 def define_readers (*names) for name in names.map { |x| x.to_sym } do if name.punctuated? # There's no way to define an efficient reader whose # name is different from the instance variable. class_eval %{def #{name} ; @#{name.without_punctuation} end} else # Use `attr_reader' to define an efficient method. attr_reader(name) end end end |
#define_soft_aliases(name_pairs) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 67 def define_soft_aliases (name_pairs) for new_aliases, existing_name in name_pairs do new_aliases.kind_of? Array or new_aliases = [new_aliases] for new_alias in new_aliases do class_eval %{def #{new_alias}(*args, &block) #{existing_name}(*args, &block) end} end end end |
#define_writer_with_opposite(name_pair, &body) ⇒ Object
164 165 166 167 168 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 164 def define_writer_with_opposite (name_pair, &body) name, opposite_name = name_pair.flatten.collect { |x| x.to_sym } define_writer(name, &body) define_opposite_writer(opposite_name => name) end |
#define_writers(*names, &body) ⇒ Object
If you pass a predicate symbol :foo? to this method, it’ll first define a regular writer method :foo, without a question mark. Then it’ll define an imperative writer method :foo! as a shorthand for setting the property to true.
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 104 def define_writers (*names, &body) for name in names.map { |x| x.to_sym } do if block_given? define_method(name.writer, &body) else attr_writer(name.without_punctuation) end if name.predicate? class_eval %{def #{name.imperative} self.#{name.writer} true end} end end end |
#guard_writers(*names, &predicate) ⇒ Object
Guard each of the specified attributes by replacing the writer method with a proxy that asks the supplied block before proceeding with the change.
If it’s okay to change the attribute, the block should return either nil or the symbol :mutable. If it isn’t okay, the block should return a string saying why the attribute can’t be changed. If you don’t want to provide a reason, you can have the block return just the symbol :immutable.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 228 def guard_writers(*names, &predicate) for name in names.map { |x| x.to_sym } do define_hard_alias("__unguarded_#{name.writer}" => name.writer) define_method(name.writer) do |new_value| case result = predicate.call when :mutable, nil __send__("__unguarded_#{name.writer}", new_value) when :immutable raise ImmutableAttributeError.new(name) else raise ImmutableAttributeError.new(name, result) end end end end |
#writer_defined?(name) ⇒ Boolean
96 97 98 |
# File 'lib/puppet/external/event-loop/better-definers.rb', line 96 def writer_defined? (name) method_defined?(name.to_sym.writer) end |