Class: Osm::Budget
Constant Summary collapse
- SORT_BY =
[:section_id, :name]
Instance Attribute Summary collapse
-
#id ⇒ Fixnum
The OSM ID for the budget.
-
#name ⇒ String
The name of the budget.
-
#section_id ⇒ Fixnum
The OSM ID for the section the budget belongs to.
Class Method Summary collapse
-
.get_for_section(api, section, options = {}) ⇒ Array<Osm::Budget>
Get budgets for a section.
Instance Method Summary collapse
-
#create(api) ⇒ Boolean
Create the budget in OSM.
-
#delete(api) ⇒ Boolean
Delete budget from OSM.
-
#initialize ⇒ Object
constructor
Initialize a new Budget.
-
#update(api) ⇒ Boolean
Update budget in OSM.
Methods inherited from Model
#<, #<=, #<=>, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i
Constructor Details
#initialize ⇒ Object
Initialize a new Budget
|
# File 'lib/osm/budget.rb', line 26
|
Instance Attribute Details
#id ⇒ Fixnum
Returns The OSM ID for the budget.
13 |
# File 'lib/osm/budget.rb', line 13 attribute :id, :type => Integer |
#name ⇒ String
Returns The name of the budget.
13 |
# File 'lib/osm/budget.rb', line 13 attribute :id, :type => Integer |
#section_id ⇒ Fixnum
Returns The OSM ID for the section the budget belongs to.
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
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, ={}) Osm::Model.require_ability_to(api, :read, :finance, section, ) section_id = section.to_i cache_key = ['budgets', section_id] if ![: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§ionid=#{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
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§ionid=#{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§ionid=#{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
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§ionid=#{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 |