Module: DataMapper::Adapters::AbstractAdapter::Migration
- Defined in:
- lib/dm-mapping/adapters/abstract_adapter.rb
Instance Method Summary collapse
-
#auto_genclass!(opts = {}) ⇒ Object
automaticly generate model class(es) and mapping all fields with mapping /.*/ for you.
-
#fields(storage) ⇒ Object
returns all fields, with format [[name, type, attrs]] e.g.
-
#storages ⇒ Object
returns all tables’ name in the repository.
-
#storages_and_fields ⇒ Object
returns a hash with storage names in keys and corresponded fields in values.
Instance Method Details
#auto_genclass!(opts = {}) ⇒ Object
automaticly generate model class(es) and mapping all fields with mapping /.*/ for you.
e.g.
dm.auto_genclass!
# => [DataMapper::Mapping::User,
# DataMapper::Mapping::SchemaInfo,
# DataMapper::Mapping::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::Mapping::User]
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/dm-mapping/adapters/abstract_adapter.rb', line 79 def auto_genclass! opts = {} opts[:scope] ||= DataMapper::Mapping 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 } dmm_genclass mapped, opts[:scope] if mapped.kind_of?(String) }.compact end |
#fields(storage) ⇒ Object
returns all fields, with format [[name, type, attrs]]
e.g.
[[:created_at, DateTime, {:nullable => true}],
[:email, String, {:nullable => true, :size => 255,
:default => '[email protected]'}],
[:id, Integer, {:nullable => false, :serial => true,
:key => true}],
[:salt_first, String, {:nullable => true, :size => 50}],
[:salt_second, String, {:nullable => true, :size => 50}]]
24 25 26 27 28 29 30 31 32 |
# File 'lib/dm-mapping/adapters/abstract_adapter.rb', line 24 def fields storage dmm_query_storage(storage).map{ |field| primitive = dmm_primitive(field) type, chain = self.class.type_map.lookup_primitive(primitive) || dmm_lookup_primitive(primitive) [dmm_field_name(field).to_sym, type, dmm_attributes(field)] } end |
#storages ⇒ Object
returns all tables’ name in the repository.
e.g.
['comments', 'users']
11 12 13 |
# File 'lib/dm-mapping/adapters/abstract_adapter.rb', line 11 def storages raise NotImplementedError end |
#storages_and_fields ⇒ Object
returns a hash with storage names in keys and corresponded fields in values. e.g.
{'users' => [[:id, Integer, {:nullable => false,
:serial => true,
:key => true}],
[:email, String, {:nullable => true,
:default => '[email protected]'}],
[:created_at, DateTime, {:nullable => true}],
[:salt_first, String, {:nullable => true, :size => 50}],
[:salt_second, String, {:nullable => true, :size => 50}]]}
see Migration#storages and Migration#fields for detail
45 46 47 48 49 50 |
# File 'lib/dm-mapping/adapters/abstract_adapter.rb', line 45 def storages_and_fields storages.inject({}){ |result, storage| result[storage] = fields(storage) result } end |