Class: Pobo::DTO::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/pobo/dto/product.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, is_visible:, name:, url:, short_description: nil, description: nil, seo_title: nil, seo_description: nil, content: nil, images: [], categories_ids: [], parameters_ids: [], guid: nil, is_loaded: nil, categories: [], created_at: nil, updated_at: nil) ⇒ Product

Returns a new instance of Product.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pobo/dto/product.rb', line 10

def initialize(
  id:,
  is_visible:,
  name:,
  url:,
  short_description: nil,
  description: nil,
  seo_title: nil,
  seo_description: nil,
  content: nil,
  images: [],
  categories_ids: [],
  parameters_ids: [],
  guid: nil,
  is_loaded: nil,
  categories: [],
  created_at: nil,
  updated_at: nil
)
  @id = id
  @is_visible = is_visible
  @name = name
  @url = url
  @short_description = short_description
  @description = description
  @seo_title = seo_title
  @seo_description = seo_description
  @content = content
  @images = images
  @categories_ids = categories_ids
  @parameters_ids = parameters_ids
  @guid = guid
  @is_loaded = is_loaded
  @categories = categories
  @created_at = created_at
  @updated_at = updated_at
end

Instance Attribute Details

#categories ⇒ Object (readonly)

Returns the value of attribute categories.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def categories
  @categories
end

#categories_ids ⇒ Object (readonly)

Returns the value of attribute categories_ids.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def categories_ids
  @categories_ids
end

#content ⇒ Object (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def content
  @content
end

#created_at ⇒ Object (readonly)

Returns the value of attribute created_at.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def created_at
  @created_at
end

#description ⇒ Object (readonly)

Returns the value of attribute description.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def description
  @description
end

#guid ⇒ Object (readonly)

Returns the value of attribute guid.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def guid
  @guid
end

#id ⇒ Object (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def id
  @id
end

#images ⇒ Object (readonly)

Returns the value of attribute images.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def images
  @images
end

#is_loaded ⇒ Object (readonly)

Returns the value of attribute is_loaded.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def is_loaded
  @is_loaded
end

#is_visible ⇒ Object (readonly)

Returns the value of attribute is_visible.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def is_visible
  @is_visible
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def name
  @name
end

#parameters_ids ⇒ Object (readonly)

Returns the value of attribute parameters_ids.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def parameters_ids
  @parameters_ids
end

#seo_description ⇒ Object (readonly)

Returns the value of attribute seo_description.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def seo_description
  @seo_description
end

#seo_title ⇒ Object (readonly)

Returns the value of attribute seo_title.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def seo_title
  @seo_title
end

#short_description ⇒ Object (readonly)

Returns the value of attribute short_description.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def short_description
  @short_description
end

#updated_at ⇒ Object (readonly)

Returns the value of attribute updated_at.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def updated_at
  @updated_at
end

#url ⇒ Object (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/pobo/dto/product.rb', line 6

def url
  @url
end

Class Method Details

.from_hash(hash) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pobo/dto/product.rb', line 48

def self.from_hash(hash)
  new(
    id: hash["id"] || hash[:id],
    is_visible: hash.key?("is_visible") ? hash["is_visible"] : hash[:is_visible],
    name: LocalizedString.from_hash(hash["name"] || hash[:name]),
    url: LocalizedString.from_hash(hash["url"] || hash[:url]),
    short_description: LocalizedString.from_hash(hash["short_description"] || hash[:short_description]),
    description: LocalizedString.from_hash(hash["description"] || hash[:description]),
    seo_title: LocalizedString.from_hash(hash["seo_title"] || hash[:seo_title]),
    seo_description: LocalizedString.from_hash(hash["seo_description"] || hash[:seo_description]),
    content: Content.from_hash(hash["content"] || hash[:content]),
    images: hash["images"] || hash[:images] || [],
    categories_ids: hash["categories_ids"] || hash[:categories_ids] || [],
    parameters_ids: hash["parameters_ids"] || hash[:parameters_ids] || [],
    guid: hash["guid"] || hash[:guid],
    is_loaded: hash.key?("is_loaded") ? hash["is_loaded"] : hash[:is_loaded],
    categories: hash["categories"] || hash[:categories] || [],
    created_at: parse_time(hash["created_at"] || hash[:created_at]),
    updated_at: parse_time(hash["updated_at"] || hash[:updated_at])
  )
end

.parse_time(value) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/pobo/dto/product.rb', line 93

def self.parse_time(value)
  return nil if value.nil?
  return value if value.is_a?(Time)

  Time.parse(value)
rescue ArgumentError
  nil
end

Instance Method Details

#to_hash ⇒ Object Also known as: to_h



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pobo/dto/product.rb', line 70

def to_hash
  data = {
    "id" => @id,
    "is_visible" => @is_visible,
    "name" => @name&.to_hash,
    "url" => @url&.to_hash
  }

  data["short_description"] = @short_description.to_hash if @short_description
  data["description"] = @description.to_hash if @description
  data["seo_title"] = @seo_title.to_hash if @seo_title
  data["seo_description"] = @seo_description.to_hash if @seo_description
  data["images"] = @images unless @images.empty?
  data["categories_ids"] = @categories_ids unless @categories_ids.empty?
  data["parameters_ids"] = @parameters_ids unless @parameters_ids.empty?

  data
end