class Ceres::Writer

Attributes

attribute[R]
block[R]

Public Class Methods

new(attribute, &block) click to toggle source
# File lib/ceres/writer.rb, line 6
def initialize(attribute, &block)
  @attribute = attribute
  @block = block
end

Public Instance Methods

apply(klass) click to toggle source
# File lib/ceres/writer.rb, line 46
def apply(klass)
  if allow_writer_optimisation?
    name = self.name

    klass.instance_exec { attr_writer name }
  else
    name = "#{@attribute.name}=".to_sym
    writer = self.to_proc

    klass.instance_exec { define_method(name, &writer) }
  end
end
name() click to toggle source
# File lib/ceres/writer.rb, line 11
def name
  @attribute.name
end
target() click to toggle source
# File lib/ceres/writer.rb, line 15
def target
  @attribute.target
end
to_proc() click to toggle source
# File lib/ceres/writer.rb, line 23
def to_proc
  target = self.target
  variable = self.variable
  block = self.block

  if target
    if block
      proc do |v|
        t = instance_variable_get(target)
        t.instance_variable_set(variable, t.instance_exec(v, &block))
      end
    else
      proc { |v| instance_variable_get(target).instance_variable_set(variable, v) }
    end
  else
    if block
      proc { |v| instance_variable_set(variable, instance_exec(v, &block)) }
    else
      proc { |v| instance_variable_set(variable, v) }
    end
  end
end
variable() click to toggle source
# File lib/ceres/writer.rb, line 19
def variable
  @attribute.variable
end

Private Instance Methods

allow_writer_optimisation?() click to toggle source
# File lib/ceres/writer.rb, line 59
        def allow_writer_optimisation?
  !@block && self.target.nil? && self.variable == "@#{@attribute.name}".to_sym
end