Class: Ceres::Writer
- Inherits:
-
Object
- Object
- Ceres::Writer
- Defined in:
- lib/ceres/writer.rb
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
Instance Method Summary collapse
- #apply(klass) ⇒ Object
-
#initialize(attribute, &block) ⇒ Writer
constructor
A new instance of Writer.
- #name ⇒ Object
- #target ⇒ Object
- #to_proc ⇒ Object
- #variable ⇒ Object
Constructor Details
#initialize(attribute, &block) ⇒ Writer
Returns a new instance of Writer.
6 7 8 9 |
# File 'lib/ceres/writer.rb', line 6 def initialize(attribute, &block) @attribute = attribute @block = block end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
3 4 5 |
# File 'lib/ceres/writer.rb', line 3 def attribute @attribute end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
4 5 6 |
# File 'lib/ceres/writer.rb', line 4 def block @block end |
Instance Method Details
#apply(klass) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# 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 ⇒ Object
11 12 13 |
# File 'lib/ceres/writer.rb', line 11 def name @attribute.name end |
#target ⇒ Object
15 16 17 |
# File 'lib/ceres/writer.rb', line 15 def target @attribute.target end |
#to_proc ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# 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 ⇒ Object
19 20 21 |
# File 'lib/ceres/writer.rb', line 19 def variable @attribute.variable end |