Module: CSquare::Hashable::SingletonMethods

Defined in:
lib/csquare.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_hashable(array_name) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/csquare.rb', line 240

def acts_as_hashable(array_name)
  unless self.is_a?(ClassMethods)
    #include InstanceMethods
    # extend ClassMethods

    # Add a to_h function which returns a hash of variables and their types.
    module_eval <<-"end_eval", __FILE__, __LINE__
      def to_h(as=nil)
        h = {}
        self.#{array_name}.each do |obj|
          if as == :string
            h[obj.name] = obj.type.CustomType? || obj.type.Struct? ? obj.type.name : obj.type.to_s
          else
            h[obj.name] = obj.type
          end
        end
        h
      end
    end_eval

  end
end