Module: Parameters::ClassMethods

Defined in:
lib/parameters/class_methods.rb

Instance Method Summary

Instance Method Details

- (String) describe_param(name)

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.


146
147
148
# File 'lib/parameters/class_methods.rb', line 146

def describe_param(name)
  get_param(name).description
end

- (Object) each_param(&block) {|param| ... }

Iterates over the parameters of the class and it’s ancestors.

Yields:

  • (param) — The block that will be passed each class parameter.


127
128
129
130
131
132
133
134
135
# File 'lib/parameters/class_methods.rb', line 127

def each_param(&block)
  ancestors.each do |ancestor|
    if ancestor.include?(Parameters)
      ancestor.params.each_value(&block)
    end
  end

  return self
end

- (ClassParam) get_param(name)

Searches for the class parameter with the matching name.

Parameters:

  • (Symbol, String) name — 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.


90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/parameters/class_methods.rb', line 90

def get_param(name)
  name = name.to_sym

  ancestors.each do |ancestor|
    if ancestor.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.name.dump}",caller)
end

- (Boolean) has_param?(name)

Specifies whether or not there is a class parameter with the specified name.

Returns:

  • (Boolean) — Specifies whether or not there is a class parameter with the specified name.


109
110
111
112
113
114
115
116
117
118
119
# File 'lib/parameters/class_methods.rb', line 109

def has_param?(name)
  name = name.to_sym

  ancestors.each do |ancestor|
    if ancestor.include?(Parameters)
      return true if ancestor.params.has_key?(name)
    end
  end

  return false
end

- (Object) param_value(name)

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.


159
160
161
# File 'lib/parameters/class_methods.rb', line 159

def param_value(name)
  get_param(name).value
end

- (Object) parameter(name, options = {})

Adds a new parameters to the class.

Examples:

  parameter 'var'

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

Parameters:

  • (Symbol, String) name — The name of the new parameter.
  • (Hash) options (defaults to: {}) — Additional options.

Options Hash (options):

  • (String) :description N/A — The description for the new parameter.
  • (Object, Proc) :default N/A — The default value for the new parameter.


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/parameters/class_methods.rb', line 58

def parameter(name,options={})
  name = name.to_sym

  # add the parameter to the class params list
  params[name] = Parameters::ClassParam.new(name,options[:description],options[:default])

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

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

  # define the getter/setter instance methods for the parameter
  attr_accessor(name)
end

- (Hash) params

Parameters for the class.

Returns:

  • (Hash) — Parameters for the class.


11
12
13
# File 'lib/parameters/class_methods.rb', line 11

def params
  @params ||= {}
end

- (Object) params=(values)

Sets the values of the class parameters.

Examples:

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

Parameters:

  • (Hash) values — The names and new values to set the class params to.


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/parameters/class_methods.rb', line 25

def params=(values)
  values.each do |name,value|
    if has_param?(name)
      if (value.kind_of?(Parameters::ClassParam) || value.kind_of?(Parameters::InstanceParam))
        value = value.value
      end

      get_param(name).value = value
    end
  end
end