Class: Relix::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/relix/index.rb

Direct Known Subclasses

MultiIndex, OrderedIndex, PrimaryKeyIndex, UniqueIndex

Defined Under Namespace

Modules: Ordering Classes: Accessor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(set, base_name, accessors, options = {}) ⇒ Index

Returns a new instance of Index.



31
32
33
34
35
36
37
38
# File 'lib/relix/index.rb', line 31

def initialize(set, base_name, accessors, options={})
  @set = set
  @base_name = base_name
  @model_name = @set.klass.name
  @accessors = Array(accessors).collect{|a| Accessor.new(a)}
  @attribute_immutable = options[:immutable_attribute]
  @options = options
end

Instance Attribute Details

#model_nameObject (readonly)

Returns the value of attribute model_name.



30
31
32
# File 'lib/relix/index.rb', line 30

def model_name
  @model_name
end

Class Method Details

.compact_kindObject



7
8
9
# File 'lib/relix/index.rb', line 7

def self.compact_kind
  @compact_kind ||= kind[0..0]
end

.kindObject



3
4
5
# File 'lib/relix/index.rb', line 3

def self.kind
  @kind ||= name.gsub(/(?:^.+::|Index$)/, '').gsub(/([a-z])([A-Z])/){"#{$1}_#{$2}"}.downcase
end

Instance Method Details

#attribute_immutable?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/relix/index.rb', line 88

def attribute_immutable?
  @attribute_immutable
end

#create_query_clause(redis) ⇒ Object



84
85
86
# File 'lib/relix/index.rb', line 84

def create_query_clause(redis)
  Query::Clause.new(redis, self)
end

#filter(r, object, value) ⇒ Object



76
77
78
# File 'lib/relix/index.rb', line 76

def filter(r, object, value)
  true
end

#nameObject



40
41
42
# File 'lib/relix/index.rb', line 40

def name
  @set.keyer.index(self, @base_name)
end

#normalize(value) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/relix/index.rb', line 52

def normalize(value)
  value_hash = case value
  when Hash
    value.inject({}){|h, (k,v)| h[k.to_s] = v; h}
  else
    {@accessors.first.identifier => value}
  end
  @accessors.collect do |accessor|
    if value_hash.include?(accessor.identifier)
      value_hash[accessor.identifier].to_s
    else
      raise MissingIndexValueError, "Missing #{accessor.identifier} when looking up by #{name}"
    end
  end.join(":")
end

#query(r, value) ⇒ Object



80
81
82
# File 'lib/relix/index.rb', line 80

def query(r, value)
  nil
end

#read(object) ⇒ Object



44
45
46
# File 'lib/relix/index.rb', line 44

def read(object)
  @accessors.inject({}){|h,e| h[e.identifier] = e.read(object); h}
end

#read_normalized(object) ⇒ Object



48
49
50
# File 'lib/relix/index.rb', line 48

def read_normalized(object)
  normalize(read(object))
end

#watch(*values) ⇒ Object



68
69
70
# File 'lib/relix/index.rb', line 68

def watch(*values)
  watch_keys(*values) unless attribute_immutable?
end

#watch_keys(*values) ⇒ Object



72
73
74
# File 'lib/relix/index.rb', line 72

def watch_keys(*values)
  nil
end