Class: Dataset::Record::Model

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta, attributes, symbolic_name, session_binding) ⇒ Model

Returns a new instance of Model.



7
8
9
10
11
12
# File 'lib/dataset/record/model.rb', line 7

def initialize(meta, attributes, symbolic_name, session_binding)
  @meta            = meta
  @attributes      = attributes.stringify_keys
  @symbolic_name   = symbolic_name || object_id
  @session_binding = session_binding
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/dataset/record/model.rb', line 5

def attributes
  @attributes
end

#metaObject (readonly)

Returns the value of attribute meta.



5
6
7
# File 'lib/dataset/record/model.rb', line 5

def meta
  @meta
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/dataset/record/model.rb', line 5

def model
  @model
end

#session_bindingObject (readonly)

Returns the value of attribute session_binding.



5
6
7
# File 'lib/dataset/record/model.rb', line 5

def session_binding
  @session_binding
end

#symbolic_nameObject (readonly)

Returns the value of attribute symbolic_name.



5
6
7
# File 'lib/dataset/record/model.rb', line 5

def symbolic_name
  @symbolic_name
end

Instance Method Details

#createObject



22
23
24
25
26
# File 'lib/dataset/record/model.rb', line 22

def create
  model = to_model
  model.save!
  model
end

#idObject



18
19
20
# File 'lib/dataset/record/model.rb', line 18

def id
  model.id
end

#record_classObject



14
15
16
# File 'lib/dataset/record/model.rb', line 14

def record_class
  meta.record_class
end

#to_hashObject



28
29
30
# File 'lib/dataset/record/model.rb', line 28

def to_hash
  to_model.attributes
end

#to_modelObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dataset/record/model.rb', line 32

def to_model
  @model ||= begin
    m = meta.record_class.new
    attributes.each do |k,v|
      if reflection = record_class.reflect_on_association(k.to_sym)
        case v
        when Symbol
          v = session_binding.find_model(reflection.klass, v)
        end
      end
      m.send "#{k}=", v
    end
    m
  end
end