Class: Rubyhexagon::Tag
- Inherits:
-
Object
- Object
- Rubyhexagon::Tag
- Defined in:
- lib/rubyhexagon/tag.rb
Overview
Class to hold tag information.
Instance Attribute Summary collapse
-
#count ⇒ Integer
readonly
Number of posts that have this tag assigned to them.
-
#id ⇒ Integer
readonly
Id of tag.
-
#name ⇒ String
readonly
Name of tag.
-
#type ⇒ Type
readonly
Type of this tag.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
Comparison operator for Tag, to override the default one.
-
#initialize(id) ⇒ Object
constructor
Initializer for Tag.
-
#show(tag = nil) ⇒ Tag
Method to return a properly filled Tag object.
-
#show!(tag = nil) ⇒ NilClass
Bang method to fill tag data.
Constructor Details
#initialize(id) ⇒ Object
Initializer for Tag.
46 47 48 49 50 51 |
# File 'lib/rubyhexagon/tag.rb', line 46 def initialize(id) unless id.is_a?(Integer) && id.positive? raise InvalidIDError, "ID out of range: #{id}" end @id = id end |
Instance Attribute Details
#count ⇒ Integer (readonly)
Returns number of posts that have this tag assigned to them.
32 33 34 |
# File 'lib/rubyhexagon/tag.rb', line 32 def count @count end |
#id ⇒ Integer (readonly)
Returns id of tag.
26 27 28 |
# File 'lib/rubyhexagon/tag.rb', line 26 def id @id end |
#name ⇒ String (readonly)
Returns name of tag.
29 30 31 |
# File 'lib/rubyhexagon/tag.rb', line 29 def name @name end |
#type ⇒ Type (readonly)
Returns type of this tag.
35 36 37 |
# File 'lib/rubyhexagon/tag.rb', line 35 def type @type end |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass
Note:
This method looks for same id and same name only.
Note:
If there is only a an ID set with one argument, then only compare IDs
Comparison operator for Tag, to override the default one.
94 95 96 97 98 99 100 101 |
# File 'lib/rubyhexagon/tag.rb', line 94 def ==(other) return false unless other.is_a?(Tag) if @name.nil? || other.name.nil? @id == other.id else @id == other.id && @name == other.name end end |
#show(tag = nil) ⇒ Tag
Note:
This makes an additional API call if no argument is given
Method to return a properly filled Tag object.
78 79 80 81 82 |
# File 'lib/rubyhexagon/tag.rb', line 78 def show(tag = nil) new_tag = Tag.new(@id) new_tag.show!(tag) new_tag end |
#show!(tag = nil) ⇒ NilClass
Bang method to fill tag data.
62 63 64 65 66 67 68 |
# File 'lib/rubyhexagon/tag.rb', line 62 def show!(tag = nil) tag = API.new.fetch('tag', 'show', id: @id) if tag.nil? @name = tag[:name] @count = tag[:count].to_i @type = (Type.new(tag[:type], tag[:type_locked]) unless tag[:type].nil?) nil end |