Class: Rie::BaseChanger
- Inherits:
-
Object
- Object
- Rie::BaseChanger
- Defined in:
- lib/rie/base_changer.rb
Instance Attribute Summary collapse
-
#changes ⇒ Object
readonly
Returns the value of attribute changes.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#original(key) ⇒ Object
readonly
Returns the value of attribute original.
-
#retractions ⇒ Object
readonly
Returns the value of attribute retractions.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #change(key = nil, &b) ⇒ Object
- #change!(&b) ⇒ Object
- #change_in(key) ⇒ Object
- #change_ref(key, &b) ⇒ Object
- #generate_datoms {|model.base_attributes.merge(:'db/id' => @id)| ... } ⇒ Object
-
#initialize(id, attrs) ⇒ BaseChanger
constructor
A new instance of BaseChanger.
- #retract!(attribute) ⇒ Object
- #updated_attributes ⇒ Object
Constructor Details
#initialize(id, attrs) ⇒ BaseChanger
Returns a new instance of BaseChanger.
4 5 6 7 8 9 |
# File 'lib/rie/base_changer.rb', line 4 def initialize(id, attrs) @id = id @original = attrs.dup.freeze @changes = {} @retractions = Set.new end |
Instance Attribute Details
#changes ⇒ Object (readonly)
Returns the value of attribute changes.
3 4 5 |
# File 'lib/rie/base_changer.rb', line 3 def changes @changes end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/rie/base_changer.rb', line 3 def id @id end |
#original(key) ⇒ Object (readonly)
Returns the value of attribute original.
3 4 5 |
# File 'lib/rie/base_changer.rb', line 3 def original @original end |
#retractions ⇒ Object (readonly)
Returns the value of attribute retractions.
3 4 5 |
# File 'lib/rie/base_changer.rb', line 3 def retractions @retractions end |
Instance Method Details
#[](key) ⇒ Object
36 37 38 39 |
# File 'lib/rie/base_changer.rb', line 36 def [](key) return nil if @retractions.include? key @changes.fetch(key) { @original[key] } end |
#[]=(key, val) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/rie/base_changer.rb', line 49 def []=(key, val) if val.nil? @retractions << key else @retractions.delete(key) @changes[key] = val end end |
#change(key = nil, &b) ⇒ Object
15 16 17 18 |
# File 'lib/rie/base_changer.rb', line 15 def change(key=nil, &b) b.call(self) self end |
#change!(&b) ⇒ Object
31 32 33 34 |
# File 'lib/rie/base_changer.rb', line 31 def change!(&b) change(&b) save! end |
#change_in(key) ⇒ Object
45 46 47 |
# File 'lib/rie/base_changer.rb', line 45 def change_in(key) [original(key), self[key]] end |
#change_ref(key, &b) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rie/base_changer.rb', line 20 def change_ref(key, &b) attribute = model.get_attribute(key) type = attribute.type unless type.respond_to? :ref_class raise ::TypeError, "change_ref only works on refs - #{key} is a #{type.inspect} is not a ref" end self[key] = self[key] ? self[key].change(&b) : type.ref_class.create(&b) end |
#generate_datoms {|model.base_attributes.merge(:'db/id' => @id)| ... } ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/rie/base_changer.rb', line 64 def generate_datoms(&b) return enum_for(:generate_datoms).to_a unless block_given? yield model.base_attributes.merge(:'db/id' => @id) @changes.each do |key, new_val| attribute = model.get_attribute(key) attribute.datoms_for(self, new_val, &b) end end |
#retract!(attribute) ⇒ Object
11 12 13 |
# File 'lib/rie/base_changer.rb', line 11 def retract!(attribute) @retractions << attribute end |
#updated_attributes ⇒ Object
58 59 60 61 62 |
# File 'lib/rie/base_changer.rb', line 58 def updated_attributes out = model.attributes.merge(@changes) @retractions.each { |r| out.delete(r) } out end |