Top Level Namespace

Defined Under Namespace

Modules: Visibilize, VisibilizeGenerator

Instance Method Summary collapse

Instance Method Details

#visibilize(options = {}) ⇒ Object



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

def visibilize(options={})

  column    = options[:column]    || :visible_id
  callback  = options[:callback]  || :before_create
  type      = options[:type]      || :integer
  unique    = options[:unique]    || true
  length    = options[:length]    || 8
  lamb      = options[:lambda]    || nil

  # Define a method to fill the column with appropiate value
  method_name="visibilize_#{column}"
  define_method(method_name) do

    raise "Visibilize Error: Attribute #{column} not found in #{self.class.name} instance." unless self.has_attribute?(column)

    # Values are generated by VisibilizeGenerator class
    value=lamb ? lamb.call : VisibilizeGenerator.get_value_for(type, self.class, column, length, unique)

    write_attribute(column, value)
  end

  # Create a callback to execute method
  callback=callback.to_s.split('_')
  set_callback callback[1].to_sym, callback[0].to_sym, method_name.to_sym

end