Sha256: 740fe679bdb898a48520b1680b85582e56c2d49cfc6873cca390a3d3920c2a17
Contents?: true
Size: 1.45 KB
Versions: 1
Compression:
Stored size: 1.45 KB
Contents
module Hipbot module Collection extend ActiveSupport::Concern included do extend ClassMethods attr_accessor :id, :name, :attributes alias_method :to_s, :name end def initialize params self.id = params.delete(:id) self.name = params.delete(:name) self.attributes = params end def update_attribute key, value if key == :name self.name = value else self.attributes[key] = value end end def update_attributes hash hash.each do |k, v| update_attribute k, v end end def delete self.class.collection.delete(self.id) end module ClassMethods def create params, &block collection[params[:id]] = new(params, &block) end def collection @collection ||= {} end def [] *items items.first.is_a?(Array) ? find_many(*items) : find_one(items.first) end def find_one item collection[item] || find{ |i| i.name == item } end def find_many *items items.flatten! items.map{ |i| find_one(i) }.compact.uniq end def find_or_create_by params find_one(params[:id] || params[:name]) || create(params) end protected def method_missing name, *args, &block return collection.values.public_send(name, *args, &block) if Array.instance_methods.include?(name) super end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
hipbot-1.0.0.rc1 | lib/hipbot/collection.rb |