Sha256: 2a875700b81c85df971c03f4305863c056fda84c381afb151b0ac01f7ea48e64

Contents?: true

Size: 928 Bytes

Versions: 2

Compression:

Stored size: 928 Bytes

Contents

class Object
  def dsl_method(name,&b)
    define_method(name) do |*args|
      if args.empty?
        res = instance_variable_get("@#{name}")
        if res.nil? && block_given?
          res = b.call
          instance_variable_set("@#{name}",res) 
        end
        res
      else
        instance_variable_set("@#{name}",args.first)
      end
    end
  end
  def dsl_class_method(name,&b)
    self.class.dsl_method(name,&b)
  end
end

class OrderedHash
  def reject(&b)
    res = OrderedHash.new
    each { |k,v| res[k] = v unless yield(k,v) }
    res
  end
end

class Object
  def sos(m)
    respond_to?(m) ? send(m) : self
  end
  def klass
    self.class
  end
end

module FromHash
  def from_hash(ops)
    ops.each do |k,v|
      send("#{k}=",v)
    end
    self
  end
  def initialize(ops={})
    from_hash(ops)
  end
end

class Hash
  def map_value
    res = {}
    each { |k,v| res[k] = yield(v) }
    res
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongo_persist-0.0.2 lib/mongo_persist/util.rb
mongo_persist-0.0.1 lib/mongo_persist/util.rb