Module: Parameters::ClassMethods

Defined in:
lib/parameters/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#describe_param(name) ⇒ String

Returns the description of the class parameters with a given name.

Returns:

  • (String)

    Description of the class parameter with the specified name.

Raises:

  • (ParamNotFound)

    No class parameter with the specified name could be found.



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.

Yields:

  • (param)

    The block that will be passed each class parameter.



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.

Parameters:

  • name (Symbol, String)

    The class parameter name to search for.

Returns:

  • (ClassParam)

    The class parameter with the matching name.

Raises:

  • (ParamNotFound)

    No class parameter with the specified name could be found.



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.

Returns:

  • (Boolean)

    Specifies whether or not there is a class parameter with the specified 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.

Returns:

  • (Object)

    Value of the class parameter with the specified name.

Raises:

  • (ParamNotFound)

    No class parameter with the specified name could be found.



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.

Examples:

parameter 'var'
parameter 'var', :default => 3, :description => 'my variable' 

Parameters:

  • name (Symbol, String)

    The name of the new parameter.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :type (Class, Array[Class])

    The type to enforce the parameter values to.

  • :default (Object, Proc)

    The default value for the new parameter.

  • :description (String)

    The description for the new parameter.



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,options={})
  name = name.to_sym

  # define the reader class method for the parameter
  meta_def(name) do
    get_param(name).value
  end

  # define the writer class method for the parameter
  meta_def("#{name}=") do |value|
    get_param(name).value = value
  end

  # define the ? method, to determine if the parameter is set
  meta_def("#{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,
    options[:type],
    options[:description],
    options[:default]
  )

  # add the parameter to the class params list
  params[name] = new_param
  return new_param
end

#paramsHash

Returns Parameters for the class.

Returns:

  • (Hash)

    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.

Examples:

Test.params = {:x => 5, :y => 2}
# => {:x=>5, :y=>2}

Parameters:

  • values (Hash)

    The names and new values to set the class params to.



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.

Parameters:

  • name (Symbol, String)

    The name of the class parameter.

  • value (Object)

    The new value for the class parameter.

Returns:

  • (Object)

    The new value of the class parameter.

Raises:

  • (ParamNotfound)

    No class parameter with the specified name could be found.

Since:

  • 0.3.0



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