Class: WrAPI::Request::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/wrapi/entity.rb

Overview

Entity class to represent and manipulate API data

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr) ⇒ Entity

Initializes a new Entity

Parameters:

  • attr (Hash)

    the attributes to initialize the entity with



27
28
29
30
31
32
33
34
# File 'lib/wrapi/entity.rb', line 27

def initialize(attr)
  case attr
  when Hash
    @attributes = attr.clone.transform_keys(&:to_s)
  else
    @attributes = attr.clone
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object

Handles dynamic method calls for attribute access and assignment

Parameters:

  • method_sym (Symbol)

    the method name

  • arguments (Array)

    the arguments passed to the method

  • block (Proc)

    an optional block



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/wrapi/entity.rb', line 55

def method_missing(method_sym, *arguments, &block)
  # assignment
  method = method_sym.to_s
  assignment = method_sym[/.*(?==\z)/m]
  if assignment
    raise ArgumentError, "wrong number of arguments (given #{arguments.length}, expected 1)", caller(1) unless arguments.length == 1

    @attributes[assignment] = arguments[0]
  elsif @attributes.include? method
    accessor(method)
  else
    # delegate to hash
    @attributes.send(method_sym, *arguments, &block)
  end
end

Class Method Details

.create(attr) ⇒ Entity+

Factory method to create an entity or array of entities

Parameters:

  • attr (Hash, Array<Hash>)

    the attributes to create the entity/entities from

Returns:

  • (Entity, Array<Entity>)

    the created entity or array of entities



16
17
18
19
20
21
22
# File 'lib/wrapi/entity.rb', line 16

def self.create(attr)
  if attr.is_a? Array
    Entity.entify(attr)
  elsif attr
    Entity.new(attr)
  end
end

.entify(attribute) ⇒ Array<Entity>

Converts an array of hashes to an array of entities

Parameters:

  • attribute (Array<Hash>)

    the array of hashes

Returns:

  • (Array<Entity>)

    the array of entities



132
133
134
135
136
137
138
139
140
141
# File 'lib/wrapi/entity.rb', line 132

def self.entify(attribute)
  if attribute.any? && (attribute.first.is_a? Hash)
    attribute.dup.map do |item|
      #item.is_a?(Hash) ? self.class.new(item) : item
      Entity.create(item)
    end
  else
    attribute
  end
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Checks if two entities are equal

Parameters:

  • other (Entity)

    the other entity to compare with

Returns:

  • (Boolean)

    true if the entities are equal, false otherwise



123
124
125
# File 'lib/wrapi/entity.rb', line 123

def ==(other)
  (self.class == other.class) && (self.attributes.eql? other.attributes)
end

#accessor(method) ⇒ Object

Accesses an attribute, converting it to an Entity if it is a Hash or Array

Parameters:

  • method (String)

    the attribute name

Returns:

  • (Object)

    the attribute value



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wrapi/entity.rb', line 97

def accessor(method)
  attribute = @attributes[method]
  case attribute
  when Hash
    @attributes[method] = self.class.new(attribute)
  when Array
    # make deep copy
    @attributes[method] = Entity.entify(attribute)
  else
    attribute
  end
end

#attributesHash

Returns the attributes of the entity

Returns:

  • (Hash)

    the attributes of the entity



39
40
41
# File 'lib/wrapi/entity.rb', line 39

def attributes
  @attributes || {}
end

#attributes=(val) ⇒ Object

Sets the attributes of the entity

Parameters:

  • val (Hash)

    the new attributes of the entity



46
47
48
# File 'lib/wrapi/entity.rb', line 46

def attributes=(val)
  @attributes = val || {}
end

#cloneEntity

Clones the entity

Returns:

  • (Entity)

    the cloned entity



113
114
115
116
117
# File 'lib/wrapi/entity.rb', line 113

def clone
  cln = super
  cln.attributes = @attributes.clone
  cln
end

#respond_to?(method_sym, include_private = false) ⇒ Boolean

Checks if the entity responds to a method

Parameters:

  • method_sym (Symbol)

    the method name

  • include_private (Boolean) (defaults to: false)

    whether to include private methods

Returns:

  • (Boolean)

    true if the entity responds to the method, false otherwise



76
77
78
79
80
81
82
83
# File 'lib/wrapi/entity.rb', line 76

def respond_to?(method_sym, include_private = false)
  @attributes ||= {}
  if @attributes.include? method_sym.to_s
    true
  else
    @attributes.respond_to?(method_sym, include_private)
  end
end

#to_json(_options = {}) ⇒ String

Converts the entity to a JSON string

Parameters:

  • options (Hash)

    options for JSON generation

Returns:

  • (String)

    the JSON representation of the entity



89
90
91
# File 'lib/wrapi/entity.rb', line 89

def to_json(_options = {})
  @attributes.to_json
end