Class: Appfuel::Repository::MappingDsl
- Inherits:
-
Object
- Object
- Appfuel::Repository::MappingDsl
- Defined in:
- lib/appfuel/storage/repository/mapping_dsl.rb
Overview
A mapping dsl that allows the collection of database columns to domain attributes. The reason this dsl is separated from the DbEntityMapper is due to the fact that we use method_missing for collecting column names and don’t want any incorrect method_missing calls to be confused when collecting mapped values vs when defining them.
Constant Summary collapse
- STORAGE_TYPES =
[:db, :file, :memory, :web_api]
Instance Attribute Summary collapse
-
#container_name ⇒ Object
readonly
Returns the value of attribute container_name.
-
#domain_name ⇒ Object
readonly
Returns the value of attribute domain_name.
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#entry_class ⇒ Object
readonly
Returns the value of attribute entry_class.
-
#storage(type = nil, *args) ⇒ Object
readonly
5) mapping ‘feature.domain’ db: true do end.
Instance Method Summary collapse
- #all_storage_symbols?(*args) ⇒ Boolean
- #db(key) ⇒ Object
-
#initialize(domain_name, options = {}) ⇒ MappingDsl
constructor
1) mapping ‘feature.domain’, db: true, do …
- #map(name, domain_attr = nil, opts = {}) ⇒ Object
Constructor Details
#initialize(domain_name, options = {}) ⇒ MappingDsl
1) mapping ‘feature.domain’, db: true, do
...
end
2) mapping ‘feature.domain’, db: ‘foo.bar’, do
...
end
3) mapping ‘feature.domain’, db: ‘global.bar’ do
...
end
4) mapping ‘feature.domain’, db: ‘foo.bar.baz’, key_translation: false) 4) mapping ‘feature.domain’, storage: [:db, :file] do
...
end
a file model requires the domain_name it represents.
case1 - build a model with default settings
file: storage.file.model
path: <root_path>/storage/file/{key}.yaml
adapter: storage.file.model
case2 - build a model with given settings
note: if path is absolute nothing is done
if path is relative we will prepend root_path
if no yaml extension the key is translated to a path
path: foo//baz.yml -> <root_path>/foo//baz.yml
path: /foo//baz.yml -> /foo//baz.yml
path auth.user -> <root_path>/storage/features/auth/file/user.yml
path gobal.user -> <root_path/storage/global/auth/file/user.yml
case3 - build a model with adapter and path
path: sames as above
adapter translates key to location of adapter in storage
container
default key -> storage.file.model is default
auth.user -> features.auth.storage.file.user
file ‘storage.file.model’
storage db: ‘foo.user_user’ storage :file storage :memory
storage db: ‘foo.user_ath’,
file: 'storage.file.model',
memory: 'storage.memory.model'
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 66 def initialize(domain_name, = {}) fail "options must be a hash" unless .is_a?(Hash) @entries = [] @domain_name = domain_name.to_s @entry_class = [:entry_class] || MappingEntry @container_name = [:container] || Appfuel.default_app_name @storage = initialize_storage() fail "entity name can not be empty" if @domain_name.empty? end |
Instance Attribute Details
#container_name ⇒ Object (readonly)
Returns the value of attribute container_name.
9 10 11 |
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 9 def container_name @container_name end |
#domain_name ⇒ Object (readonly)
Returns the value of attribute domain_name.
9 10 11 |
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 9 def domain_name @domain_name end |
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
9 10 11 |
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 9 def entries @entries end |
#entry_class ⇒ Object (readonly)
Returns the value of attribute entry_class.
9 10 11 |
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 9 def entry_class @entry_class end |
#storage(type = nil, *args) ⇒ Object (readonly)
5) mapping ‘feature.domain’ db: true do
end
6) mapping ‘feature.domain’, db: true do
storage :db, 'foo.bar'
storage :file
end
storage(type = nil, = {})
91 92 93 |
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 91 def storage @storage end |
Instance Method Details
#all_storage_symbols?(*args) ⇒ Boolean
117 118 119 120 |
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 117 def all_storage_symbols?(*args) result = args - STORAGE_TYPES result.empty? end |
#db(key) ⇒ Object
78 79 80 |
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 78 def db(key) @storage[:db] = translate_storage_key(key) end |
#map(name, domain_attr = nil, opts = {}) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 122 def map(name, domain_attr = nil, opts = {}) domain_attr = name if domain_attr.nil? data = opts.merge({ domain_name: domain_name, domain_attr: domain_attr, storage: storage, storage_attr: name, container: container_name, }) @entries << entry_class.new(data) end |