Sha256: 8c67f7514d7366ea28f5924c5e06d2a206689264c763733b41c540c8119d0851

Contents?: true

Size: 909 Bytes

Versions: 4

Compression:

Stored size: 909 Bytes

Contents

module WhirledPeas
  module Component
    module Factory
      class << self
        def register(name, klass)
          classes[name] = klass
        end

        def build(name)
          unless classes.key?(name)
            expected = classes.keys.join(', ')
            raise ArgumentError, "Unrecognized component: #{name.inspect}, expecting one of #{expected}"
          end
          @classes[name].new
        end

        private

        def classes
          return @classes if @classes
          @classes = {}
          Component.constants.each do |const|
            next if const == name
            klass = Component.const_get(const)
            next unless klass.is_a?(Class)
            name = const.to_s.gsub(/([a-z])([A-Z])/, '\1_\2').downcase.to_sym
            @classes[name] = klass
          end
          @classes
        end
      end
    end
  end
  private_constant :Component
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
whirled_peas-0.13.0 lib/whirled_peas/component.rb
whirled_peas-0.12.0 lib/whirled_peas/component.rb
whirled_peas-0.11.1 lib/whirled_peas/component.rb
whirled_peas-0.11.0 lib/whirled_peas/component.rb