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

#initialize(params = {}) ⇒ FieldLocks

@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

#locksObject

object_name: [method_name, method_name1]


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

def locks
  @locks
end

Instance Method Details

#has_locks?Boolean

Returns:

  • (Boolean)

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

def has_locks?
  locks.keys.any?
end

#lock(object_name, method) ⇒ Object

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

#locked?(object_name, method) ⇒ Boolean

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

#resolve(object_name, method, value) ⇒ Object?

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

#to_paramsHash

Returns:

  • (Hash)

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

def to_params
  locks
end

#unlock(object_name, method) ⇒ Object

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