Class: IParty::MaxMind::Result

Inherits:
Hash
  • Object
show all
Defined in:
lib/iparty/max_mind/result.rb

Direct Known Subclasses

Asn, Geo, Location, NamedLocation, Postal, Traits

Defined Under Namespace

Classes: Asn, City, Continent, Country, Geo, GeoCity, GeoCountry, Location, NamedLocation, Postal, Subdivision, Subdivisions, Traits

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Result




185
186
187
188
# File 'lib/iparty/max_mind/result.rb', line 185

def initialize(data = {})
  super()
  merge!(data) if data
end

Class Method Details

.define_attr(name, attribute = name, type: nil, aliases: nil, memoize: nil, export: nil, &transform) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/iparty/max_mind/result.rb', line 7

def define_attr(name, attribute = name, type: nil, aliases: nil, memoize: nil, export: nil, &transform)
  ivar = :"@#{attribute}"
  transform ||= ->(v) { type.new(v) } if type
  memoize = true if memoize.nil? && transform
  method_names = [name] + Array(aliases)

  method_names.each do |meth|
    define_method(meth) do
      value = dig(attribute)
      return transform ? instance_exec(value, &transform) : value unless memoize
      return instance_variable_get(ivar) if instance_variable_defined?(ivar)

      instance_variable_set(ivar, instance_exec(value, &transform))
    end
  end

  Array(export).each{|exp| export_attr(name, exp == true ? name : exp) }

  method_names
end

.export_attr(name, export) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/iparty/max_mind/result.rb', line 28

def export_attr name, export
  if self == Result::Asn
    IParty::Address.def_delegator(:asn, name, export)
  elsif self == Result::Geo || self == Result
    IParty::Address.def_delegator(:geo, name, export)
  else
    raise ArgumentError, "you can only export on Result, Geo(*) and Asn (got #{self})"
  end
end

Instance Method Details

#inspectObject



190
191
192
# File 'lib/iparty/max_mind/result.rb', line 190

def inspect
  "#<#{self.class}:#{format("0x%x", object_id << 1)}: #{super}>"
end

#presenceObject



200
201
202
# File 'lib/iparty/max_mind/result.rb', line 200

def presence
  self if present?
end

#present?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/iparty/max_mind/result.rb', line 196

def present?
  !empty?
end