Sha256: bb02126f6e244b42722fd6b36888cee03469317b043dda2d0c99ca2e754dd036

Contents?: true

Size: 1.11 KB

Versions: 43

Compression:

Stored size: 1.11 KB

Contents

module Dataset
  module Record # :nodoc:
    
    class Model # :nodoc:
      attr_reader :attributes, :model, :meta, :symbolic_name, :session_binding
      
      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
      
      def record_class
        meta.record_class
      end
      
      def id
        model.id
      end
      
      def create
        model = to_model
        model.save!
        model
      end
      
      def to_hash
        to_model.attributes
      end
      
      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
    end
    
  end
end

Version data entries

43 entries across 43 versions & 8 rubygems

Version Path
aiwilliams-dataset-1.2.0 lib/dataset/record/model.rb
aiwilliams-dataset-1.3.0 lib/dataset/record/model.rb
aiwilliams-dataset-1.3.1 lib/dataset/record/model.rb
karsthammer-dataset-1.2.0 lib/dataset/record/model.rb
radiant-1.1.4 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.1.3 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.1.2 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.1.1 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.1.0 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.1.0.rc1 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.1.0.beta vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.0.1 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.1.0.alpha vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.0.0 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.0.0.rc5 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.0.0.rc4 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.0.0.rc3 vendor/plugins/dataset/lib/dataset/record/model.rb
kajam-1.0.3.rc2 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.0.0.rc2 vendor/plugins/dataset/lib/dataset/record/model.rb
radiant-1.0.0.rc1 vendor/plugins/dataset/lib/dataset/record/model.rb