Class: HumanName::Name

Inherits:
FFI::AutoPointer
  • Object
show all
Defined in:
lib/humanname.rb

Constant Summary collapse

DESTRUCTOR =
Native.method(:human_name_free_name)
JSON_PARTS =
%w( surname given_name first_initial middle_initials middle_names generational_suffix ).map(&:to_sym)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer) ⇒ Name

Returns a new instance of Name.



103
104
105
# File 'lib/humanname.rb', line 103

def initialize(pointer)
  super(pointer, DESTRUCTOR)
end

Class Method Details

.parse(string) ⇒ Object



97
98
99
100
101
# File 'lib/humanname.rb', line 97

def self.parse(string)
  string = string.encode(UTF8) unless string.encoding == UTF8
  pointer = Native.human_name_parse(string)
  new(pointer) unless pointer.null?
end

Instance Method Details

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



107
108
109
# File 'lib/humanname.rb', line 107

def ==(other)
  other.is_a?(Name) && Native.human_name_consistent_with(self, other)
end

#as_json(options = {}) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/humanname.rb', line 135

def as_json(options = {})
  # We take an "options" argument for minimal compatibility with ActiveSupport,
  # but don't actually respect it
  [:root, :only, :except, :include].each do |opt|
    raise ArgumentError.new("Unsupported option: #{opt}") if options[opt]
  end

  to_h
end

#goes_by_middle_nameObject



125
126
127
# File 'lib/humanname.rb', line 125

def goes_by_middle_name
  Native.human_name_goes_by_middle_name(self)
end

#hashObject



112
113
114
# File 'lib/humanname.rb', line 112

def hash
  Native.human_name_hash(self)
end

#inspectObject



152
153
154
# File 'lib/humanname.rb', line 152

def inspect
  "HumanName::Name(#{display_full})"
end

#lengthObject



129
130
131
# File 'lib/humanname.rb', line 129

def length
  Native.human_name_byte_len(self)
end

#to_hObject



145
146
147
148
149
150
# File 'lib/humanname.rb', line 145

def to_h
  JSON_PARTS.inject({}) do |memo, part|
    memo[part] = send(part)
    memo
  end
end