Module: Parameters::ClassMethods
- Defined in:
- lib/parameters/class_methods.rb
Instance Method Summary collapse
-
#describe_param(name) ⇒ String
Returns the description of the class parameters with a given name.
-
#each_param {|param| ... } ⇒ Object
Iterates over the parameters of the class and it's ancestors.
-
#get_param(name) ⇒ ClassParam
Searches for the class parameter with the matching name.
-
#has_param?(name) ⇒ Boolean
Determines if a class parameter exists with the given name.
-
#param_value(name) ⇒ Object
Returns the value of the class parameters with a given name.
-
#parameter(name, options = {}) ⇒ Object
Adds a new parameters to the class.
-
#params ⇒ Hash
Parameters for the class.
-
#params=(values) ⇒ Object
Sets the values of the class parameters.
-
#set_param(name, value) ⇒ Object
Sets a class parameter.
Instance Method Details
#describe_param(name) ⇒ String
Returns the description of the class parameters with a given name.
226 227 228 |
# File 'lib/parameters/class_methods.rb', line 226 def describe_param(name) get_param(name).description end |
#each_param {|param| ... } ⇒ Object
Iterates over the parameters of the class and it's ancestors.
205 206 207 208 209 210 211 212 213 |
# File 'lib/parameters/class_methods.rb', line 205 def each_param(&block) ancestors.reverse_each do |ancestor| if ancestor.included_modules.include?(Parameters) ancestor.params.each_value(&block) end end return self end |
#get_param(name) ⇒ ClassParam
Searches for the class parameter with the matching name.
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/parameters/class_methods.rb', line 150 def get_param(name) name = name.to_sym ancestors.each do |ancestor| if ancestor.included_modules.include?(Parameters) if ancestor.params.has_key?(name) return ancestor.params[name] end end end raise(Parameters::ParamNotFound,"parameter #{name.to_s.dump} was not found in class #{self}") end |
#has_param?(name) ⇒ Boolean
Determines if a class parameter exists with the given name.
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/parameters/class_methods.rb', line 124 def has_param?(name) name = name.to_sym ancestors.each do |ancestor| if ancestor.included_modules.include?(Parameters) return true if ancestor.params.has_key?(name) end end return false end |
#param_value(name) ⇒ Object
Returns the value of the class parameters with a given name.
241 242 243 |
# File 'lib/parameters/class_methods.rb', line 241 def param_value(name) get_param(name).value end |
#parameter(name, options = {}) ⇒ Object
Adds a new parameters to the class.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/parameters/class_methods.rb', line 69 def parameter(name,={}) name = name.to_sym # define the reader class method for the parameter (name) do get_param(name).value end # define the writer class method for the parameter ("#{name}=") do |value| get_param(name).value = value end # define the ? method, to determine if the parameter is set ("#{name}?") do !!get_param(name).value end # define the reader instance methods for the parameter define_method(name) do get_param(name).value end # define the writter instance methods for the parameter define_method("#{name}=") do |value| get_param(name).value = value end # define the ? method, to determine if the parameter is set define_method("#{name}?") do !!get_param(name).value end # create the new parameter new_param = Parameters::ClassParam.new( name, [:type], [:description], [:default] ) # add the parameter to the class params list params[name] = new_param return new_param end |
#params ⇒ Hash
Returns Parameters for the class.
13 14 15 |
# File 'lib/parameters/class_methods.rb', line 13 def params @parameters ||= {} end |
#params=(values) ⇒ Object
Sets the values of the class parameters.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/parameters/class_methods.rb', line 29 def params=(values) values.each do |name,value| if has_param?(name) get_param(name).value = case value when Parameters::ClassParam, Parameters::InstanceParam value.value else value end end end end |
#set_param(name, value) ⇒ Object
Sets a class parameter.
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/parameters/class_methods.rb', line 183 def set_param(name,value) name = name.to_sym ancestors.each do |ancestor| if ancestor.included_modules.include?(Parameters) if ancestor.params.has_key?(name) return ancestor.params[name].set(value) end end end raise(Parameters::ParamNotFound,"parameter #{name.to_s.dump} was not found in class #{self}") end |