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
39
# 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]
  @conditional = options[:if]
  @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)


93
94
95
# File 'lib/relix/index.rb', line 93

def attribute_immutable?
  @attribute_immutable
end

#create_query_clause(redis) ⇒ Object



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

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

#filter(r, object, value) ⇒ Object



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

def filter(r, object, value)
  true
end

#index?(r, object, value) ⇒ Boolean

Returns:

  • (Boolean)


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

def index?(r, object, value)
  (@conditional ? object.send(@conditional) : true)
end

#nameObject



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

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

#normalize(value) ⇒ Object



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

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



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

def query(r, value)
  nil
end

#read(object) ⇒ Object



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

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

#read_normalized(object) ⇒ Object



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

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

#watch(*values) ⇒ Object



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

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

#watch_keys(*values) ⇒ Object



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

def watch_keys(*values)
  nil
end