module Nali::Model

Public Class Methods

included( base ) click to toggle source
# File lib/nali/model.rb, line 5
def self.included( base )
  base.extend self
  base.class_eval do
    after_destroy { sync }
  end
end

Public Instance Methods

access_action( action, client ) { |options| ... } click to toggle source
# File lib/nali/model.rb, line 16
def access_action( action, client )
  level = self.access_level client
  if access_levels = access_options[ action ] and access_levels.keys.include?( level )
    options = []
    ( [] << access_levels[ level ] ).flatten.compact.each { |option| options << option.to_sym }
    yield options
  end
end
access_level( client ) click to toggle source
# File lib/nali/model.rb, line 12
def access_level( client )
  :unknown
end
call_method( name, params ) click to toggle source
# File lib/nali/model.rb, line 70
def call_method( name, params )
  clients.each { |client| client.call_method self, name, params if client.watch?( self ) }
end
clients() click to toggle source
# File lib/nali/model.rb, line 61
def clients
  Nali::Clients.list
end
get_sync_params( client ) click to toggle source
# File lib/nali/model.rb, line 25
def get_sync_params( client )
  params    = {}
  relations = []
  if self.destroyed?
    sync_initial params
    params[ :destroyed ] = true
  else
    access_action( :read, client ) do |options|
      sync_initial params
      options.each do |option|
        if self.respond_to?( option )
          value = self.send option
          if value.is_a?( ActiveRecord::Relation )
            relations << value
          elsif value.is_a?( ActiveRecord::Base )
            relations << value
            if self.respond_to?( key = option.to_s + '_id' ) and self[ key ] == value.id
              params[ :attributes ][ key ] = value.id
            end
          else
            params[ :attributes ][ option ] = value
          end
        end
      end
      params[ :created ] = self.created_at.to_f
      params[ :updated ] = self.updated_at.to_f
    end
  end
  [ params, relations.flatten.compact ]
end
sync( *watches ) click to toggle source
# File lib/nali/model.rb, line 65
def sync( *watches )
  watches.flatten.each { |client| client.watch self }
  clients.each { |client| client.sync self if client.watch?( self ) }
end
sync_initial( params ) click to toggle source
# File lib/nali/model.rb, line 56
def sync_initial( params )
  params[ :name ]       = self.class.name
  params[ :attributes ] = { id: self.id }
end

Private Instance Methods

access_options() click to toggle source
# File lib/nali/model.rb, line 76
def access_options
  Nali::Application.access_options[ self.class.name.to_sym ] or {}
end