Sha256: 54993182c3bcf861050b6975830a311fac852c8962ef0a95c7435592e637b698

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require "cubbyhole/version"

def Object.const_missing(name)
  eval "#{name} = Class.new(Cubbyhole::Base)"
end

module Cubbyhole
  class Base
    def self.create(params={})
      new(params).save
    end

    def self.get(id)
      objs[id]
    end

    def self.next_id
      @next_id ||= 0
      id = @next_id
      @next_id += 1
      id
    end

    def self.all
      objs.values
    end

    def self.objs
      @objs ||= {}
    end

    def initialize(params={})
      @id = self.class.next_id
      @params = params
      @persisted = false
      stringify_keys!
    end

    attr_reader :id

    def persisted?
      @persisted
    end

    def save
      @persisted = true
      stringify_keys!
      self.class.objs[@id] = self
    end

    def method_missing(meth, *args, &blk)
      key = meth.to_s

      if key =~ /=$/
        raise ArgumentError unless args.size == 1
        @params[key.gsub(/=$/, "")] = args.first
      else
        raise ArgumentError unless args.size == 0
        @params[key]
      end
    end

    def respond_to?(*args); true; end

    def update_attributes(params)
      @params.merge!(params)
      stringify_keys!
      save
    end

    def stringify_keys!
      @params.keys.each do |key|
        @params[key.to_s] = @params.delete(key)
      end
      @params
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cubbyhole-0.1.1 lib/cubbyhole.rb