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.



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

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



5
6
7
8
9
# File 'lib/economic/entity/handle.rb', line 5

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

.id_propertiesObject



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

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



24
25
26
# File 'lib/economic/entity/handle.rb', line 24

def self.supported_keys
  id_properties.keys
end

Instance Method Details

#==(other) ⇒ Object



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

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



59
60
61
# File 'lib/economic/entity/handle.rb', line 59

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)


33
34
35
# File 'lib/economic/entity/handle.rb', line 33

def empty?
  to_hash.empty?
end

#to_hash(only_keys = id_properties.keys) ⇒ Object



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

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