lib/cieloz/helpers.rb in cieloz-0.0.13 vs lib/cieloz/helpers.rb in cieloz-0.0.14

- old
+ new

@@ -12,14 +12,51 @@ instance_variable_set "@#{attr}", value yield(value) if block_given? end } end + + def attrs_from source, opts, *keys + attrs = keys.map { |k| + value_or_attr_name = opts[k] || k + if value_or_attr_name.is_a? Symbol + source.send value_or_attr_name if source.respond_to? value_or_attr_name + else + value_or_attr_name + end + } + attrs.count == 1 ? attrs.first : attrs + end end + module InstanceMethods + def valid? + valid = _valid? + unless @source.nil? + unless valid + errors.messages.each { |attr,attr_errors| + source_attr = @opts[attr] + if source_attr.is_a?(Symbol) and @source.respond_to?(source_attr) + attr_errors.each {|e| @source.errors.add source_attr, e } + else + attr_errors.each {|e| @source.errors.add :base, "#{attr}: #{e}" if e.is_a? String } + end + @source.errors.messages.each {|attr,attr_errors| attr_errors.uniq! } + } + end + end + valid + end + end + def self.included base - base.extend ClassMethods base.send :include, ActiveModel::Validations + base.extend ClassMethods + base.class_eval do + alias :_valid? :valid? + attr_accessor :source, :opts + end + base.send :include, InstanceMethods end def initialize attrs={} self.attributes = attrs end