Module: FoundationApi::Table::Persistence::ClassMethods
- Defined in:
- lib/foundation_api/table/persistence.rb
Instance Method Summary collapse
- #create(attributes = {}) ⇒ Object
- #create!(attributes = {}) ⇒ Object
- #destroy(*ids) ⇒ Object
-
#persistence(options = {}) ⇒ Object
specify persistence support.
- #remote_create(object) ⇒ Object
- #remote_destroy(*ids) ⇒ Object
Instance Method Details
#create(attributes = {}) ⇒ Object
51 52 53 |
# File 'lib/foundation_api/table/persistence.rb', line 51 def create(attributes = {}) new(attributes).save end |
#create!(attributes = {}) ⇒ Object
54 55 56 |
# File 'lib/foundation_api/table/persistence.rb', line 54 def create!(attributes = {}) new(attributes).save! end |
#destroy(*ids) ⇒ Object
58 59 60 |
# File 'lib/foundation_api/table/persistence.rb', line 58 def destroy(*ids) remote_destroy *ids end |
#persistence(options = {}) ⇒ Object
specify persistence support. You may specify all on one line if you like Example:
persistence create: :CreatePossession, unique: :name
persistence update: :UpdatePossession
persistence destroy: :DestroyPossession, destroy_key: possessionId
23 24 25 26 |
# File 'lib/foundation_api/table/persistence.rb', line 23 def persistence( = {}) .assert_valid_keys(:create, :update, :destroy, :unique, :hash_to_array, :destroy_key, :destroy_type) self. = self..merge() end |
#remote_create(object) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/foundation_api/table/persistence.rb', line 28 def remote_create(object) unique_key = object.[:unique] if unique_key existing_object = where(unique_key => object[unique_key], :deleted => 0).first if existing_object raise RecordNotUnique, "Record with key #{unique_key}: #{object[unique_key]} already exists" end end id = request [:create], object.map_attributes ::Rails.logger.debug "FoundationApi::Table::Persistence.remote_create: request response: #{id.inspect}" id.is_a?(Array) ? id.first : id end |
#remote_destroy(*ids) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/foundation_api/table/persistence.rb', line 41 def remote_destroy(*ids) if [:destroy_type] == :array request [:destroy], ids else ids.flatten.each do |id| request [:destroy], {destroy_key => id } end end end |