Class: Oj::Bag
- Inherits:
-
Object
- Object
- Oj::Bag
- Defined in:
- lib/oj/bag.rb
Overview
A generic class that is used only for storing attributes. It is the base Class for auto-generated classes in the storage system. Instance variables are added using the instance_variable_set() method. All instance variables can be accessed using the variable name (without the @ prefix). No setters are provided as the Class is intended for reading only.
Class Method Summary collapse
-
.define_class(classname) ⇒ Object
Define a new class based on the Oj::Bag class.
Instance Method Summary collapse
-
#eql?(other) ⇒ Boolean
(also: #==)
Replaces eql?() with something more reasonable for this Class.
-
#initialize(args = {}) ⇒ Bag
constructor
The initializer can take multiple arguments in the form of key values where the key is the variable name and the value is the variable value.
-
#method_missing(m, *args, &block) ⇒ Boolean
Handles requests for variable values.
-
#respond_to?(m) ⇒ Boolean
Replaces the Object.respond_to?() method.
Constructor Details
#initialize(args = {}) ⇒ Bag
The initializer can take multiple arguments in the form of key values where the key is the variable name and the value is the variable value. This is intended for testing purposes only.
17 18 19 20 21 |
# File 'lib/oj/bag.rb', line 17 def initialize(args = {}) args.each do |k, v| self.instance_variable_set(k, v) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Boolean
Handles requests for variable values. Others cause an Exception to be raised.
39 40 41 42 43 44 45 46 |
# File 'lib/oj/bag.rb', line 39 def method_missing(m, *args, &block) raise ArgumentError.new("wrong number of arguments (#{args.size} for 0) to method #{m}") unless args.nil? or args.empty? at_m = :"@#{m}" raise NoMethodError.new("undefined method #{m}", m) unless instance_variable_defined?(at_m) instance_variable_get(at_m) end |
Class Method Details
.define_class(classname) ⇒ Object
Define a new class based on the Oj::Bag class. This is used internally in the Oj module and is available to service wrappers that receive XML requests that include Objects of Classes not defined in the storage process.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/oj/bag.rb', line 69 def self.define_class(classname) classname = classname.to_s unless classname.is_a?(String) tokens = classname.split('::').map(&:to_sym) raise NameError.new("Invalid classname '#{classname}") if tokens.empty? m = Object tokens[0..-2].each do |sym| if m.const_defined?(sym) m = m.const_get(sym) else c = Module.new m.const_set(sym, c) m = c end end sym = tokens[-1] if m.const_defined?(sym) c = m.const_get(sym) else c = Class.new(Oj::Bag) m.const_set(sym, c) end c end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
Replaces eql?() with something more reasonable for this Class.
51 52 53 54 55 56 57 58 59 |
# File 'lib/oj/bag.rb', line 51 def eql?(other) return false if (other.nil? or self.class != other.class) ova = other.instance_variables iv = instance_variables return false if ova.size != iv.size iv.all? { |vid| instance_variable_get(vid) != other.instance_variable_get(vid) } end |
#respond_to?(m) ⇒ Boolean
Replaces the Object.respond_to?() method.
27 28 29 30 31 |
# File 'lib/oj/bag.rb', line 27 def respond_to?(m) return true if super instance_variables.include?(:"@#{m}") end |