Class: Appfuel::Db::Mapper
- Inherits:
-
Repository::Mapper
- Object
- Repository::Mapper
- Appfuel::Db::Mapper
- Defined in:
- lib/appfuel/storage/db/mapper.rb
Instance Attribute Summary
Attributes inherited from Repository::Mapper
Instance Method Summary collapse
- #convert_conjunction(expr, values = [], entry = nil) ⇒ Object
-
#convert_expr(expr, values = [], entry = nil) ⇒ Array
Converts an entity expression into a valid active record expresion expression.
- #convert_order_exprs(list) ⇒ Object
-
#db_table_column(expr, entry = nil) ⇒ table_name, column
Determine Domain Mapentry and DbModel from entity expression.
-
#exists?(domain_expr) ⇒ Boolean
Validates if a record exists in the table that matches the array with the conditions given.
-
#limit(criteria, relation) ⇒ ActiveRecord::Relation
Eventhough there is no mapping here we add the interface for consistency.
-
#order(criteria, relation) ⇒ ActiveRecord::Relation
Build an order by expression for the given db relation based on the criteria.
-
#qualified_db_column(expr, entry = nil) ⇒ Object
Return qualified db column name from entity expression.
- #storage_hash(db_model) ⇒ Object
-
#where(criteria, relation) ⇒ DbModel, ActiveRecord::Relation
Build a where expression from the mapped db class using the criteria.Ï.
Methods inherited from Repository::Mapper
#create_entity_hash, #each_entity_attr, #entity?, #entity_attr?, #entity_value, #expr_conjunction?, #find, #initialize, #map, #resolve_entity_value, #storage_attr, #storage_attr_mapped?, #storage_class, #storage_class_from_entry, #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
50 51 52 53 54 |
# File 'lib/appfuel/storage/db/mapper.rb', line 50 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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/appfuel/storage/db/mapper.rb', line 32 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
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/appfuel/storage/db/mapper.rb', line 94 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.
18 19 20 21 22 23 |
# File 'lib/appfuel/storage/db/mapper.rb', line 18 def db_table_column(expr, entry = nil) entry ||= find(expr.domain_name, expr.domain_attr) db = storage_class_from_entry(entry, :db) [db.table_name, entry.storage_attr] end |
#exists?(domain_expr) ⇒ Boolean
Validates if a record exists in the table that matches the array with the conditions given.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/appfuel/storage/db/mapper.rb', line 61 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.
111 112 113 114 115 |
# File 'lib/appfuel/storage/db/mapper.rb', line 111 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
88 89 90 91 92 |
# File 'lib/appfuel/storage/db/mapper.rb', line 88 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.
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
117 118 119 |
# File 'lib/appfuel/storage/db/mapper.rb', line 117 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.Ï
77 78 79 80 |
# File 'lib/appfuel/storage/db/mapper.rb', line 77 def where(criteria, relation) conditions, values = convert_expr(criteria.filters) relation.where(conditions, *values) end |