Class: Forms::FieldLocks

Inherits:
Object
  • Object
show all
Defined in:
lib/forms/field_locks.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (FieldLocks) initialize(params = {})

@param a instance of some ActiveRecord model



10
11
12
13
# File 'lib/forms/field_locks.rb', line 10

def initialize(params = {})
  @locks = params
  @locks ||= {}
end

Instance Attribute Details

- (Object) locks

object_name: [method_name, method_name1]



7
8
9
# File 'lib/forms/field_locks.rb', line 7

def locks
  @locks
end

Instance Method Details

- (Boolean) has_locks?

Returns:

  • (Boolean)


23
24
25
# File 'lib/forms/field_locks.rb', line 23

def has_locks?
  locks.keys.any?
end

- (Object) lock(object_name, method)

Parameters:

  • object_name (Object)
  • method (String, Symbol)

Returns:

  • (Object)


48
49
50
51
# File 'lib/forms/field_locks.rb', line 48

def lock(object_name, method)
  locks[object_name] ||= {}
  locks[object_name].merge!(method => 1)
end

- (Boolean) locked?(object_name, method)

Parameters:

  • object_name (Object)
  • method (String, Symbol)

Returns:

  • (Boolean)


18
19
20
# File 'lib/forms/field_locks.rb', line 18

def locked?(object_name, method)
  (locks[object_name.to_s] && locks[object_name.to_s][method.to_s].to_s == '1') ? true : false
end

- (Object?) resolve(object_name, method, value)

Parameters:

  • object_name (Object)
  • method (String, Symbol)
  • value (Object)

Returns:

  • (Object, nil)


36
37
38
39
40
41
42
43
# File 'lib/forms/field_locks.rb', line 36

def resolve(object_name, method, value)
  return nil if value.nil?
  if locked?(object_name.to_s, method.to_s)
    value
  else
    nil
  end
end

- (Hash) to_params

Returns:

  • (Hash)


28
29
30
# File 'lib/forms/field_locks.rb', line 28

def to_params
  locks
end

- (Object) unlock(object_name, method)

Parameters:

  • object_name (Object)
  • method (String, Symbol)

Returns:

  • (Object)


56
57
58
# File 'lib/forms/field_locks.rb', line 56

def unlock(object_name, method)
  locks[object_name].delete(method)
end