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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain_name, to:, model:, **opts) ⇒ MappingDsl

1) mapping ‘feature.domain’, to: :db, model: ‘foo.db.bar’, contextual_key: false do

end

mapping 'feature.domain' web_api: 'web_api.http.model' do
 map 'id'
 map 'foo', 'bar'
 map 'biz', 'baz'
end


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 22

def initialize(domain_name, to:, model:, **opts)
  opts          ||= {}
  @entries        = []
  @domain_name    = domain_name.to_s
  @map_class      = opts[:map_class] || StorageMap
  @container_name = opts[:container] || Appfuel.default_app_name

  @contextual_key = true
  if opts.key?(:contextual_key) && opts[:contextual_key] == false
    @contextual_key = false
  end
  @storage_type = to
  @storage_key  = translate_storage_key(to, model)

  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

#map_classObject (readonly)

Returns the value of attribute map_class.



9
10
11
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 9

def map_class
  @map_class
end

#storage_keyObject (readonly)

Returns the value of attribute storage_key.



9
10
11
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 9

def storage_key
  @storage_key
end

#storage_typeObject (readonly)

Returns the value of attribute storage_type.



9
10
11
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 9

def storage_type
  @storage_type
end

Instance Method Details

#contextual_key?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 68

def contextual_key?
  @contextual_key
end

#create_storage_mapObject



39
40
41
42
43
44
45
46
47
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 39

def create_storage_map
  map_class.new(
    domain_name:  domain_name,
    container:    container_name,
    storage_type: storage_type,
    storage_key:  storage_key,
    entries:      entries
  )
end

#map(name, domain_attr = nil, opts = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 49

def map(name, domain_attr = nil, opts = {})
  if domain_attr.is_a?(Hash)
    opts = domain_attr
    domain_attr = nil
  end

  domain_attr = name if domain_attr.nil?

  skip = opts[:skip] == true ? true : false

  data = opts.merge({
    domain_attr: domain_attr,
    storage_attr: name,
    skip: skip
  })

  entries << data
end

#translate_storage_key(type, partial_key) ⇒ Object

global.user global.storage.db.user membership.user features.membership.type.user



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/appfuel/storage/repository/mapping_dsl.rb', line 76

def translate_storage_key(type, partial_key)
  return partial_key unless contextual_key?
  fail "#{type} model key can not be empty" if partial_key.empty?

  # take the feature or domain root unless its global
  domain_top, _domain_base = domain_name.split('.')
  top, *parts = partial_key.split('.')

  if top == 'global'
    root = top
    base = parts.join('.')
  else
    root = "features.#{domain_top}"
    base = top
  end

  "#{root}.#{type}.#{base}"
end