Module: Miasma::Utils::Lazy::InstanceMethods
- Defined in:
- lib/miasma/utils/lazy.rb
Overview
Instance methods for laziness
Instance Method Summary (collapse)
-
- (Smash) attributes
Current data state.
-
- (Smash) data
Argument hash.
-
- (Smash) dirty
Updated data.
-
- (TrueClass, FalseClass) dirty?(attr = nil)
Model is dirty or specific attribute is dirty.
- - (String) inspect
-
- (self) load_data(args = {})
Create new instance.
- - (String) to_s
-
- (self) valid_state
Identifies valid state and automatically merges dirty attributes into data, clears dirty attributes.
Instance Method Details
- (Smash) attributes
Returns current data state
29 30 31 |
# File 'lib/miasma/utils/lazy.rb', line 29 def attributes data.merge(dirty) end |
- (Smash) data
Returns argument hash
13 14 15 16 17 18 |
# File 'lib/miasma/utils/lazy.rb', line 13 def data unless(@data) @data = Smash.new end @data end |
- (Smash) dirty
Returns updated data
21 22 23 24 25 26 |
# File 'lib/miasma/utils/lazy.rb', line 21 def dirty unless(@dirty) @dirty = Smash.new end @dirty end |
- (TrueClass, FalseClass) dirty?(attr = nil)
Model is dirty or specific attribute is dirty
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/miasma/utils/lazy.rb', line 73 def dirty?(attr=nil) if(attr) dirty.has_key?(attr) else if(@_checksum) !dirty.empty? || @_checksum != Digest::SHA256.hexdigest(MultiJson.dump(data)) else true end end end |
- (String) inspect
92 93 94 |
# File 'lib/miasma/utils/lazy.rb', line 92 def inspect "<#{self.class.name}:#{object_id} [#{data.inspect}]>" end |
- (self) load_data(args = {})
Create new instance
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/miasma/utils/lazy.rb', line 37 def load_data(args={}) 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 self end |
- (String) to_s
87 88 89 |
# File 'lib/miasma/utils/lazy.rb', line 87 def to_s "<#{self.class.name}:#{object_id}>" end |
- (self) valid_state
Identifies valid state and automatically merges dirty attributes into data, clears dirty attributes
62 63 64 65 66 67 |
# File 'lib/miasma/utils/lazy.rb', line 62 def valid_state data.merge!(dirty) dirty.clear @_checksum = Digest::SHA256.hexdigest(MultiJson.dump(data)) self end |