Sha256: 607db4f3c631a184e78582a7e0fbde9392803fbd867179dc0ab96c82a6b82639

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module AttributeCartographer
  class InvalidBlockArityError < StandardError; end
  class InvalidArgumentError < StandardError; end

  class << self
    def included base
      base.send :extend, AttributeCartographer::ClassMethods
      base.send :include, AttributeCartographer::InstanceMethods
    end
  end

  module ClassMethods
    def map *args
      @mapper ||= {}
      block = (Proc === args.last) ? args.pop : ->(v) { v }

      raise AttributeCartographer::InvalidArgumentError if block.arity > 1

      if Array === args.first
        raise AttributeCartographer::InvalidArgumentError if args.first.empty?
        args.first.each { |arg| @mapper.merge! arg => [arg, block] }
      elsif args.size == 2
        from, to = args
        @mapper.merge! from => [to, block]
      elsif args.size == 1
        from = args.pop
        @mapper.merge! from => [from, block]
      end
    end
  end

  module InstanceMethods
    def initialize attributes
      @_original_attributes = attributes
      mapper = self.class.instance_variable_get(:@mapper)

      mapper.each { |from, (meth, block)|
        value = attributes.has_key?(from) ? block.call(attributes[from]) : nil
        self.send :define_singleton_method, meth, ->{ value }
      } if mapper

      super
    end

    def original_attributes
      @_original_attributes
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
attribute_cartographer-0.0.1 lib/attribute_cartographer.rb