Class: Appfuel::Repository::MappingDsl

Inherits:
Object
  • Object
show all
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]

Instance Attribute Summary collapse

Instance Method Summary collapse

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/bar/baz.yml -> <root_path>/foo/bar/baz.yml
        path: /foo/bar/baz.yml -> /foo/bar/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, options = {})
  fail "options must be a hash" unless options.is_a?(Hash)

  @entries        = []
  @domain_name    = domain_name.to_s
  @entry_class    = options[:entry_class] || MappingEntry
  @container_name = options[:container] || Appfuel.default_app_name
  @storage        = initialize_storage(options)

  fail "entity name can not be empty" if @domain_name.empty?
end

Instance Attribute Details

#container_nameObject (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_nameObject (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

#entriesObject (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_classObject (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, options = {})


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

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 120

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



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 125

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