Class: Blog::Taxonomy
- Inherits:
-
Object
- Object
- Blog::Taxonomy
- Includes:
- MongoModel::MapReduce
- Defined in:
- app/models/blog/taxonomy.rb
Instance Attribute Summary collapse
-
#blog ⇒ Object
Returns the value of attribute blog.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .alphabetical ⇒ Object
- .for_blog(blog) ⇒ Object
- .from_mongo(attrs) ⇒ Object
- .map_function ⇒ Object
- .new(type) ⇒ Object
- .reduce_function ⇒ Object
- .refresh ⇒ Object
- .subclass_new ⇒ Object
- .top(n = nil) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(name, count) ⇒ Taxonomy
constructor
A new instance of Taxonomy.
- #inspect ⇒ Object
- #parameterize ⇒ Object
- #path ⇒ Object
- #to_s ⇒ Object
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
#blog ⇒ Object
Returns the value of attribute blog.
23 24 25 |
# File 'app/models/blog/taxonomy.rb', line 23 def blog @blog end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
22 23 24 |
# File 'app/models/blog/taxonomy.rb', line 22 def count @count end |
#name ⇒ Object (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
.alphabetical ⇒ Object
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_function ⇒ Object
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_function ⇒ Object
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" |
.refresh ⇒ Object
68 69 70 |
# File 'app/models/blog/taxonomy.rb', line 68 def self.refresh scoped.to_a end |
.subclass_new ⇒ Object
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 |
#hash ⇒ Object
42 43 44 |
# File 'app/models/blog/taxonomy.rb', line 42 def hash name.hash end |
#inspect ⇒ Object
46 47 48 |
# File 'app/models/blog/taxonomy.rb', line 46 def inspect "#<#{self.class.name} \"#{name}\">" end |
#parameterize ⇒ Object
38 39 40 |
# File 'app/models/blog/taxonomy.rb', line 38 def parameterize name.parameterize end |
#path ⇒ Object
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_s ⇒ Object
34 35 36 |
# File 'app/models/blog/taxonomy.rb', line 34 def to_s name end |