lib/clevic/extensions.rb in clevic-0.8.0 vs lib/clevic/extensions.rb in clevic-0.11.1

- old
+ new

@@ -1,22 +1,25 @@ # extensions specific to clevic require 'qtext/flags.rb' +require 'qtext/hash_collector.rb' +class Object + # recursively calls each entry in path_ary + # will return nil if any entry in path_ary + # results in a nil value. + def evaluate_path( path_ary ) + path_ary.inject( self ) do |value, att| + value.nil? ? nil : value.send( att ) + end + end +end + module ActiveRecord class Base - # recursively calls each entry in path_ary - def evaluate_path( path_ary ) - path_ary.inject( self ) do |value, att| - if value.nil? - nil - else - value.send( att ) - end - end - end - + # checks to see if attribute_sym is either in the column + # name list, or in the set of reflections. def self.has_attribute?( attribute_sym ) if column_names.include?( attribute_sym.to_s ) true elsif reflections.has_key?( attribute_sym ) true @@ -54,27 +57,20 @@ # return the Clevic::Field for this index def field @field ||= model.field_for_index( self ) end - # set the value returned from the gui, as whatever the underlying - # entity wants it to be - # TODO this will break for more than 2 objects in a path - def gui_value=( obj ) - entity.send( "#{model.dots[column]}=", obj ) - end - def dump + if valid? <<-EOF - field_name: #{field_name} - field_value: #{field_value} - dotted_path: #{dotted_path.inspect} - attribute_path: #{attribute_path.inspect} - attribute: #{attribute.inspect} - attribute_value: #{attribute_value.inspect} + field: #{field_name} => #{field_value} + attribute: #{attribute.inspect} => #{attribute_value.inspect} metadata: #{metadata.inspect} EOF + else + 'invalid' + end end # return the attribute of the underlying entity corresponding # to the column of this index def attribute @@ -88,26 +84,16 @@ def attribute_value entity.send( attribute ) end # set the value of the attribute, without following the - # full path + # full path. + # TODO remove need to constantly recalculate the attribute writer def attribute_value=( obj ) entity.send( "#{attribute.to_s}=", obj ) end - # the dotted attribute path, same as a 'column' in the model - def dotted_path - model.dots[column] - end - - # return an array of path elements from dotted_path - def attribute_path - return nil if model.nil? - model.attribute_paths[column] - end - # returns the ActiveRecord column_for_attribute def metadata # use the optimised version model.metadata( column ) end @@ -116,19 +102,18 @@ # be suffixed with _id def field_name metadata.name end - # return the value of the field, it the _id value + # return the value of the field, it may be the _id value def field_value entity.send( field_name ) end # the underlying entity def entity return nil if model.nil? - #~ puts "fetching entity from collection for xy=(#{row},#{column})" if @entity.nil? @entity ||= model.collection[row] end attr_writer :entity @@ -146,8 +131,9 @@ # will always return an array that will have zero, one # or many elements. def errors [ entity.errors[field_name.to_sym] ].flatten end + end end