Class: Object

Inherits:
BasicObject
Defined in:
lib/moon/packages/std/ptree.rb,
lib/moon/packages/std/core_ext/object.rb

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

This method is abstract.

Whether the object is valid or not, subclasses should overwrite this method to denote their own `blank?` state.

Returns:

  • (Boolean)

    is the object blank?


9
10
11
# File 'lib/moon/packages/std/core_ext/object.rb', line 9

def blank?
  !self
end

#dotsend(path, *args, &block) ⇒ Object

Examples:

obj.dotsend('position.x')
obj.dotsend('map.data.xsize')

Parameters:


58
59
60
61
62
63
64
65
66
# File 'lib/moon/packages/std/core_ext/object.rb', line 58

def dotsend(path, *args, &block)
  if path.is_a?(Symbol)
    __send__(path, *args, &block)
  elsif path.is_a?(Array)
    recursive_send(path, *args, &block)
  else
    recursive_send(path.split('.'), *args, &block)
  end
end

#presenceself

Checks whether the object is blank or not, returns `nil` if the object is #blank?, otherwise `self`.

Returns:

  • (self)

24
25
26
# File 'lib/moon/packages/std/core_ext/object.rb', line 24

def presence
  blank? ? nil : self
end

#present?Boolean

The opposite of #blank?

Returns:

  • (Boolean)

    is the object present?


16
17
18
# File 'lib/moon/packages/std/core_ext/object.rb', line 16

def present?
  !blank?
end

#to_tree(mark = {}) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/moon/packages/std/ptree.rb', line 11

def to_tree(mark = {})
  return mark[self] += 1 if mark.key?(self)
  mark[self] ||= 0
  mark[self] += 1
  tree = Moon::Tree.new
  instance_variables.each do |name|
    v = instance_variable_get(name)
    tree.add Pair.new(name, v.to_tree(mark))
  end
  tree
end

#try(method_name = nil, *args, &block) ⇒ Object

Tries to invoke method_name on the object, if the object does not `respond_to?` the method, nil is returned.

Parameters:


32
33
34
35
36
37
38
# File 'lib/moon/packages/std/core_ext/object.rb', line 32

def try(method_name = nil, *args, &block)
  if respond_to?(method_name)
    public_send(method_name, *args, &block)
  else
    nil
  end
end