Class: Invoicing::ClassInfo::Base
- Inherits:
-
Object
- Object
- Invoicing::ClassInfo::Base
- Defined in:
- lib/invoicing/class_info.rb
Overview
Base class for ClassInfo
objects, from which you need to derive a subclass in each module where you want to use ClassInfo
. An instance of a ClassInfo::Base
subclass is created every time an acts_as_
method is called, and that instance can be accessed through the my_module_name_class_info
method on the class which called acts_as_my_module_name
.
Direct Known Subclasses
Invoicing::CachedRecord::ClassInfo, Invoicing::CurrencyValue::ClassInfo, LedgerItem::ClassInfo, LineItem::ClassInfo, TaxRate::ClassInfo, Taxable::ClassInfo, TimeDependent::ClassInfo
Instance Attribute Summary collapse
-
#all_args ⇒ Object
readonly
Union of
current_args
andprevious_info.all_args
. -
#all_options ⇒ Object
readonly
Hash of options with symbolized keys, with
option_defaults
overridden byprevious_info
options, in turn overridden bycurrent_options
. -
#current_args ⇒ Object
readonly
The list of arguments passed to the current
acts_as_
method call (excluding the final options hash). -
#current_options ⇒ Object
readonly
The options hash passed to the current
acts_as_
method call. -
#model_class ⇒ Object
readonly
The class on which the
acts_as_
method was called. -
#new_args ⇒ Object
readonly
self.all_args - previous_info.all_args
. -
#previous_info ⇒ Object
readonly
The
ClassInfo::Base
instance created by the lastacts_as_
method call on the same class (or its superclass);nil
if this is the first call.
Instance Method Summary collapse
-
#get(object, method_name) ⇒ Object
Returns the value returned by calling
method_name
(renamed through options usingmethod
) onobject
. -
#initialize(model_class, previous_info, args) ⇒ Base
constructor
Initialises a
ClassInfo::Base
instance and parses arguments. -
#method(name) ⇒ Object
If there is an option with the given key, returns the associated value; otherwise returns the key.
-
#option_defaults ⇒ Object
Override this method to return a hash of default option values.
-
#set(object, method_name, new_value) ⇒ Object
Assigns
new_value
tomethod_name=
(renamed through options usingmethod
) onobject
.
Constructor Details
#initialize(model_class, previous_info, args) ⇒ Base
Initialises a ClassInfo::Base
instance and parses arguments. If subclasses override initialize
they should call super
.
model_class
-
The class on which the
acts_as
method was called previous_info
-
The
ClassInfo::Base
instance created by the lastacts_as_
method call on the same class (or its superclass);nil
if this is the first call. args
-
Array of arguments given to the
acts_as_
method when it was invoked.
If the last element of args
is a hash, it is used as an options array. All other elements of args
are concatenated into an array, uniqed and flattened. (They could be a list of symbols representing method names, for example.)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/invoicing/class_info.rb', line 146 def initialize(model_class, previous_info, args) @model_class = model_class @previous_info = previous_info = args..symbolize_keys = (@previous_info.nil? ? option_defaults : @previous_info.).clone .update() @all_args = @new_args = @current_args = args.flatten.uniq unless @previous_info.nil? @all_args = (@previous_info.all_args + @all_args).uniq @new_args = @all_args - previous_info.all_args end end |
Instance Attribute Details
#all_args ⇒ Object (readonly)
Union of current_args
and previous_info.all_args
124 125 126 |
# File 'lib/invoicing/class_info.rb', line 124 def all_args @all_args end |
#all_options ⇒ Object (readonly)
Hash of options with symbolized keys, with option_defaults
overridden by previous_info
options, in turn overridden by current_options
.
134 135 136 |
# File 'lib/invoicing/class_info.rb', line 134 def end |
#current_args ⇒ Object (readonly)
The list of arguments passed to the current acts_as_
method call (excluding the final options hash)
121 122 123 |
# File 'lib/invoicing/class_info.rb', line 121 def current_args @current_args end |
#current_options ⇒ Object (readonly)
The options hash passed to the current acts_as_
method call
130 131 132 |
# File 'lib/invoicing/class_info.rb', line 130 def end |
#model_class ⇒ Object (readonly)
The class on which the acts_as_
method was called
114 115 116 |
# File 'lib/invoicing/class_info.rb', line 114 def model_class @model_class end |
#new_args ⇒ Object (readonly)
self.all_args - previous_info.all_args
127 128 129 |
# File 'lib/invoicing/class_info.rb', line 127 def new_args @new_args end |
#previous_info ⇒ Object (readonly)
The ClassInfo::Base
instance created by the last acts_as_
method call on the same class (or its superclass); nil
if this is the first call.
118 119 120 |
# File 'lib/invoicing/class_info.rb', line 118 def previous_info @previous_info end |
Instance Method Details
#get(object, method_name) ⇒ Object
Returns the value returned by calling method_name
(renamed through options using method
) on object
. Returns nil
if object
is nil
or object
does not respond to that method.
175 176 177 178 |
# File 'lib/invoicing/class_info.rb', line 175 def get(object, method_name) meth = method(method_name) (object.nil? || !object.respond_to?(meth)) ? nil : object.send(meth) end |
#method(name) ⇒ Object
If there is an option with the given key, returns the associated value; otherwise returns the key. This is useful for mapping method names to their renamed equivalents through options.
168 169 170 171 |
# File 'lib/invoicing/class_info.rb', line 168 def method(name) name = name.to_sym ([name] || name).to_s end |
#option_defaults ⇒ Object
Override this method to return a hash of default option values.
162 163 164 |
# File 'lib/invoicing/class_info.rb', line 162 def option_defaults {} end |
#set(object, method_name, new_value) ⇒ Object
Assigns new_value
to method_name=
(renamed through options using method
) on object
. method_name
should not include the equals sign.
182 183 184 |
# File 'lib/invoicing/class_info.rb', line 182 def set(object, method_name, new_value) object.send("#{method(method_name)}=", new_value) unless object.nil? end |