Module: Miasma::Utils::Lazy::InstanceMethods
- Defined in:
- lib/miasma/utils/lazy.rb
Overview
Instance methods for laziness
Instance Method Summary collapse
-
#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_s ⇒ String
-
#valid_state ⇒ self
Identifies valid state and automatically merges dirty attributes into data, clears dirty attributes.
Instance Method Details
#attributes ⇒ Smash
Returns current data state.
29 30 31 |
# File 'lib/miasma/utils/lazy.rb', line 29 def attributes data.merge(dirty) end |
#data ⇒ Smash
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 |
#dirty ⇒ Smash
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 |
#dirty?(attr = nil) ⇒ TrueClass, FalseClass
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 |
#inspect ⇒ String
92 93 94 |
# File 'lib/miasma/utils/lazy.rb', line 92 def inspect "<#{self.class.name}:#{object_id} [#{data.inspect}]>" end |
#load_data(args = {}) ⇒ self
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 |
#to_s ⇒ String
87 88 89 |
# File 'lib/miasma/utils/lazy.rb', line 87 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
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 |