Class: Beeminder::Datapoint

Inherits:
Object
  • Object
show all
Defined in:
lib/beeminder/goals.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info = {}) ⇒ Datapoint

Returns a new instance of Datapoint.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/beeminder/goals.rb', line 210

def initialize info={}
  # set variables
  info.each do |k,v|
    instance_variable_set "@#{k}", v
  end

  # defaults
  @timestamp ||= DateTime.now
  @comment   ||= ""

  # some conversions
  @timestamp  = DateTime.strptime(@timestamp.to_s,  '%s') unless @timestamp.is_a?(Date) || @timestamp.is_a?(Time)
  @updated_at = DateTime.strptime(@updated_at.to_s, '%s') unless @updated_at.nil?

  # set timezone if possible
  unless @goal.nil?
    @timestamp  = @timestamp.in_time_zone  @goal.user.timezone
    @updated_at = @updated_at.in_time_zone @goal.user.timezone
  end
end

Instance Attribute Details

#commentString

Returns An optional comment about the datapoint.

Returns:

  • (String)

    An optional comment about the datapoint.



198
199
200
# File 'lib/beeminder/goals.rb', line 198

def comment
  @comment
end

#goalBeeminder::Goal

Returns Goal this datapoint belongs to. Optional for new datapoints. Use ‘Goal#add` to add new datapoints to a goal.

Returns:

  • (Beeminder::Goal)

    Goal this datapoint belongs to. Optional for new datapoints. Use ‘Goal#add` to add new datapoints to a goal.



208
209
210
# File 'lib/beeminder/goals.rb', line 208

def goal
  @goal
end

#idString (readonly)

Returns A unique ID, used to identify a datapoint when deleting or editing it.

Returns:

  • (String)

    A unique ID, used to identify a datapoint when deleting or editing it.



201
202
203
# File 'lib/beeminder/goals.rb', line 201

def id
  @id
end

#timestampDateTime

Returns Time of the datapoint.

Returns:

  • (DateTime)

    Time of the datapoint.



192
193
194
# File 'lib/beeminder/goals.rb', line 192

def timestamp
  @timestamp
end

#updated_atDateTime (readonly)

Returns The time that this datapoint was entered or last updated.

Returns:

  • (DateTime)

    The time that this datapoint was entered or last updated.



204
205
206
# File 'lib/beeminder/goals.rb', line 204

def updated_at
  @updated_at
end

#valueNumeric

Returns Value of the datapoint.

Returns:

  • (Numeric)

    Value of the datapoint.



195
196
197
# File 'lib/beeminder/goals.rb', line 195

def value
  @value
end

Instance Method Details

#to_hashHash

Convert datapoint to hash for POSTing.

Returns:

  • (Hash)


233
234
235
236
237
238
239
240
241
242
243
# File 'lib/beeminder/goals.rb', line 233

def to_hash
  if not @goal.nil? and @goal.user.enforce_timezone?
    @timestamp = @goal.user.convert_to_timezone @timestamp
  end
  
  {
    "timestamp" => @timestamp.strftime('%s'),
    "value"     => @value || 0,
    "comment"   => @comment || "",
  }
end