Class: CRStruct::Open
- Inherits:
-
Object
- Object
- CRStruct::Open
- Defined in:
- lib/crstruct/open.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#free?(k) ⇒ Boolean
free?, set!, and get? ### make purposefull access to @h possible, and easier to subclass CRStruct.
- #get?(k) ⇒ Boolean
-
#initialize(h = {}) ⇒ Open
constructor
h should support interface ### raise “need Hash(Symbol,Value)” unless [:to_h, :keys, :has_key?, :[], :[]=].all?{|_|h.respond_to?_} and h.keys.all?{|_|_.is_a? Symbol}.
- #method_missing(key, *args, &proc) ⇒ Object
- #set!(k, v) ⇒ Object
-
#to_h ⇒ Object
Can it somehow be converted into a Hash? Most likely.
Constructor Details
#initialize(h = {}) ⇒ Open
h should support interface ### raise “need Hash(Symbol,Value)” unless
[:to_h, :keys, :has_key?, :[], :[]=].all?{|_|h.respond_to?_} and
h.keys.all?{|_|_.is_a? Symbol}
7 8 9 |
# File 'lib/crstruct/open.rb', line 7 def initialize(h={}) @h = h end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *args, &proc) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/crstruct/open.rb', line 33 def method_missing(key, *args, &proc) if proc.nil? case args.length when 0 return get?(key) if @h.has_key? key when 1 if key=~/^\w+=$/ k = key[0..-2].to_sym return set!(k, args[0]) if free?(k) end end end super end |
Instance Method Details
#free?(k) ⇒ Boolean
free?, set!, and get? ### make purposefull access to @h possible, and easier to subclass CRStruct.
23 24 25 |
# File 'lib/crstruct/open.rb', line 23 def free?(k) !(@h.has_key?(k) or respond_to?(k)) end |
#get?(k) ⇒ Boolean
29 30 31 |
# File 'lib/crstruct/open.rb', line 29 def get?(k) @h[k] end |
#set!(k, v) ⇒ Object
26 27 28 |
# File 'lib/crstruct/open.rb', line 26 def set!(k, v) @h[k]=v end |
#to_h ⇒ Object
Can it somehow be converted into a Hash? Most likely.
13 14 15 |
# File 'lib/crstruct/open.rb', line 13 def to_h @h.to_h end |