lib/pg_serializable/nodes/attribute.rb in pg_serializable-0.1.0 vs lib/pg_serializable/nodes/attribute.rb in pg_serializable-0.1.1

- old
+ new

@@ -1,14 +1,25 @@ module PgSerializable module Nodes class Attribute < Base - def initialize(column_name, label: nil) + def initialize(column_name, label: nil, &prc) @column_name = column_name @label = label || column_name + @prc = prc if block_given? end def to_sql(table_alias=nil) - ["\'#{@label}\'", "#{table_alias ? table_alias + '.' : ''}#{@column_name}"].join(',') + [key, value(table_alias)].join(',') + end + + private + def key + "\'#{@label}\'" + end + + def value(tbl) + val = "#{tbl && "#{tbl}."}#{@column_name}" + @prc ? @prc.call(val) : val end end end end