Class: Invoicing::ClassInfo::Base

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

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 last acts_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

  @current_options = args.extract_options!.symbolize_keys
  @all_options = (@previous_info.nil? ? option_defaults : @previous_info.all_options).clone
  @all_options.update(@current_options)

  @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_argsObject (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_optionsObject (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 all_options
  @all_options
end

#current_argsObject (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_optionsObject (readonly)

The options hash passed to the current acts_as_ method call


130
131
132
# File 'lib/invoicing/class_info.rb', line 130

def current_options
  @current_options
end

#model_classObject (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_argsObject (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_infoObject (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
  (all_options[name] || name).to_s
end

#option_defaultsObject

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