Class: Rubyhexagon::Tag::Implication

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhexagon/tag/implication.rb,
lib/rubyhexagon/api/tag/implication.rb

Overview

A class to interact with the e621 web interface.

Author:

  • Maxine Michalski

Since:

  • 2.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(imply) ⇒ Object

Initializer for Implication

Examples:

Get new implication instance

E621::Tag::Implication.new(id: 1, consequent_id: 1, predicate_id: 2,
pending: false) #=> E621::Tag::Implication

Parameters:

  • imply (Hash)

    implication data, fetched from e621

Options Hash (imply):

  • :id (Integer)

    implication ID

  • :consequent_id (Integer)

    implying tag ID

  • :predicate_id (Integer)

    implied tag ID

  • :pending (TrueClass|FalseClass)

    statud

Raises:

  • InvalidIDError if implication ID is not valid

  • ArgumentError hash not valid

Author:

  • Maxine Michalski

Since:

  • 2.0.0



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rubyhexagon/tag/implication.rb', line 61

def initialize(imply)
  unless imply.is_a?(Hash)
    raise ArgumentError, "#{imply.class} is not a Hash"
  end
  if imply.keys != %i[id consequent_id predicate_id pending]
    mis = %i[id consequent_id predicate_id pending] - imply.keys
    raise ArgumentError, "Missing key#{mis.count > 1 ? 's' : ''}: #{mis}"
  end
  imply.each do |k, v|
    if %i[id pending].include?(k)
      if k == :id && !(v.is_a?(Integer) && v.positive?)
        raise InvalidIDError, "Invalid id: #{v}"
      end
      instance_variable_set("@#{k}".to_sym, v)
    elsif k == :consequent_id
      @tag = E621::Tag.new(id: v)
    elsif k == :predicate_id
      @implies = E621::Tag.new(id: v)
    end
  end
end

Instance Attribute Details

#idInteger (readonly)

Implication ID

Examples:

Get implication ID

implication.id #=> Integer

Returns:

  • (Integer)

    id of implication

Since:

  • 2.0.0



31
32
33
# File 'lib/rubyhexagon/tag/implication.rb', line 31

def id
  @id
end

#impliesE621::Tag (readonly)

Tag that is implied

Examples:

Get implied tag

implication.implied #=> E621::Tag

Returns:

Since:

  • 2.0.0



43
44
45
# File 'lib/rubyhexagon/tag/implication.rb', line 43

def implies
  @implies
end

#tagE621::Tag (readonly)

Tag that implies another tag

Examples:

Get implying tag

implication.tag #=> E621::Tag

Returns:

Since:

  • 2.0.0



37
38
39
# File 'lib/rubyhexagon/tag/implication.rb', line 37

def tag
  @tag
end

Class Method Details

.list(query) ⇒ Array[E621::Tag::Implication]

Fetch tag implications

Examples:

Get a list of posts

E621::Tag::Implication.list(implied_to: 'feline',
page: 1) #=> Array<E621::Tag::Implication>

Parameters:

  • query (Hash)

    Parameter for query

Returns:

  • (Array[E621::Tag::Implication])

Raises:

  • (ArgumentError)

See Also:

Author:

  • Maxine Michalski

Since:

  • 2.0.0



38
39
40
41
42
43
# File 'lib/rubyhexagon/api/tag/implication.rb', line 38

def self.list(query)
  raise ArgumentError, 'A Hash is required' unless query.is_a?(Hash)
  E621::API.fetch(:tag_implication, :index, query).map do |tag|
    new(tag)
  end
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Comparison method for Types, to give a more meaningful comparison

Examples:

Compare two tags, that are unequal

Tag.new(id: 1) == Tag.new(id: 2) #=> false

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 2.0.0



89
90
91
# File 'lib/rubyhexagon/tag/implication.rb', line 89

def ==(other)
  other.is_a?(Implication) && @id == other.id
end

#pending?TrueClass, FalseClass

Check if this implication is pending confirmation

Examples:

Is implication pending?

implication.pending? #=> true or false

Returns:

  • (TrueClass)

    tag implication is pending

  • (FalseClass)

    tag implication is not pending

Author:

  • Maxine Michalski

Since:

  • 2.0.0



101
102
103
# File 'lib/rubyhexagon/tag/implication.rb', line 101

def pending?
  @pending
end

#to_hashHash

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:

  • 2.0.0



112
113
114
115
116
117
118
# File 'lib/rubyhexagon/tag/implication.rb', line 112

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