Sha256: dd992c37bad3a560b7b76ad6922d0f251138a6042dae2c4605d544bde6bd6c6f

Contents?: true

Size: 783 Bytes

Versions: 10

Compression:

Stored size: 783 Bytes

Contents

module Cieloz
  module Helpers
    module ClassMethods
      def hattr_writer *attrs
        attrs.each { |attr|
          define_method "#{attr}=" do |value|
            if value.is_a? Hash
              name = attr.to_s.camelize
              cls = self.class.const_get name
              value = cls.new(value)
            end
            instance_variable_set "@#{attr}", value
            yield(value) if block_given?
          end
        }
      end
    end

    def self.included base
      base.extend ClassMethods
      base.send :include, ActiveModel::Validations
    end

    def initialize attrs={}
      self.attributes = attrs
    end

    def attributes= attrs
      attrs.each {|k,v|
        m = "#{k}="
        send(m, v) if respond_to? m
      }
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cieloz-0.0.13 lib/cieloz/helpers.rb
cieloz-0.0.12 lib/cieloz/helpers.rb
cieloz-0.0.11 lib/cieloz/helpers.rb
cieloz-0.0.10 lib/cieloz/helpers.rb
cieloz-0.0.9 lib/cieloz/helpers.rb
cieloz-0.0.8 lib/cieloz/helpers.rb
cieloz-0.0.7 lib/cieloz/helpers.rb
cieloz-0.0.4 lib/cieloz/helpers.rb
cieloz-0.0.3 lib/cieloz/helpers.rb
cieloz-0.0.2 lib/cieloz/helpers.rb