Class: Mongoid::Name

Inherits:
String
  • Object
show all
Defined in:
lib/mongoid-name.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Name

Name.new(‘AJ’, ‘Ostrow’) Name.new(‘Alexander’, ‘Jacob’, ‘Ostrow’) Name.new(%w(AJ Ostrow)) Name.new(%w(Alexander Jacob Ostrow))



9
10
11
12
13
14
# File 'lib/mongoid-name.rb', line 9

def initialize(*args)
  args.flatten!
  @first, @last = args.shift, args.pop
  @middle = args.join(' ') if args.any?
  super(to_s)
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



3
4
5
# File 'lib/mongoid-name.rb', line 3

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



3
4
5
# File 'lib/mongoid-name.rb', line 3

def last
  @last
end

#middleObject (readonly)

Returns the value of attribute middle.



3
4
5
# File 'lib/mongoid-name.rb', line 3

def middle
  @middle
end

Class Method Details

.demongoize(array) ⇒ Object



34
35
36
# File 'lib/mongoid-name.rb', line 34

def self.demongoize(array)
  new(*array)
end

.evolve(object) ⇒ Object



42
43
44
# File 'lib/mongoid-name.rb', line 42

def self.evolve(object)
  object.respond_to?(:mongoize) ? object.mongoize : object
end

.mongoize(object) ⇒ Object



38
39
40
# File 'lib/mongoid-name.rb', line 38

def self.mongoize(object)
  object.respond_to?(:mongoize) ? object.mongoize : object
end

Instance Method Details

#to_aObject Also known as: mongoize



22
23
24
# File 'lib/mongoid-name.rb', line 22

def to_a
  [ first, middle, last ].compact
end

#to_hObject



16
17
18
19
20
# File 'lib/mongoid-name.rb', line 16

def to_h
  { 'first' => first,
    'middle' => middle,
    'last' => last }
end

#to_sObject



26
27
28
# File 'lib/mongoid-name.rb', line 26

def to_s
  to_a.join(' ')
end