Class: Appfuel::Db::Mapper

Inherits:
Repository::Mapper show all
Defined in:
lib/appfuel/storage/db/mapper.rb

Instance Attribute Summary

Attributes inherited from Repository::Mapper

#container_root_name

Instance Method Summary collapse

Methods inherited from Repository::Mapper

#create_entity_hash, #entity?, #entity_attr?, #entity_container_name, #entity_value, #expr_conjunction?, #fetch_storage_class, #initialize, #map, #resolve_entity_value, #storage_attr, #storage_class, #storage_key, #storage_map, #to_entity_hash, #to_storage, #undefined?, #update_entity_hash

Constructor Details

This class inherits a constructor from Appfuel::Repository::Mapper

Instance Method Details

#convert_conjunction(expr, values = [], entry = nil) ⇒ Object



52
53
54
55
56
# File 'lib/appfuel/storage/db/mapper.rb', line 52

def convert_conjunction(expr, values = [], entry = nil)
  left, values  = convert_expr(expr.left, values, entry)
  right, values = convert_expr(expr.right, values, entry)
  ["#{left} #{expr.op} #{right}", values]
end

#convert_expr(expr, values = [], entry = nil) ⇒ Array

Converts an entity expression into a valid active record expresion expression.

Parameters:

Returns:

  • (Array)

    The first index is the expr string using ? for values The second index is the actual value(s)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/appfuel/storage/db/mapper.rb', line 34

def convert_expr(expr, values = [], entry = nil)
  if expr_conjunction?(expr)
    return convert_conjunction(expr, values, entry)
  end

  column = qualified_db_column(expr, entry)
  op     = expr.op
  arg    = case expr.op
           when 'in', 'not in' then '(?)'
           when 'between', 'not between' then '? AND ?'
           else
             '?'
           end

  values << expr.value
  ["#{column} #{op} #{arg}", values]
end

#convert_order_exprs(list) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/appfuel/storage/db/mapper.rb', line 96

def convert_order_exprs(list)
  str = ''
  list.each do |expr|
    db_column = qualified_db_column(expr)
    direction = expr.op
    str << "#{db_column} #{direction}, "
  end

  str.strip.chomp(',')
end

#db_table_column(expr, entry = nil) ⇒ table_name, column

Determine Domain Mapentry and DbModel from entity expression.

Parameters:

  • expr (SpCore::Domain::Expr)

Returns:

  • (table_name, column)
    Array


18
19
20
21
22
23
24
25
# File 'lib/appfuel/storage/db/mapper.rb', line 18

def db_table_column(expr, entry = nil)
  storage_map = storage_map(:db, expr.domain_name)

  db     = fetch_storage_class(storage_map.storage_key)
  column = storage_map.storage_attr(expr.domain_attr)

  [db.table_name, column]
end

#exists?(domain_expr) ⇒ Boolean

Validates if a record exists in the table that matches the array with the conditions given.

Parameters:

  • criteria (Criteria)

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
# File 'lib/appfuel/storage/db/mapper.rb', line 63

def exists?(domain_expr)
  domain_name = domain_expr.domain_name
  domain_attr = domain_expr.domain_attr

  entry = find(domain_name, domain_attr)
  db_expr, values = convert_expr(domain_expr, [], entry)
  db = storage_class_from_entry(entry, :db)

  db.exists?([db_expr, *values])
end

#limit(criteria, relation) ⇒ ActiveRecord::Relation

Eventhough there is no mapping here we add the interface for consistency.

Parameters:

  • criteria (Criteria)
  • relation (DbModel, ActiveRecord::Relation)

Returns:

  • (ActiveRecord::Relation)


113
114
115
116
117
# File 'lib/appfuel/storage/db/mapper.rb', line 113

def limit(criteria, relation)
  return relation unless criteria.limit?

  relation.limit(criteria.limit)
end

#order(criteria, relation) ⇒ ActiveRecord::Relation

Build an order by expression for the given db relation based on the criteria

Parameters:

  • criteria (Criteria)
  • relation (DbModel, ActiveRecord::Relation)

Returns:

  • (ActiveRecord::Relation)


90
91
92
93
94
# File 'lib/appfuel/storage/db/mapper.rb', line 90

def order(criteria, relation)
  return relation unless criteria.order?
  order_str = convert_order_exprs(criteria.order_by)
  relation.order(order_str)
end

#qualified_db_column(expr, entry = nil) ⇒ Object

Return qualified db column name from entity expression.

Parameters:

  • expr (SpCore::Domain::Expr)

Returns:

  • db column name [String]



9
10
11
12
# File 'lib/appfuel/storage/db/mapper.rb', line 9

def qualified_db_column(expr, entry = nil)
  table_name, column = db_table_column(expr, entry)
  "#{table_name}.#{column}"
end

#storage_hash(db_model) ⇒ Object



119
120
121
# File 'lib/appfuel/storage/db/mapper.rb', line 119

def storage_hash(db_model)
  db_model.attributes.select {|_, value| !value.nil?}
end

#where(criteria, relation) ⇒ DbModel, ActiveRecord::Relation

Build a where expression from the mapped db class using the criteria.Ï

Parameters:

  • criteria (Criteria)
  • relation (DbModel, ActiveRecord::Relation)

Returns:

  • (DbModel, ActiveRecord::Relation)


79
80
81
82
# File 'lib/appfuel/storage/db/mapper.rb', line 79

def where(criteria, relation)
  conditions, values = convert_expr(criteria.filters)
  relation.where(conditions, *values)
end