Sha256: 934c2e5a4164ecf3971654c8e5b6627817c7f9038241c7061d95dc2a10b67615

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

module RiceBubble
  class Attributes
    attr_reader :attributes

    include Enumerable

    def initialize(attrs = {})
      @attributes = {}
      attrs.each do |name, attr|
        self[name] = attr
      end
    end

    def initialize_copy(other)
      super
      @attributes = other.attributes.dup
    end

    def each(&)
      if block_given?
        attributes.each(&)
      else
        to_enum(:each)
      end
    end

    def include?(name)
      attributes.key?(name.to_sym)
    end

    def [](name)
      attributes[name.to_sym]
    end

    def []=(name, attr)
      attributes[name] = attr
    end

    def map(&)
      attributes.keys.each.with_object({}) do |name, hash|
        hash[name] = yield(name, attributes[name])
      end
    end

    def self.[](type)
      @types ||= {}
      @types[type] ||=
        begin
          class_name = type.to_s.gsub(/(^|_)(\w)/) do
            ::Regexp.last_match(2).upcase
          end
          Object.const_get "RiceBubble::Attributes::#{class_name}"
        end
    rescue NameError
      nil
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rice_bubble-0.2.1 lib/rice_bubble/attributes.rb
rice_bubble-0.2.0 lib/rice_bubble/attributes.rb
rice_bubble-0.1.2 lib/rice_bubble/attributes.rb
rice_bubble-0.1.1 lib/rice_bubble/attributes.rb
rice_bubble-0.1.0 lib/rice_bubble/attributes.rb