Class: Rubyhexagon::Tag::Type Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhexagon/tag/type.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class to hold type information.

Author:

  • Maxine Michalski

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializer for Type

Parameters:

  • type (Hash)

    type data, fetched from e621

  • locked (Hash)

    a customizable set of options

Options Hash (type):

  • :id (Integer)

    Type ID (can be one of 0, 1, 3, 4 or 5

Raises:

  • (ArgumentError)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubyhexagon/tag/type.rb', line 44

def initialize(type)
  unless type.is_a?(Hash)
    raise ArgumentError, "#{type.class} is not a Hash"
  end
  raise ArgumentError, 'Missing :locked key' if type[:locked].nil?
  unless [0, 1, 3, 4, 5].include?(type[:id])
    raise InvalidIDError, "Unkown type id: #{type[:id].inspect}"
  end
  @id     = type[:id]
  @name   = [:general, :artist, nil, :copyright, :character,
             :species][@id]
  @locked = type[:locked] ? true : false
end

Instance Attribute Details

#idInteger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Type ID

Returns:

  • (Integer)

    id of type

Since:

  • 1.0.0



29
30
31
# File 'lib/rubyhexagon/tag/type.rb', line 29

def id
  @id
end

#nameString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Type name

Returns:

  • (String)

    name of type

Since:

  • 1.0.0



33
34
35
# File 'lib/rubyhexagon/tag/type.rb', line 33

def name
  @name
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Comparison method for Types, to give a more meaningful comparison

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



63
64
65
66
# File 'lib/rubyhexagon/tag/type.rb', line 63

def ==(other)
  other.is_a?(Type) && @id == other.id && @name == other.name &&
    @locked == other.locked?
end

#locked?TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if this type is locked for the tag who has it assigned

Returns:

  • (TrueClass)

    tag type is locked

  • (FalseClass)

    tag type is not locked

Author:

  • Maxine Michalski

Since:

  • 1.0.0



74
75
76
# File 'lib/rubyhexagon/tag/type.rb', line 74

def locked?
  @locked
end

#to_hashHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Turn object into a hash representation of itself

Examples:

Turn a User into a Hash

Tag.new(id: 1).to_hash #=> { id: 1 }

Returns:

  • (Hash)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



85
86
87
88
89
90
91
# File 'lib/rubyhexagon/tag/type.rb', line 85

def to_hash
  hash = {}
  instance_variables.each do |i|
    hash.store(i.to_s.sub(/^@/, '').to_sym, instance_variable_get(i))
  end
  hash
end