Module: Sack::Database::Model::Data
- Defined in:
- lib/sack/database/model/data.rb
Overview
Data Module: Provides a simple data access layer through models.
Constant Summary collapse
- VALIDATED_ACTIONS =
Actions requiring Validation
[ :create, :update, :save ]
Instance Method Summary collapse
-
#method_missing(name, *args) ⇒ Object
Method Missing: Catches and routes model actions through database.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Method Missing: Catches and routes model actions through database.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sack/database/model/data.rb', line 33 def method_missing name, *args # Check action allowed super name, *args unless ACTIONS.include? name # Acquire Database db = args.slice! 0 # Check Validation Required if VALIDATED_ACTIONS.include? name # Acquire Entity data = args.last # Pre-load for updates data = data.clone.merge find(db, args.slice(0)) if name == :update # Validate errors = [] raise Validation::ValidationException.new "Invalid Entity [#{data}] for Model #{@model}", errors unless is_valid? db, data, errors end # Forward to Database result = db.send name, table_name, *args # Inject Model Proxy into Entity/ies (result.is_a?(Array) ? result : [result]).each { |e| e.instance_variable_set('@model_mod', self); e.define_singleton_method(:method_missing) { |n, *a| @model_mod.send n, *a, self } } unless result.frozen? result end |