Class: ODDB::Remote::Drugs::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/oddb/remote/drugs/package.rb

Instance Attribute Summary

Attributes inherited from Object

#source

Instance Method Summary collapse

Methods inherited from Object

delegate, #uid

Constructor Details

#initialize(source, remote, currency_rate, tax_factor = 1.0) ⇒ Package

Returns a new instance of Package.



20
21
22
23
24
25
# File 'lib/oddb/remote/drugs/package.rb', line 20

def initialize(source, remote, currency_rate, tax_factor=1.0)
  @tax_factor = tax_factor.to_f
  @currency_rate = currency_rate.to_f
  @cache = {}
  super(source, remote)
end

Instance Method Details

#active_agentsObject



26
27
28
29
30
# File 'lib/oddb/remote/drugs/package.rb', line 26

def active_agents
  @active_agents ||= @remote.active_agents.collect { |act|
    Remote::Drugs::ActiveAgent.new(@source, act)
  }
end

#atcObject



31
32
33
34
35
# File 'lib/oddb/remote/drugs/package.rb', line 31

def atc
  @atc ||= if atc = @remote.atc_class
             Remote::Drugs::Atc.new(@source, atc)
           end
end

#code(type, country = 'CH') ⇒ Object



36
37
38
39
40
41
# File 'lib/oddb/remote/drugs/package.rb', line 36

def code(type, country='CH')
  case type
  when :ean
    @ean ||= Util::Code.new(:ean, @remote.barcode, 'CH')
  end
end

#companyObject



42
43
44
# File 'lib/oddb/remote/drugs/package.rb', line 42

def company
  @company ||= Remote::Business::Company.new(@source, @remote.company)
end

#comparable_sizeObject



50
51
52
# File 'lib/oddb/remote/drugs/package.rb', line 50

def comparable_size
  @comparable_size ||= @remote.comparable_size
end

#comparablesObject



45
46
47
48
49
# File 'lib/oddb/remote/drugs/package.rb', line 45

def comparables
  local_comparables.concat @remote.comparables.collect { |pac|
    Package.new(@source, pac, @currency_rate, @tax_factor)
  }
end

#dddsObject



53
54
55
56
57
58
59
60
# File 'lib/oddb/remote/drugs/package.rb', line 53

def ddds
  @ddds ||= galenic_forms.inject([]) { |memo, form|
    if(group = form.group)
      memo.concat atc.ddds(group.administration)
    end
    memo
  }
end

#dose_price(dose) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/oddb/remote/drugs/package.rb', line 61

def dose_price(dose)
  if(price = price(:public))
    pdose = doses.first.want(dose.unit)
    Util::Money.new((dose / pdose).to_f * (price.to_f / size))
  end
rescue StandardError
end

#galenic_formsObject



68
69
70
71
72
# File 'lib/oddb/remote/drugs/package.rb', line 68

def galenic_forms
  @galenic_forms ||= @remote.galenic_forms.collect { |form|
    Remote::Drugs::GalenicForm.new(@source, form)
  }
end

#local_comparablesObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/oddb/remote/drugs/package.rb', line 73

def local_comparables
  comparables = []
  doses = active_agents.collect { |act| act.dose }
  if(doses.size == 1 \
     && (atc = self.atc && ODDB::Drugs::Atc.find_by_code(self.atc.code)))
    descriptions = galenic_forms.collect { |form| form.description.de }
    groupnames = galenic_forms.collect { |form| form.groupname }
    range = (size*0.75)..(size*1.25)
    atc.products.each { |prod|
      prod.sequences.each { |seq|
        if(seq.doses == doses)
          descs = []
          names = []
          seq.galenic_forms.each { |form|
            descs.push form.description
            if grp = form.group
              names.push grp.name
            end
          }
          if(descs == descriptions || groupnames.all? { |name|
             names.any? { |other| other == name } })
            comparables.concat seq.packages.select { |pac|
              range.include?(pac.size)
            }
          end
        end
      }
    }
  end
  comparables
end

#nameObject



104
105
106
# File 'lib/oddb/remote/drugs/package.rb', line 104

def name
  @name ||= Util::Multilingual.new(:de => @remote.name_base)
end

#partsObject



107
108
109
110
111
# File 'lib/oddb/remote/drugs/package.rb', line 107

def parts
  @parts ||= @remote.parts.collect { |part| 
    Remote::Drugs::Part.new(@source, part)
  }
end

#price(type) ⇒ Object



112
113
114
115
116
117
# File 'lib/oddb/remote/drugs/package.rb', line 112

def price(type)
  case type
  when :public, :exfactory
    @cache.fetch(type) { @cache.store(type, remote_price(type)) }
  end
end

#remote_price(key) ⇒ Object



118
119
120
121
# File 'lib/oddb/remote/drugs/package.rb', line 118

def remote_price(key)
  pr = @remote.send("price_#{key}").to_f * @currency_rate / @tax_factor
  Util::Money.new(pr.to_f) if(pr > 0)
end

#sizeObject



122
123
124
# File 'lib/oddb/remote/drugs/package.rb', line 122

def size
  @size ||= comparable_size.qty
end