Class: Rubirai::Info Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/rubirai/objects/info.rb

Overview

This class is abstract.

Direct Known Subclasses

GroupConfig, GroupFileSimple, MemberInfo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, bot = nil) ⇒ Info

Returns a new instance of Info.

Parameters:

  • hash (Hash{String => Object})
  • bot (Rubirai::Bot, nil) (defaults to: nil)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubirai/objects/info.rb', line 13

def self.set_fields(*fields, **default_values)
  attr_reader(*fields)

  class_eval do
    define_method(:initialize) do |hash, bot = nil|
      # noinspection RubySuperCallWithoutSuperclassInspection
      super hash, bot
      fields.each do |field|
        value = hash[field.to_s.snake_to_camel(lower: true)] || default_values[field]
        instance_variable_set("@#{field}", value)
      end
    end

    define_method(:to_h) do
      fields.to_h do |field|
        [field.to_s.snake_to_camel(lower: true), instance_variable_get(field)]
      end.compact
    end
  end
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



39
40
41
# File 'lib/rubirai/objects/info.rb', line 39

def bot
  @bot
end

#rawObject (readonly)

Returns the value of attribute raw.



39
40
41
# File 'lib/rubirai/objects/info.rb', line 39

def raw
  @raw
end

Class Method Details

.set_fields(*fields, **default_values) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubirai/objects/info.rb', line 13

def self.set_fields(*fields, **default_values)
  attr_reader(*fields)

  class_eval do
    define_method(:initialize) do |hash, bot = nil|
      # noinspection RubySuperCallWithoutSuperclassInspection
      super hash, bot
      fields.each do |field|
        value = hash[field.to_s.snake_to_camel(lower: true)] || default_values[field]
        instance_variable_set("@#{field}", value)
      end
    end

    define_method(:to_h) do
      fields.to_h do |field|
        [field.to_s.snake_to_camel(lower: true), instance_variable_get(field)]
      end.compact
    end
  end
end

.set_modifiable_fields(*fields) ⇒ Object



34
35
36
37
# File 'lib/rubirai/objects/info.rb', line 34

def self.set_modifiable_fields(*fields)
  set_fields(*fields)
  attr_writer(*fields)
end

Instance Method Details

#to_hHash{String => Object}

Returns:

  • (Hash{String => Object})


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubirai/objects/info.rb', line 13

def self.set_fields(*fields, **default_values)
  attr_reader(*fields)

  class_eval do
    define_method(:initialize) do |hash, bot = nil|
      # noinspection RubySuperCallWithoutSuperclassInspection
      super hash, bot
      fields.each do |field|
        value = hash[field.to_s.snake_to_camel(lower: true)] || default_values[field]
        instance_variable_set("@#{field}", value)
      end
    end

    define_method(:to_h) do
      fields.to_h do |field|
        [field.to_s.snake_to_camel(lower: true), instance_variable_get(field)]
      end.compact
    end
  end
end