Class: Object

Inherits:
BasicObject
Defined in:
lib/omega/common.rb

Overview

TODO rename to core_ext

Instance Method Summary collapse

Instance Method Details

#attr_from_args(args, params = {}) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/omega/common.rb', line 18

def attr_from_args(args, params = {})
  params.keys.each { |p|
    getter = "#{p}".intern
    setter = "#{p}=".intern
    if args.has_key?(p)
      self.send(setter, args[p])

    elsif args.has_key?(p.to_s)
      self.send(setter, args[p.to_s])

    else
      v = self.send(getter)
      self.send(setter, v || params[p])

    end
  }
  # TODO raise error if args has key not in params ? (optional?)
end

#eigenclassObject


47
48
49
50
51
# File 'lib/omega/common.rb', line 47

def eigenclass
  class << self
    self
  end
end

#numeric?Boolean


9
10
11
# File 'lib/omega/common.rb', line 9

def numeric?
  self.kind_of?(Numeric)
end

#numeric_string?Boolean


13
14
15
16
# File 'lib/omega/common.rb', line 13

def numeric_string?
  self.kind_of?(String) &&
  Float(self) != nil rescue false
end

#update_from(old, *attrs) ⇒ Object


37
38
39
40
41
42
43
44
45
# File 'lib/omega/common.rb', line 37

def update_from(old, *attrs)
  attrs.each { |attr|
    getter = attr.intern
    setter = "#{attr}=".intern
    v  = old.send(:[], getter) if old.respond_to?(:[])
    v  = old.send(getter)      if old.respond_to?(getter)
    self.send(setter, v)       unless v.nil?
  }
end