Class: Spree::Address::Name

Inherits:
Object
  • Object
show all
Defined in:
core/app/models/spree/address/name.rb

Overview

Provides a value object to help transitioning from legacy firstname and lastname fields to a unified name field.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*components) ⇒ Name

Returns a new instance of Name.



28
29
30
31
# File 'core/app/models/spree/address/name.rb', line 28

def initialize(*components)
  @value = components.join(' ').strip
  initialize_name_components(components)
end

Instance Attribute Details

#first_nameObject (readonly)

Returns the value of attribute first_name.



8
9
10
# File 'core/app/models/spree/address/name.rb', line 8

def first_name
  @first_name
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



8
9
10
# File 'core/app/models/spree/address/name.rb', line 8

def last_name
  @last_name
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'core/app/models/spree/address/name.rb', line 8

def value
  @value
end

Class Method Details

.from_attributes(attributes) ⇒ Spree::Address::Name

Creates an instance of Spree::Address::Name parsing input attributes.

Parameters:

  • attributes (Hash)

    an hash possibly containing name-related attributes (name, firstname, lastname, first_name, last_name)

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'core/app/models/spree/address/name.rb', line 14

def self.from_attributes(attributes)
  params = attributes.with_indifferent_access

  if params[:name].present?
    Spree::Address::Name.new(params[:name])
  elsif params[:firstname].present?
    Spree::Address::Name.new(params[:firstname], params[:lastname])
  elsif params[:first_name].present?
    Spree::Address::Name.new(params[:first_name], params[:last_name])
  else
    Spree::Address::Name.new
  end
end

Instance Method Details

#to_sObject



33
34
35
# File 'core/app/models/spree/address/name.rb', line 33

def to_s
  @value
end