module DmIsReflective::DataObjectsAdapter

Public Instance Methods

auto_genclass!(opts = {}) click to toggle source

automaticly generate model class(es) and reflect all fields with reflect /.*/ for you.

e.g.
     dm.auto_genclass!
     # => [DataMapper::Is::Reflective::User,
     #     DataMapper::Is::Reflective::SchemaInfo,
     #     DataMapper::Is::Reflective::Session]

you can change the scope of generated models:

e.g.
     dm.auto_genclass! :scope => Object
     # => [User, SchemaInfo, Session]

you can generate classes for tables you specified only:

e.g.
     dm.auto_genclass! :scope => Object, :storages => /^phpbb_/
     # => [PhpbbUser, PhpbbPost, PhpbbConfig]

you can generate classes with String too:

e.g.
     dm.auto_genclass! :storages => ['users', 'config'], :scope => Object
     # => [User, Config]

you can generate a class only:

e.g.
     dm.auto_genclass! :storages => 'users'
     # => [DataMapper::Is::Reflective::User]
# File lib/dm-is-reflective/adapters/data_objects_adapter.rb, line 86
def auto_genclass! opts = {}
  opts[:scope] ||= DmIsReflective
  opts[:storages] ||= /.*/
  opts[:storages] = [opts[:storages]].flatten

  storages.map{ |storage|

    mapped = opts[:storages].each{ |target|
      case target
        when Regexp;
          break storage if storage =~ target

        when Symbol, String;
          break storage if storage == target.to_s

        else
          raise ArgumentError.new("invalid argument: #{target.inspect}")
      end
    }

    reflective_genclass(mapped, opts[:scope]) if mapped.kind_of?(String)
  }.compact
end
fields(storage) click to toggle source

returns all fields, with format [[name, type, attrs]]

e.g.
     [[:created_at,  DateTime, {:required => false}],
      [:email,       String,   {:required => false, :size => 255,
                                :default => 'nospam@nospam.tw'}],
      [:id, DataMapper::Property::Serial,  {:required => true, :serial => true,
                                :key => true}],
      [:salt_first,  String,   {:required => false, :size => 50}],
      [:salt_second, String,   {:required => false, :size => 50}]]
# File lib/dm-is-reflective/adapters/data_objects_adapter.rb, line 28
def fields storage
  reflective_query_storage(storage).map{ |field|
    attr = reflective_attributes(field)
    type = reflective_lookup_primitive(reflective_primitive(field))
    pick = if attr[:serial] && type == Integer
             Property::Serial
           else
             type
           end
    [reflective_field_name(field).to_sym, pick, attr]
  }
end
indices(storage) click to toggle source

returns all indices in the storage.

# File lib/dm-is-reflective/adapters/data_objects_adapter.rb, line 14
def indices storage
  reflective_auto_load_adapter_extension
  indices(storage) # call the overrided method
end
storages() click to toggle source

returns all tables' name in the repository.

e.g.
     ['comments', 'users']
# File lib/dm-is-reflective/adapters/data_objects_adapter.rb, line 8
def storages
  reflective_auto_load_adapter_extension
  storages # call the overrided method
end
storages_and_fields() click to toggle source

returns a hash with storage names in keys and corresponded fields in values. e.g.

{'users' => [[:id,          Integer,  {:required => true,
                                       :serial => true,
                                       :key => true}],
             [:email,       String,   {:required => false,
                                       :default => 'nospam@nospam.tw'}],
             [:created_at,  DateTime, {:required => false}],
             [:salt_first,  String,   {:required => false, :size => 50}],
             [:salt_second, String,   {:required => false, :size => 50}]]}

see AbstractAdapter#storages and AbstractAdapter#fields for detail

# File lib/dm-is-reflective/adapters/data_objects_adapter.rb, line 52
def storages_and_fields
  storages.inject({}){ |result, storage|
    result[storage] = fields(storage)
    result
  }
end

Private Instance Methods

reflective_auto_load_adapter_extension() click to toggle source
# File lib/dm-is-reflective/adapters/data_objects_adapter.rb, line 131
def reflective_auto_load_adapter_extension
  # TODO: can we fix this shit in dm-mysql-adapter?
  name = options[:adapter] || options['adapter']
  # TODO: can we fix this adapter name in dm-sqlite-adapter?
  adapter = name.sub(/\Asqlite3\Z/, 'sqlite')

  require "dm-is-reflective/adapters/#{adapter}_adapter"
  class_name = "#{Inflector.camelize(adapter)}Adapter"
  Adapters.const_get(class_name).__send__(:include,
    DmIsReflective.const_get(class_name))
end
reflective_genclass(storage, scope) click to toggle source
# File lib/dm-is-reflective/adapters/data_objects_adapter.rb, line 116
def reflective_genclass storage, scope
  model = Class.new
  model.__send__(:include, Resource)
  model.is(:reflective)
  model.storage_names[:default] = storage
  scope.const_set(Inflector.classify(storage), model)
  model.__send__(:reflect, /.*/)
  model
end
reflective_indices_hash(key, idx_uni, idx_com) click to toggle source
# File lib/dm-is-reflective/adapters/data_objects_adapter.rb, line 143
def reflective_indices_hash key, idx_uni, idx_com
  h = {}
  h[:key]          = key     if key
  h[:unique_index] = idx_uni if idx_uni
  h[       :index] = idx_com if idx_com
  h
end
reflective_lookup_primitive(primitive) click to toggle source
# File lib/dm-is-reflective/adapters/data_objects_adapter.rb, line 126
def reflective_lookup_primitive primitive
  warn "#{primitive} not found for #{self.class}: #{caller.inspect}"
  String # falling back to the universal interface
end
reflective_query_storage(storage) click to toggle source
# File lib/dm-is-reflective/adapters/data_objects_adapter.rb, line 111
def reflective_query_storage storage
  reflective_auto_load_adapter_extension
  reflective_query_storage(storage) # call the overrided method
end