lib/blobject.rb in blobject-0.1.0 vs lib/blobject.rb in blobject-0.1.1

- old
+ new

@@ -1,5 +1,9 @@ +def blobject *parameters, &block + Blobject.new *parameters, &block +end + # similar to OpenStruct # b.foo.bar = 8 automatically creates a blobject called "foo" on "b" # to check whether a variable is defined use the '?' syntax i.e: b.is_it_here? => nil, b.foo? == b.foo # calling an unassigned member returns a new blobject unless the blobject is frozen # a frozen blobject cannot be assigned new members or populated from a hash @@ -23,14 +27,24 @@ end def defrost unfreeze end - def initialize hash_of_initial_values={} + def initialize hash_of_initial_values_or_an_object={}, array_of_methods_to_copy_data_from_object=[] @values = {} - merge_hash hash_of_initial_values - yield self if block_given? + + if (hash_of_initial_values_or_an_object.class == Hash) + merge_hash hash_of_initial_values_or_an_object + else + array_of_methods_to_copy_data_from_object.each do |accessor| + @values[accessor.to_s] = hash_of_initial_values_or_an_object.send accessor + end + end + + if block_given? + yield self + end self end def method_missing(sym, *args, &block) @@ -44,11 +58,13 @@ elsif assignment raise AssignToFrozenBlobjectException if @blobject_frozen @values[assignment.to_s] = args[0] else # return the value or a new blobject - return @values[str] unless @values[str].nil? + value = @values[str] + + return value unless value.nil? return nil if frozen? @values[str] = Blobject.new return @values[str] end end @@ -64,11 +80,11 @@ if value.class==Array value = Blobject.blobjectify_array value end - @values[key] = value + @values[key.to_s] = value end self end def to_hash @@ -134,8 +150,22 @@ def []= key, value @values[key] = value end def blank? - @values.blank? + empty? end + + def empty? + @values.empty? || @values.values.empty? || !@values.values.any? do |v| + # if the value is a Blobject, Hash or Array return + # true if it is not empty. + # else just return true, the value is regarded as not empty. + if [Blobject, Array, Hash].include?(v.class) + !v.empty? + else + true + end + end + end + end \ No newline at end of file