Class: Hudu::AssetHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/hudu/asset_helper.rb

Overview

The AssetHelper class contains helper methods for constructing and creating asset data.

Class Method Summary collapse

Class Method Details

.construct_asset(asset) ⇒ Hash

Constructs an asset for updates by extracting key attributes and formatting custom fields.

Examples:

asset = SomeAssetEntity.new(attributes: { id: 1, name: "Asset 1", company_id: 101 }, fields: [field1, field2])
Hudu::AssetHelper.construct_asset(asset)
# => { asset:
#      { id: 1, company_id: 101, asset_layout_id: nil, slug: nil, name: "Asset 1", custom_fields: [...]
#      }
#    }


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hudu/asset_helper.rb', line 18

def self.construct_asset(asset)
  custom_asset = asset.attributes.slice(
    *%w[
      asset_layout_id name
      primary_serial primary_model primary_mail
      primary_manufacturer
    ]
  )
  custom_asset['custom_fields'] = custom_fields(asset.fields)
  custom_asset
end

.create_asset(name, asset_layout_id, fields) ⇒ Hash

Creates a new asset from the given layout and fields.

Examples:

fields = [Field.new(label: "Warranty", value: "2025"), Field.new(label: "Location", value: "NYC")]
Hudu::AssetHelper.create_asset("New Asset", 10, fields)
# => { asset: { name: "New Asset", asset_layout_id: 10, custom_fields: [...] } }


41
42
43
44
45
46
47
48
49
# File 'lib/hudu/asset_helper.rb', line 41

def self.create_asset(name, asset_layout_id, fields)
  {
    asset: {
      name: name,
      asset_layout_id: asset_layout_id,
      custom_fields: custom_fields(fields)
    }
  }
end

.custom_fields(fields) ⇒ Array<Hash>

Formats custom fields into a standardized hash structure.

Examples:

fields = [Field.new(label: "Warranty", value: "2025"), Field.new(label: "Location", value: "NYC")]
Hudu::AssetHelper.custom_fields(fields)
# => [{ "warranty" => "2025", "location" => "NYC" }]


61
62
63
# File 'lib/hudu/asset_helper.rb', line 61

def self.custom_fields(fields)
  fields.map { |field| { field.label.downcase.gsub(' ', '_') => field.value.to_s } }
end