Class: Cosmos::Resource

Inherits:
Object show all
Defined in:
lib/cosmos/resource.rb

Overview

en.wikipedia.org/wiki/Material

Consumable entity, usually associated with a Cosmos entity for extraction or a Manufacturing entity for use.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Resource

Resource initializer

Options Hash (args):

  • :resource,'resource' (Cosmos::Resource)

    resource which to copy attributes from

  • :id'id (String)

    to assign to the resource

  • :material_id (String)

    id of material to assign to resource

  • :entity (String)

    entity to assign to the resource

  • :quantity (Integer)

    amount of resource present in resource_source


48
49
50
51
52
53
54
55
56
57
# File 'lib/cosmos/resource.rb', line 48

def initialize(args = {})
  attr_from_args args, :id     => nil,
                       :material_id => nil,
                       :entity_id => nil,
                       :entity => nil,
                       :quantity => 0

  #resource = args['resource'] || args[:resource]
  #unless resource.nil? ...
end

Instance Attribute Details

#entityObject

Entity which resource is assigned


29
30
31
# File 'lib/cosmos/resource.rb', line 29

def entity
  @entity
end

#entity_idObject

Id of entity which resource is assigned


26
27
28
# File 'lib/cosmos/resource.rb', line 26

def entity_id
  @entity_id
end

#idObject

ID of the resource


14
15
16
# File 'lib/cosmos/resource.rb', line 14

def id
  @id
end

#material_idObject

ID of the material


17
18
19
# File 'lib/cosmos/resource.rb', line 17

def material_id
  @material_id
end

#quantityObject

Quantity of resource assigned to entity


38
39
40
# File 'lib/cosmos/resource.rb', line 38

def quantity
  @quantity
end

Class Method Details

.json_create(o) ⇒ Object

Create new resource from json representation


95
96
97
98
# File 'lib/cosmos/resource.rb', line 95

def self.json_create(o)
  rs = new(o['data'])
  return rs
end

Instance Method Details

#nameObject

Name of resource


23
# File 'lib/cosmos/resource.rb', line 23

def name ; material_id.split('-').last end

#to_json(*a) ⇒ Object

Convert resource to json representation and return it


84
85
86
87
88
89
90
91
92
# File 'lib/cosmos/resource.rb', line 84

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       => {:id       => id,
                     :material_id => material_id,
                     :quantity => quantity,
                     :entity_id => entity_id }
  }.to_json(*a)
end

#to_sObject

Convert resource to string


79
80
81
# File 'lib/cosmos/resource.rb', line 79

def to_s
  "resource-#{@material_id} (#{@quantity} at #{@entity_id})"
end

#typeObject

Type of resource


20
# File 'lib/cosmos/resource.rb', line 20

def type ; material_id.split('-').first end

#valid?Boolean

Return boolean indicating if this resource is valid.

Ensures:

  • id is a valid string

  • material_id is in a valid format

  • entity_id is not nil

  • entity is nil or valid

  • quantitiy is numeric


67
68
69
70
71
72
73
74
75
76
# File 'lib/cosmos/resource.rb', line 67

def valid?
  @id.is_a?(String) && @id != "" &&

  @material_id.is_a?(String) &&
  @material_id =~ /[a-zA-Z0-9]*-[a-zA-Z0-9]*/ &&

  !@entity_id.nil? &&
  (@entity.nil? || @entity.valid?) &&
   @quantity.numeric?
end