Class: Osm::Term
Instance Attribute Summary collapse
-
#finish ⇒ Date
When the term finishes.
-
#id ⇒ Fixnum
The id for the term.
-
#name ⇒ Fixnum
The name of the term.
-
#section_id ⇒ Fixnum
The section the term belongs to.
-
#start ⇒ Date
When the term starts.
Class Method Summary collapse
-
.create(api, options = {}) ⇒ Boolean
Create a term in OSM.
-
.get(api, term_id, options = {}) ⇒ Object
Get a term.
-
.get_all(api, options = {}) ⇒ Array<Osm::Term>
Get the terms that the OSM user can access.
-
.get_current_term_for_section(api, section, options = {}) ⇒ Osm::Term?
Get the current term for a given section.
-
.get_for_section(api, section, options = {}) ⇒ Array<Osm::Term>?
Get the terms that the OSM user can access for a given section.
Instance Method Summary collapse
-
#<=>(another_term) ⇒ Object
Compare Term based on section_id, start then id.
-
#after?(date) ⇒ Boolean
Determine if the term is completly after the passed date.
-
#before?(date) ⇒ Boolean
Determine if the term is completly before the passed date.
-
#contains_date?(date) ⇒ Boolean
Determine if the provided date is within the term.
-
#current? ⇒ Boolean
Determine if the term is current.
-
#future? ⇒ Boolean
Determine if the term is in the future.
-
#initialize ⇒ Object
constructor
Initialize a new Term.
-
#past? ⇒ Boolean
Determine if the term is in the past.
-
#update(api) ⇒ Boolean
Update a term in OSM.
Methods inherited from Model
#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i
Constructor Details
#initialize ⇒ Object
Initialize a new Term
|
# File 'lib/osm/term.rb', line 181
|
Instance Attribute Details
#finish ⇒ Date
Returns when the term finishes.
16 |
# File 'lib/osm/term.rb', line 16 attribute :id, :type => Integer |
#id ⇒ Fixnum
Returns the id for the term.
16 |
# File 'lib/osm/term.rb', line 16 attribute :id, :type => Integer |
#name ⇒ Fixnum
Returns the name of the term.
16 |
# File 'lib/osm/term.rb', line 16 attribute :id, :type => Integer |
#section_id ⇒ Fixnum
Returns the section the term belongs to.
16 |
# File 'lib/osm/term.rb', line 16 attribute :id, :type => Integer |
#start ⇒ Date
Returns when the term starts.
16 |
# File 'lib/osm/term.rb', line 16 attribute :id, :type => Integer |
Class Method Details
.create(api, options = {}) ⇒ Boolean
Create a term in OSM
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/osm/term.rb', line 128 def self.create(api, ={}) raise ArgumentError, ":section can't be nil" if [:section].nil? raise ArgumentError, ":name can't be nil" if [:name].nil? raise ArgumentError, ":start can't be nil" if [:start].nil? raise ArgumentError, ":finish can't be nil" if [:finish].nil? require_access_to_section(api, [:section]) api_data = { 'term' => [:name], 'start' => [:start].strftime(Osm::OSM_DATE_FORMAT), 'end' => [:finish].strftime(Osm::OSM_DATE_FORMAT), 'termid' => '0' } data = api.perform_query("users.php?action=addTerm§ionid=#{options[:section].to_i}", api_data) # The cached terms for the section will be out of date - remove them get_all(api, ).each do |term| cache_delete(api, ['term', term.id]) if term.section_id == section_id end cache_delete(api, ['terms', api.user_id]) return data.is_a?(Hash) && data['terms'].is_a?(Hash) end |
.get(api, term_id, options = {}) ⇒ Object
Get a term
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/osm/term.rb', line 83 def self.get(api, term_id, ={}) cache_key = ['term', term_id] if ![:no_cache] && cache_exist?(api, cache_key) term = cache_read(api, cache_key) return term if can_access_section?(api, term.section_id, ) end terms = get_all(api, ) return nil unless terms.is_a? Array terms.each do |term| if term.id == term_id return (can_access_section?(api, term.section_id, ) ? term : nil) end end return nil end |
.get_all(api, options = {}) ⇒ Array<Osm::Term>
Get the terms that the OSM user can access
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 60 61 62 63 64 |
# File 'lib/osm/term.rb', line 35 def self.get_all(api, ={}) cache_key = ['terms', api.user_id] if ![:no_cache] && cache_exist?(api, cache_key) ids = cache_read(api, cache_key) return get_from_ids(api, ids, 'term', , :get_all) end data = api.perform_query('api.php?action=getTerms') terms = Array.new ids = Array.new data.each_key do |key| data[key].each do |term_data| term = Osm::Term.new( :id => Osm::to_i_or_nil(term_data['termid']), :section_id => Osm::to_i_or_nil(term_data['sectionid']), :name => term_data['name'], :start => Osm::parse_date(term_data['startdate']), :finish => Osm::parse_date(term_data['enddate']), ) terms.push term ids.push term.id cache_write(api, ['term', term.id], term) end end cache_write(api, cache_key, ids) return terms end |
.get_current_term_for_section(api, section, options = {}) ⇒ Osm::Term?
Get the current term for a given section
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/osm/term.rb', line 108 def self.get_current_term_for_section(api, section, ={}) section_id = section.to_i terms = get_for_section(api, section_id, ) return nil if terms.nil? terms.each do |term| return term if term.current? end raise Osm::Error, 'There is no current term for the section.' end |
.get_for_section(api, section, options = {}) ⇒ Array<Osm::Term>?
Get the terms that the OSM user can access for a given section
71 72 73 74 75 |
# File 'lib/osm/term.rb', line 71 def self.get_for_section(api, section, ={}) require_access_to_section(api, section, ) section_id = section.to_i return get_all(api, ).select{ |term| term.section_id == section_id } end |
Instance Method Details
#<=>(another_term) ⇒ Object
Compare Term based on section_id, start then id
234 235 236 237 238 239 |
# File 'lib/osm/term.rb', line 234 def <=>(another_term) result = self.section_id <=> another_term.section_id result = self.start <=> another_term.start if result == 0 result = self.id <=> another_term.id if result == 0 return result end |
#after?(date) ⇒ Boolean
Determine if the term is completly after the passed date
197 198 199 200 |
# File 'lib/osm/term.rb', line 197 def after?(date) return false if start.nil? return start > date.to_date end |
#before?(date) ⇒ Boolean
Determine if the term is completly before the passed date
189 190 191 192 |
# File 'lib/osm/term.rb', line 189 def before?(date) return false if finish.nil? return finish < date.to_date end |
#contains_date?(date) ⇒ Boolean
Determine if the provided date is within the term
227 228 229 230 231 |
# File 'lib/osm/term.rb', line 227 def contains_date?(date) return false if start.nil? return false if finish.nil? return (start <= date) && (finish >= date) end |
#current? ⇒ Boolean
Determine if the term is current
218 219 220 221 222 |
# File 'lib/osm/term.rb', line 218 def current? return false if start.nil? return false if finish.nil? return (start <= Date.today) && (finish >= Date.today) end |
#future? ⇒ Boolean
Determine if the term is in the future
204 205 206 207 |
# File 'lib/osm/term.rb', line 204 def future? return false if start.nil? return start > Date.today end |
#past? ⇒ Boolean
Determine if the term is in the past
211 212 213 214 |
# File 'lib/osm/term.rb', line 211 def past? return false if finish.nil? return finish < Date.today end |
#update(api) ⇒ Boolean
Update a term in OSM
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/osm/term.rb', line 158 def update(api) raise Osm::ObjectIsInvalid, 'term is invalid' unless valid? require_access_to_section(api, section_id) data = api.perform_query("users.php?action=addTerm§ionid=#{section_id}", { 'term' => name, 'start' => start.strftime(Osm::OSM_DATE_FORMAT), 'end' => finish.strftime(Osm::OSM_DATE_FORMAT), 'termid' => id }) if data.is_a?(Hash) && data['terms'].is_a?(Hash) reset_changed_attributes # The cached term will be out of date - remove it cache_delete(api, ['term', id]) return true else return false end end |