Module: Nali::Model

Defined in:
lib/nali/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
# File 'lib/nali/model.rb', line 5

def self.included( base )
  base.extend self
  base.class_eval do
    after_destroy { sync }
  end
end

Instance Method Details

#access_action(action, client) ⇒ Object



16
17
18
19
20
21
22
23
# 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) ⇒ Object



12
13
14
# File 'lib/nali/model.rb', line 12

def access_level( client )
  :unknown
end

#call_method(name, params) ⇒ Object



70
71
72
# 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

#clientsObject



61
62
63
# File 'lib/nali/model.rb', line 61

def clients
  Nali::Clients.list
end

#get_sync_params(client) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



65
66
67
68
# 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) ⇒ Object



56
57
58
59
# File 'lib/nali/model.rb', line 56

def sync_initial( params )
  params[ :name ]       = self.class.name
  params[ :attributes ] = { id: self.id }
end