Sha256: 4db9e47cc0b37220256df7b7d4c151b4e028de044db1118deaaba905d948a4c5

Contents?: true

Size: 846 Bytes

Versions: 2

Compression:

Stored size: 846 Bytes

Contents

module Lotus
  module Validations
    class Attributes
      def initialize(definitions, attributes)
        attributes  = attributes.to_h

        @attributes = Utils::Hash.new.tap do |result|
          definitions.iterate(attributes) do |name, validations|
            value = attributes[name]
            value = attributes[name.to_s] if value.nil?

            result[name] = Attribute.new(attributes, name, value, validations)
          end
        end
      end

      def get(name)
        (attr = @attributes[name]) and attr.value
      end

      def dup
        Utils::Hash.new(to_h).deep_dup
      end

      def each(&blk)
        @attributes.each(&blk)
      end

      def to_h
        ::Hash.new.tap do |result|
          each do |name, _|
            result[name] = get(name)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lotus-validations-0.2.1 lib/lotus/validations/attributes.rb
lotus-validations-0.2.0 lib/lotus/validations/attributes.rb