Class: OpenTraits

Inherits:
Object show all
Defined in:
lib/traits.rb,
lib/traits-0.10.0.rb

Instance Method Summary collapse

Constructor Details

#initialize(h = {}, &b) ⇒ OpenTraits

–{{{



925
926
927
928
# File 'lib/traits.rb', line 925

def initialize h = {}, &b
  h.each{|k,v| trait k => v}
  instance_eval &b if b
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object



930
931
932
933
934
935
936
937
938
939
940
# File 'lib/traits.rb', line 930

def method_missing m, *a, &b
  m = m.to_s.delete '='
  unless respond_to? m
    if a.empty?
      b ? trait(m, &b) : trait(m)
    else
      b ? trait(m => a.shift, &b) : trait(m => a.shift)
    end
  end
  send m
end

Instance Method Details

#inspect(*a, &b) ⇒ Object



951
952
953
# File 'lib/traits.rb', line 951

def inspect *a, &b
  to_hash.inspect *a, &b
end

#to_hashObject Also known as: to_h



942
943
944
# File 'lib/traits.rb', line 942

def to_hash
  rtraits.inject({}){|h,t| h.update t => send(t)}
end

#to_s(*a, &b) ⇒ Object



947
948
949
# File 'lib/traits.rb', line 947

def to_s *a, &b
  to_hash.to_s *a, &b
end

#to_yaml(*a, &b) ⇒ Object



955
956
957
# File 'lib/traits.rb', line 955

def to_yaml *a, &b
  to_hash.to_yaml *a, &b
end