Module: DmIsReflective::DataObjectsAdapter
- Includes:
- DataMapper
- Defined in:
- lib/dm-is-reflective/adapters/data_objects_adapter.rb
Instance Method Summary collapse
-
#auto_genclass!(opts = {}) ⇒ Object
automaticly generate model class(es) and reflect all fields with reflect /.*/ 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 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]
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dm-is-reflective/adapters/data_objects_adapter.rb', line 80 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) ⇒ Object
returns all fields, with format [[name, type, attrs]]
e.g.
[[:created_at, DateTime, {:required => false}],
[:email, String, {:required => false, :size => 255,
:default => '[email protected]'}],
[:id, DataMapper::Property::Serial, {:required => true, :serial => true,
:key => true}],
[:salt_first, String, {:required => false, :size => 50}],
[:salt_second, String, {:required => false, :size => 50}]]
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/dm-is-reflective/adapters/data_objects_adapter.rb', line 22 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 |
#storages ⇒ Object
returns all tables’ name in the repository.
e.g.
['comments', 'users']
8 9 10 11 |
# 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 ⇒ Object
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 => '[email protected]'}],
[: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
46 47 48 49 50 51 |
# File 'lib/dm-is-reflective/adapters/data_objects_adapter.rb', line 46 def storages_and_fields storages.inject({}){ |result, storage| result[storage] = fields(storage) result } end |