Class: Blog::Taxonomy

Inherits:
Object
  • Object
show all
Includes:
MongoModel::MapReduce
Defined in:
app/models/blog/taxonomy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, count) ⇒ Taxonomy



25
26
27
# File 'app/models/blog/taxonomy.rb', line 25

def initialize(name, count)
  @name, @count = name, count.to_i
end

Instance Attribute Details

#blogObject

Returns the value of attribute blog.



23
24
25
# File 'app/models/blog/taxonomy.rb', line 23

def blog
  @blog
end

#countObject (readonly)

Returns the value of attribute count.



22
23
24
# File 'app/models/blog/taxonomy.rb', line 22

def count
  @count
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'app/models/blog/taxonomy.rb', line 22

def name
  @name
end

Class Method Details

.alphabeticalObject



59
60
61
# File 'app/models/blog/taxonomy.rb', line 59

def self.alphabetical
  order(:"_id.name".asc)
end

.for_blog(blog) ⇒ Object



72
73
74
75
76
# File 'app/models/blog/taxonomy.rb', line 72

def self.for_blog(blog)
  where('_id.blog_id' => blog.id).on_load do |taxonomy|
    taxonomy.blog = blog
  end
end

.from_mongo(attrs) ⇒ Object



78
79
80
# File 'app/models/blog/taxonomy.rb', line 78

def self.from_mongo(attrs)
  new(attrs['_id']['name'], attrs['value'])
end

.map_functionObject



82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/blog/taxonomy.rb', line 82

def self.map_function
  "  function() {\n    if (!this.\#{taxonomy_type}) { return; }\n    \n    for (i in this.\#{taxonomy_type}) {\n      emit({ blog_id: this.blog_id, name: this.\#{taxonomy_type}[i] }, 1);\n    }\n  }\n  MAP\nend\n"

.new(type) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'app/models/blog/taxonomy.rb', line 10

def self.new(type)
  Class.new(self) do
    self.taxonomy_type = type
    
    def self.new(*args, &block)
      subclass_new(*args, &block)
    end
  end
end

.reduce_functionObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/blog/taxonomy.rb', line 94

def self.reduce_function
  "  function(key, values) {\n    var count = 0;\n    \n    for (i in values) {\n      count += values[i];\n    }\n    \n    return count;\n  }\n  REDUCE\nend\n"

.refreshObject



68
69
70
# File 'app/models/blog/taxonomy.rb', line 68

def self.refresh
  scoped.to_a
end

.subclass_newObject



7
# File 'app/models/blog/taxonomy.rb', line 7

alias_method :subclass_new, :new

.top(n = nil) ⇒ Object



63
64
65
66
# File 'app/models/blog/taxonomy.rb', line 63

def self.top(n=nil)
  ordered = order(:value.desc)
  n ? ordered.first(n) : ordered
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



50
51
52
53
54
55
56
# File 'app/models/blog/taxonomy.rb', line 50

def ==(other)
  if other === self.class
    self == other
  else
    name == other
  end
end

#hashObject



42
43
44
# File 'app/models/blog/taxonomy.rb', line 42

def hash
  name.hash
end

#inspectObject



46
47
48
# File 'app/models/blog/taxonomy.rb', line 46

def inspect
  "#<#{self.class.name} \"#{name}\">"
end

#parameterizeObject



38
39
40
# File 'app/models/blog/taxonomy.rb', line 38

def parameterize
  name.parameterize
end

#pathObject



29
30
31
32
# File 'app/models/blog/taxonomy.rb', line 29

def path
  raise "#path requires a blog to be set" unless blog
  blog.send(path_method, name)
end

#to_sObject



34
35
36
# File 'app/models/blog/taxonomy.rb', line 34

def to_s
  name
end