Class: Economic::Entity::Handle

Inherits:
Object
  • Object
show all
Defined in:
lib/economic/entity/handle.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Handle

Returns a new instance of Handle.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/economic/entity/handle.rb', line 35

def initialize(hash)
  verify_sanity_of_arguments!(hash)
  hash = prepare_hash_argument(hash) unless hash.is_a?(self.class)

  [:code, :name, :vat_code, :number].each do |key|
    instance_variable_set("@#{key}", hash[key]) if hash[key]
  end
  [:id, :id1, :id2, :serial_number].each do |key|
    instance_variable_set("@#{key}", hash[key].to_i) if hash[key]
  end
end

Class Method Details

.build(options) ⇒ Object



3
4
5
6
7
# File 'lib/economic/entity/handle.rb', line 3

def self.build(options)
  return options if options.is_a?(Handle)
  return nil if options.nil?
  new(options)
end

.id_propertiesObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/economic/entity/handle.rb', line 9

def self.id_properties
  {
    :code => "Code",
    :id => "Id",
    :id1 => "Id1",
    :id2 => "Id2",
    :name => "Name",
    :number => "Number",
    :serial_number => "SerialNumber",
    :vat_code => "VatCode"
  }
end

.supported_keysObject



22
23
24
# File 'lib/economic/entity/handle.rb', line 22

def self.supported_keys
  id_properties.keys
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/economic/entity/handle.rb', line 61

def ==(other)
  return true if object_id == other.object_id
  return false if other.nil?
  return false if empty? || (other.respond_to?(:empty?) && other.empty?)
  return false unless other.respond_to?(:id) && other.respond_to?(:number)
  id == other.id &&
    number == other.number &&
    id1 == other.id1 &&
    id2 == other.id2 &&
    name == other.name
end

#[](key) ⇒ Object



57
58
59
# File 'lib/economic/entity/handle.rb', line 57

def [](key)
  instance_variable_get("@#{key}")
end

#empty?Boolean

Returns true if Handle hasn’t been initialized with any values yet. This usually happens when the handle is constructed for an entity whose id properties (id, number, etc) haven’t been set yet.

Returns:

  • (Boolean)


31
32
33
# File 'lib/economic/entity/handle.rb', line 31

def empty?
  to_hash.empty?
end

#to_hash(only_keys = id_properties.keys) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/economic/entity/handle.rb', line 47

def to_hash(only_keys = id_properties.keys)
  only_keys = [only_keys].flatten
  only_keys.each_with_object({}) do |key, hash|
    property = id_properties[key]
    value = send(key)
    next if value.blank?
    hash[property] = value
  end
end