Skip to content
Browse files

Use #nested! method instead of attribute methods.

  • Loading branch information...
1 parent 13c3800 commit 370b76b00a0163a14dec2e5f9092fcc9f38d50f1 @trans trans committed
Showing with 24 additions and 18 deletions.
  1. +24 −18 lib/ostruct2.rb
View
42 lib/ostruct2.rb
@@ -42,25 +42,32 @@ class << self
# o = OpenStruct2.renew
# o.a #=> #<OpenStruct2: {}>
#
- def renew(data=nil)
+ def auto(data=nil)
leet = lambda{ |h,k| new(&leet) }
new(&leet)
end
#
- # Original name for #renew method.
+ # Another name for #auto method.
#
- alias :auto :renew
+ # TODO: Still wondering waht the best name is for this.
+ #
+ alias :renew :auto
#
+ # Create a nested OpenStruct, such that all sub-hashes
+ # added to the table also become OpenStruct objects.
+ #
def nested(data=nil)
o = new
- o.nested = true
+ o.nested!(true)
o.update!(data) if data
o
end
#
+ # Shorter name for `nested`.
+ #
alias :nest :nested
#
@@ -68,16 +75,16 @@ def nested(data=nil)
#
def cascade(data=nil)
o = renew
- o.nested = true
+ o.nested!(true)
o.update!(data) if data
o
end
private
+
def const_missing(name)
::Object.const_get(name)
end
-
end
#
@@ -99,18 +106,6 @@ def __class__
end
#
- # Is the OpenStruct in nested mode?
- #
- def is_nested?
- @nested
- end
-
- #
- def nested=(boolean)
- @nested = !!boolean
- end
-
- #
# Duplicate underlying table when OpenStruct is duplicated or cloned.
#
# @param [OpenStruct] original
@@ -146,6 +141,17 @@ def method_missing(sym, *args, &blk)
end
#
+ # Get/set nested flag.
+ #
+ def nested!(boolean=nil)
+ if boolean.nil?
+ @nested
+ else
+ @nested = !!boolean
+ end
+ end
+
+ #
# CRUD method for listing all keys.
#
def keys!

0 comments on commit 370b76b

Please sign in to comment.
Something went wrong with that request. Please try again.