module Bogo::Lazy::InstanceMethods
Instance methods for laziness
Public Instance Methods
attributes()
click to toggle source
@return [Smash] current data state
# File lib/bogo/lazy.rb, line 34 def attributes data.merge(dirty) end
data()
click to toggle source
@return [Smash] argument hash
# File lib/bogo/lazy.rb, line 13 def data unless(@data) @data = Smash.new self.class.attributes.each do |key, value| if(value.has_key?('default')) @data[key] = value['default'] end end end @data end
dirty()
click to toggle source
@return [Smash] updated data
# File lib/bogo/lazy.rb, line 26 def dirty unless(@dirty) @dirty = Smash.new end @dirty end
dirty?(attr=nil)
click to toggle source
Model is dirty or specific attribute is dirty
@param attr [String, Symbol] name of attribute @return [TrueClass, FalseClass] model or attribute is dirty
# File lib/bogo/lazy.rb, line 78 def dirty?(attr=nil) 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
inspect()
click to toggle source
@return [String]
# File lib/bogo/lazy.rb, line 97 def inspect "<#{self.class.name}:#{object_id} [#{data.inspect}]>" end
load_data(args={})
click to toggle source
Create new instance
@param args [Hash] @return [self]
# File lib/bogo/lazy.rb, line 42 def load_data(args={}) args = args.to_smash @data = Smash.new self.class.attributes.each do |name, options| val = args[name] if(options[:required] && !args.has_key?(name) && !options.has_key?(:default)) raise ArgumentError.new("Missing required option: `#{name}`") end if(val.nil? && !args.has_key?(name) && options[:default]) if(options[:default]) val = options[:default].respond_to?(:call) ? options[:default].call : options[:default] end end if(args.has_key?(name) || val) self.send("#{name}=", val) end end self end
to_h()
click to toggle source
@return [Hash]
# File lib/bogo/lazy.rb, line 102 def to_h 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
to_s()
click to toggle source
@return [String]
# File lib/bogo/lazy.rb, line 92 def to_s "<#{self.class.name}:#{object_id}>" end
valid_state()
click to toggle source
Identifies valid state and automatically merges dirty attributes into data, clears dirty attributes
@return [self]
# File lib/bogo/lazy.rb, line 67 def valid_state data.merge!(dirty) dirty.clear @_checksum = Digest::SHA256.hexdigest(MultiJson.dump(data.inspect).to_s) self end