Class: Gamma::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/gamma/hook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#applyObject

Returns the value of attribute apply.



2
3
4
# File 'lib/gamma/hook.rb', line 2

def apply
  @apply
end

#column_nameObject

Returns the value of attribute column_name.



2
3
4
# File 'lib/gamma/hook.rb', line 2

def column_name
  @column_name
end

#hook_typeObject

Returns the value of attribute hook_type.



2
3
4
# File 'lib/gamma/hook.rb', line 2

def hook_type
  @hook_type
end

#root_dirObject

Returns the value of attribute root_dir.



2
3
4
# File 'lib/gamma/hook.rb', line 2

def root_dir
  @root_dir
end

#script_pathObject

Returns the value of attribute script_path.



2
3
4
# File 'lib/gamma/hook.rb', line 2

def script_path
  @script_path
end

Instance Method Details

#execute_script(record) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gamma/hook.rb', line 4

def execute_script(record)
  path = File.join(root_dir, script_path)
  fail "Hook Scripts Not Found. path: #{path}" unless File.exist?(path)

  result = record
  require File.join(root_dir, script_path)

  begin
    klass_name = "#{File.basename(path, ".*").camelize}"
    instance = klass_name.constantize.new
    case self.hook_type.to_s
    when "column"
      r = instance.execute(apply, column_name.to_s, record[column_name.to_s])
      record[column_name.to_s] = r
    when "row"
      record = instance.execute(apply, record)
    else
      fail "Error"
    end
  rescue => e
    raise "Invalid Hook Class #{klass_name}"
  end

  result
end