Module: Bogo::Lazy::InstanceMethods
- Defined in:
- lib/bogo/lazy.rb
Overview
Instance methods for laziness
Class Method Summary collapse
Instance Method Summary collapse
-
#_mon ⇒ Monitor
Monitor instance for synchronization.
-
#attributes ⇒ Smash
Current data state.
-
#data ⇒ Smash
Argument hash.
-
#dirty ⇒ Smash
Updated data.
-
#dirty?(attr = nil) ⇒ TrueClass, FalseClass
Model is dirty or specific attribute is dirty.
- #inspect ⇒ String
-
#load_data(args = {}) ⇒ self
Create new instance.
- #to_h ⇒ Hash
- #to_s ⇒ String
-
#valid_state ⇒ self
Identifies valid state and automatically merges dirty attributes into data, clears dirty attributes.
Class Method Details
.included(klass) ⇒ Object
11 12 13 14 |
# File 'lib/bogo/lazy.rb', line 11 def self.included(klass) klass.include(MonitorMixin) klass.instance_variable_set(:@calling_on_missing, false) end |
Instance Method Details
#_mon ⇒ Monitor
17 18 19 |
# File 'lib/bogo/lazy.rb', line 17 def _mon @mon ||= Monitor.new end |
#attributes ⇒ Smash
47 48 49 |
# File 'lib/bogo/lazy.rb', line 47 def attributes _mon.synchronize { data.merge(dirty) } end |
#data ⇒ Smash
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bogo/lazy.rb', line 22 def data _mon.synchronize do unless(@data) @data = Smash.new self.class.attributes.each do |key, value| if(value.has_key?('default')) @data[key] = value['default'] end end end end @data end |
#dirty ⇒ Smash
37 38 39 40 41 42 43 44 |
# File 'lib/bogo/lazy.rb', line 37 def dirty _mon.synchronize do unless(@dirty) @dirty = Smash.new end end @dirty end |
#dirty?(attr = nil) ⇒ TrueClass, FalseClass
Model is dirty or specific attribute is dirty
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/bogo/lazy.rb', line 95 def dirty?(attr=nil) _mon.synchronize do if(attr) dirty.has_key?(attr) else if(@_checksum) !dirty.empty? || @_checksum != Digest::SHA256.hexdigest(MultiJson.dump(data.inspect).to_s) else true end end end end |
#inspect ⇒ String
116 117 118 |
# File 'lib/bogo/lazy.rb', line 116 def inspect "<#{self.class.name}:#{object_id} [#{data.inspect}]>" end |
#load_data(args = {}) ⇒ self
Create new instance
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/bogo/lazy.rb', line 55 def load_data(args={}) _mon.synchronize do args = args.to_smash @data = Smash.new self.class.attributes.each do |name, | val = args[name] if([:required] && !args.has_key?(name) && !.has_key?(:default)) raise ArgumentError.new("Missing required option: `#{name}`") end if(val.nil? && !args.has_key?(name) && [:default]) if([:default]) val = [:default].respond_to?(:call) ? [:default].call : [:default] end end if(args.has_key?(name) || val) self.send("#{name}=", val) end end end self end |
#to_h ⇒ Hash
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/bogo/lazy.rb', line 121 def to_h _mon.synchronize do Hash[ attributes.map{|k,v| [k, v.is_a?(Array) ? v.map{|x| x.respond_to?(:to_h) ? x.to_h : x} : v.respond_to?(:to_h) ? v.to_h : v] } ] end end |
#to_s ⇒ String
111 112 113 |
# File 'lib/bogo/lazy.rb', line 111 def to_s "<#{self.class.name}:#{object_id}>" end |
#valid_state ⇒ self
Identifies valid state and automatically merges dirty attributes into data, clears dirty attributes
82 83 84 85 86 87 88 89 |
# File 'lib/bogo/lazy.rb', line 82 def valid_state _mon.synchronize do data.merge!(dirty) dirty.clear @_checksum = Digest::SHA256.hexdigest(MultiJson.dump(data.inspect).to_s) end self end |