Sha256: 4801fce2ca7533b7893bff7cd4eeaa1217823d8e124627589901405455aa2db0

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module ActiveData
  module Model
    module Associations
      module Reflections
        class EmbedsOne < Base
          def self.build target, generated_methods, name, options = {}, &block
            if target < ActiveData::Model::Attributes
              target.add_attribute(ActiveData::Model::Attributes::Reflections::Base, name)
            end
            options[:validate] = true unless options.key?(:validate)
            super
          end

          def self.generate_methods name, target
            target.class_eval <<-RUBY, __FILE__, __LINE__ + 1
              def #{name} force_reload = false
                association(:#{name}).reader(force_reload)
              end

              def #{name}= value
                association(:#{name}).writer(value)
              end

              def build_#{name} attributes = {}
                association(:#{name}).build(attributes)
              end

              def create_#{name} attributes = {}
                association(:#{name}).create(attributes)
              end

              def create_#{name}! attributes = {}
                association(:#{name}).create!(attributes)
              end
            RUBY
          end

          def collection?
            false
          end

          def embedded?
            true
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_data-1.0.0 lib/active_data/model/associations/reflections/embeds_one.rb