Class: Osm::Budget

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

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

Parameters:

  • attributes (Hash)

    The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)


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

Instance Attribute Details

#idFixnum

Returns The OSM ID for the budget.

Returns:

  • (Fixnum)

    The OSM ID for the budget


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

attribute :id, :type => Integer

#nameString

Returns The name of the budget.

Returns:

  • (String)

    The name of the budget


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

attribute :id, :type => Integer

#section_idFixnum

Returns The OSM ID for the section the budget belongs to.

Returns:

  • (Fixnum)

    The OSM ID for the section the budget belongs to


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

attribute :id, :type => Integer

Class Method Details

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

Get budgets for a section

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the structure for

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

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

Returns:

  • (Array<Osm::Budget>)

    representing the donations made


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

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

#<=>(another) ⇒ Object

Compare Budget based on section_id then name


130
131
132
133
134
# File 'lib/osm/budget.rb', line 130

def <=>(another)
  result = self.section_id <=> another.try(:section_id)
  result = self.name <=> another.try(:name) if result == 0
  return result
end

#create(api) ⇒ Boolean

Create the budget in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the budget was created

Raises:


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

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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the budget was deleted


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

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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the budget was updated

Raises:


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

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