Class: Osm::Budget

Inherits:
Model
  • Object
show all
Defined in:
lib/osm/budget.rb

Constant Summary collapse

SORT_BY =
[:section_id, :name]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #<=>, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Constructor Details

#initializeObject

Initialize a new Budget



# File 'lib/osm/budget.rb', line 26


Instance Attribute Details

#idFixnum



13
# File 'lib/osm/budget.rb', line 13

attribute :id, :type => Integer

#nameString



13
# File 'lib/osm/budget.rb', line 13

attribute :id, :type => Integer

#section_idFixnum



13
# File 'lib/osm/budget.rb', line 13

attribute :id, :type => Integer

Class Method Details

.get_for_section(api, section, options = {}) ⇒ Array<Osm::Budget>

Get budgets for a section

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/osm/budget.rb', line 36

def self.get_for_section(api, section, options={})
  Osm::Model.require_ability_to(api, :read, :finance, section, options)
  section_id = section.to_i
  cache_key = ['budgets', section_id]

  if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
    return Osm::Model.cache_read(api, cache_key)
  end

  data = api.perform_query("finances.php?action=getCategories&sectionid=#{section_id}")

  budgets = []
  data = data['items']
  if data.is_a?(Array)
    data.each do |budget|
      budgets.push Budget.new(
        :id => Osm::to_i_or_nil(budget['categoryid']),
        :section_id => Osm::to_i_or_nil(budget['sectionid']),
        :name => budget['name'],
      )
    end
  end

  Osm::Model.cache_write(api, cache_key, budgets) unless budgets.nil?
  return budgets
end

Instance Method Details

#create(api) ⇒ Boolean

Create the budget in OSM

Raises:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/osm/budget.rb', line 69

def create(api)
  raise Osm::Error, 'the budget already exists in OSM' unless id.nil?
  raise Osm::ObjectIsInvalid, 'budget is invalid' unless valid?
  Osm::Model.require_ability_to(api, :write, :finance, section_id)

  data = api.perform_query("finances.php?action=addCategory&sectionid=#{section_id}")
  if data.is_a?(Hash) && data['ok'].eql?(true)
    # The cached budgets for the section will be out of date - remove them
    cache_delete(api, ['budgets', section_id])
    budgets = Budget.get_for_section(api, section_id, {:no_cache => true})
    budget = budgets.sort.select{ |b| b.name.eql?('** Unnamed **') }[-1]
    return false if budget.nil? # a new blank budget was NOT created
    budget.name = name
    if budget.update(api)
      self.id = budget.id
      return true
    end
  end
  return false
end

#delete(api) ⇒ Boolean

Delete budget from OSM



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/osm/budget.rb', line 116

def delete(api)
  Osm::Model.require_ability_to(api, :write, :finance, section_id)

  data = api.perform_query("finances.php?action=deleteCategory&sectionid=#{section_id}", {
    'categoryid' => id,
  })
  if (data.is_a?(Hash) && data['ok'].eql?(true))
    # The cached budgets for the section will be out of date - remove them
    cache_delete(api, ['budgets', section_id])
    return true
  end
  return false
end

#update(api) ⇒ Boolean

Update budget in OSM

Raises:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/osm/budget.rb', line 94

def update(api)
  raise Osm::ObjectIsInvalid, 'budget is invalid' unless valid?
  Osm::Model.require_ability_to(api, :write, :finance, section_id)

  data = api.perform_query("finances.php?action=updateCategory&sectionid=#{section_id}", {
    'categoryid' => id,
    'column' => 'name',
    'value' => name,
    'section_id' => section_id,
    'row' => 0,
  })
  if (data.is_a?(Hash) && data['ok'].eql?(true))
    # The cached budgets for the section will be out of date - remove them
    cache_delete(api, ['budgets', section_id])
    return true
  end
  return false
end