Class: Ohm::Model::Set
- Inherits:
-
Collection
- Object
- Collection
- Ohm::Model::Set
- Defined in:
- lib/ohm.rb
Overview
Provides a Ruby-esque interface to a Redis SET. The SET is assumed to be composed of ids which maps to Collection#model.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Collection
Instance Method Summary (collapse)
-
- (Object) <<(model)
(also: #add)
Adds a model to this set.
-
- (Ohm::Model?) [](id)
Convenient way to scope access to a predefined set, useful for access control.
-
- (Array<Ohm::Model>) all
Array representation of this set.
-
- (Object) delete(member)
Thin Ruby interface wrapper for SREM.
-
- (Object) each(&block)
An implementation which relies on SMEMBERS and yields an instance of Collection#model.
-
- (Ohm::Model::Set) except(options)
Similar to find except that it negates the criteria.
-
- (Ohm::Model::Set) find(options)
Allows you to find members of this set which fits the given criteria.
-
- (Ohm::Model?) first(options = {})
Returns by default the lowest id value for this set.
-
- (true, false) include?(model)
Ruby-like interface wrapper around SISMEMBER.
- - (Object) inspect
-
- (Fixnum) size
Thin Ruby interface wrapper for SCARD.
Methods inherited from Collection
#clear, #empty?, #initialize, #replace, #sort, #sort_by, #to_a
Constructor Details
This class inherits a constructor from Ohm::Model::Collection
Instance Method Details
- (Object) <<(model) Also known as: add
Adds a model to this set.
512 513 514 |
# File 'lib/ohm.rb', line 512 def <<(model) key.sadd(model.id) end |
- (Ohm::Model?) [](id)
Convenient way to scope access to a predefined set, useful for access control.
502 503 504 |
# File 'lib/ohm.rb', line 502 def [](id) model[id] if key.sismember(id) end |
- (Array<Ohm::Model>) all
Array representation of this set.
560 561 562 |
# File 'lib/ohm.rb', line 560 def all key.smembers.map(&model) end |
- (Object) delete(member)
Thin Ruby interface wrapper for SREM.
531 532 533 |
# File 'lib/ohm.rb', line 531 def delete(member) key.srem(member.id) end |
- (Object) each(&block)
An implementation which relies on SMEMBERS and yields an instance of Collection#model.
472 473 474 |
# File 'lib/ohm.rb', line 472 def each(&block) key.smembers.each { |id| block.call(model.to_proc[id]) } end |
- (Ohm::Model::Set) except(options)
Similar to find except that it negates the criteria.
625 626 627 628 629 |
# File 'lib/ohm.rb', line 625 def except() source = keys() target = source.inject(key.volatile) { |chain, other| chain - other } apply(:sdiffstore, key, source, target) end |
- (Ohm::Model::Set) find(options)
Allows you to find members of this set which fits the given criteria.
601 602 603 604 605 |
# File 'lib/ohm.rb', line 601 def find() source = keys() target = source.inject(key.volatile) { |chain, other| chain + other } apply(:sinterstore, key, source, target) end |
- (Ohm::Model?) first(options = {})
Returns by default the lowest id value for this set. You may also pass in options similar to Collection#sort.
668 669 670 671 672 673 674 675 676 677 |
# File 'lib/ohm.rb', line 668 def first( = {}) opts = .dup opts.merge!(:limit => 1) if opts[:by] sort_by(opts.delete(:by), opts).first else sort(opts).first end end |
- (true, false) include?(model)
Ruby-like interface wrapper around SISMEMBER.
688 689 690 |
# File 'lib/ohm.rb', line 688 def include?(model) key.sismember(model.id) end |
- (Object) inspect
692 693 694 |
# File 'lib/ohm.rb', line 692 def inspect "#<Set (#{model}): #{key.smembers.inspect}>" end |
- (Fixnum) size
Thin Ruby interface wrapper for SCARD.
522 523 524 |
# File 'lib/ohm.rb', line 522 def size key.scard end |