Module: Bogo::Lazy::InstanceMethods

Defined in:
lib/bogo/lazy.rb

Overview

Instance methods for laziness

Instance Method Summary collapse

Instance Method Details

#attributesSmash

Returns current data state.

Returns:

  • (Smash)

    current data state



34
35
36
# File 'lib/bogo/lazy.rb', line 34

def attributes
  data.merge(dirty)
end

#dataSmash

Returns argument hash.

Returns:

  • (Smash)

    argument hash



13
14
15
16
17
18
19
20
21
22
23
# 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

#dirtySmash

Returns updated data.

Returns:

  • (Smash)

    updated data



26
27
28
29
30
31
# File 'lib/bogo/lazy.rb', line 26

def dirty
  unless(@dirty)
    @dirty = Smash.new
  end
  @dirty
end

#dirty?(attr = nil) ⇒ TrueClass, FalseClass

Model is dirty or specific attribute is dirty

Parameters:

  • attr (String, Symbol) (defaults to: nil)

    name of attribute

Returns:

  • (TrueClass, FalseClass)

    model or attribute is dirty



78
79
80
81
82
83
84
85
86
87
88
89
# 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

#inspectString

Returns:

  • (String)


97
98
99
# File 'lib/bogo/lazy.rb', line 97

def inspect
  "<#{self.class.name}:#{object_id} [#{data.inspect}]>"
end

#load_data(args = {}) ⇒ self

Create new instance

Parameters:

  • args (Hash) (defaults to: {})

Returns:

  • (self)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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_hHash

Returns:



102
103
104
105
106
107
108
109
110
# 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_sString

Returns:

  • (String)


92
93
94
# File 'lib/bogo/lazy.rb', line 92

def to_s
  "<#{self.class.name}:#{object_id}>"
end

#valid_stateself

Identifies valid state and automatically merges dirty attributes into data, clears dirty attributes

Returns:

  • (self)


67
68
69
70
71
72
# 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