Class: ODPI::Object::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/odpi/object.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, objtype = nil, object = nil) ⇒ Base

Returns a new instance of Base.


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/odpi/object.rb', line 166

def initialize(conn, objtype = nil, object = nil)
  @conn = conn
  @objtype = objtype || @conn.raw_connection.object_type(ODPI::Object.find_name_by_class(self.class))
  @object = object || @objtype.create_object
  if @objtype.is_collection?
    self.extend(NamedCollection)
    @attr_types = nil
    @elem_type = @objtype.element_type_info
    @bindclass = ODPI::BindType::ObjectAttrMapping[@elem_type.oracle_type].to_bindclass(@elem_type)
  else
    @attr_types = {}
    @objtype.attributes.each do |attr|
      attr_name = ODPI::Object.oracle_name_to_ruby_identifier_name(attr.name).intern
      @attr_types[attr_name] = attr
    end
    @elem_type = nil
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/odpi/object.rb', line 185

def method_missing(method_id, *args)
  unless @objtype.is_collection?
    if @attr_types.has_key?(method_id)
      self.class.class_eval do
        define_method(method_id) do
          attr = @attr_types[method_id]
          bindclass = ODPI::BindType::ObjectAttrMapping[attr.type_info.oracle_type].to_bindclass(attr.type_info)
          val = @object.attribute_value(attr, bindclass::TYPES[1])
          val.nil? ? nil : bindclass.convert_out(@conn, val)
        end
      end
      return self.send(method_id, *args)
    elsif method_id[-1] == '=' && @attr_types.has_key?(attr_name = method_id[0..-2].intern)
      self.class.class_eval do
        define_method(method_id) do |val|
          attr = @attr_types[attr_name]
          bindclass = ODPI::BindType::ObjectAttrMapping[attr.type_info.oracle_type].to_bindclass(attr.type_info)
          val = val.nil? ? nil : bindclass.convert_in(@conn, val)
          @object.set_attribute_value(attr, bindclass::TYPES[1], val)
        end
      end
      return self.send(method_id, *args)
    end
  end
  super(method_id, *args)
end

Class Method Details

.set_typename(name) ⇒ Object


162
163
164
# File 'lib/odpi/object.rb', line 162

def self.set_typename(name)
  ODPI::Object.set_typename(self, name)
end

Instance Method Details

#__attr_names__Object


232
233
234
# File 'lib/odpi/object.rb', line 232

def __attr_names__
  @attr_types.keys
end

#__object__Object


236
237
238
# File 'lib/odpi/object.rb', line 236

def __object__
  @object
end

#inspectObject


228
229
230
# File 'lib/odpi/object.rb', line 228

def inspect
  self.to_s
end

#to_sObject


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/odpi/object.rb', line 212

def to_s
  attrs = if @attr_types
            @attr_types.keys.collect do |name| "#{name}: #{self.send(name).inspect}" end
          else
            ary = self.to_hash.map do |key, val|
              "#{key}: #{val.inspect}"
            end
            if ary.length > 4
              ary[0..4] << '...'
            else
              ary
            end
          end
  %Q[#<#{self.class}: #{@objtype.schema}.#{@objtype.name} (#{attrs.join(', ')})>]
end