Class: Athena::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/athena/record.rb

Defined Under Namespace

Classes: NoRecordError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, block = nil, add = !block)) ⇒ Record

Returns a new instance of Record.



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/athena/record.rb', line 58

def initialize(id = nil, block = nil, add = !block)
  @id, @block, @struct = id || object_id.map_positive, block, {}

  add_record if add

  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



56
57
58
# File 'lib/athena/record.rb', line 56

def block
  @block
end

#idObject (readonly)

Returns the value of attribute id.



56
57
58
# File 'lib/athena/record.rb', line 56

def id
  @id
end

#structObject (readonly)

Returns the value of attribute struct.



56
57
58
# File 'lib/athena/record.rb', line 56

def struct
  @struct
end

Class Method Details

.[](field = nil, config = nil) ⇒ Object

Raises:



46
47
48
49
50
51
52
# File 'lib/athena/record.rb', line 46

def [](field = nil, config = nil)
  record = records.last
  raise NoRecordError unless record

  record.fill(field, config) if field && config
  record
end

.recordsObject



42
43
44
# File 'lib/athena/record.rb', line 42

def records
  @records
end

Instance Method Details

#closeObject



81
82
83
# File 'lib/athena/record.rb', line 81

def close
  block ? block[self] : self
end

#fill(field, config) ⇒ Object



72
73
74
# File 'lib/athena/record.rb', line 72

def fill(field, config)
  struct[field] ||= config.merge(:values => Hash.new { |h, k| h[k] = [] })
end

#to(format) ⇒ Object



85
86
87
# File 'lib/athena/record.rb', line 85

def to(format)
  Formats[:out, format].convert(self)
end

#update(element, data, field_config = nil) ⇒ Object



76
77
78
79
# File 'lib/athena/record.rb', line 76

def update(element, data, field_config = nil)
  field_config.each { |field, config| fill(field, config) } if field_config
  struct.each_key { |field| struct[field][:values][element] << data }
end