class Object

Public Instance Methods

define_first_level_nesting_methods_for_property(root, propname) click to toggle source

HELPER METHODS

# File lib/nested_accessors.rb, line 48
def define_first_level_nesting_methods_for_property(root, propname)
  class_eval <<-RUBY, __FILE__, __LINE__+1
    def #{propname}=(val)
      #{root}.store("#{propname}", val.to_s)
    end

    def #{propname}
      #{root}.store("#{propname}", nil) unless (#{root}.has_key?("#{propname}"))
      #{root}.fetch "#{propname}"
    end
  RUBY
end
define_first_level_nesting_methods_for_subroot(root, subroot, subroot_type, propnames=nil) click to toggle source
# File lib/nested_accessors.rb, line 61
def define_first_level_nesting_methods_for_subroot(root, subroot, subroot_type, propnames=nil)
  subroot_value = case subroot_type.to_s
    when "Array" then []
    else {}
  end

  class_eval <<-RUBY, __FILE__, __LINE__+1
    def #{subroot}
      #{root}.store("#{subroot}", #{subroot_value}) unless (#{root}.has_key?("#{subroot}") and #{root}.fetch("#{subroot}").is_a?(#{subroot_value}.class))
      #{root}.fetch "#{subroot}"
    end
  RUBY

  if propnames
    propnames.each do |a_propname|
      self.class_eval <<-RUBY, __FILE__, __LINE__+1
        def #{subroot}_#{a_propname}=(val)
          #{subroot}.store("#{a_propname}".to_s, val.to_s)
        end

        def #{subroot}_#{a_propname}
          #{subroot}.store("#{a_propname}", nil) unless (#{subroot}.has_key?("#{a_propname}"))
          #{subroot}.fetch "#{a_propname}".to_s
        end
      RUBY
    end
  end
end
define_second_level_nesting_methods(subroot, subsubroot, propnames) click to toggle source
# File lib/nested_accessors.rb, line 90
def define_second_level_nesting_methods(subroot, subsubroot, propnames)
  self.class_eval <<-RUBY, __FILE__, __LINE__+1
    def #{subroot}_#{subsubroot}
      #{subroot}.store("#{subsubroot}", {}) unless (#{subroot}.has_key?("#{subsubroot}") and #{subroot}.fetch("#{subsubroot}").is_a?(Hash))
      #{subroot}.fetch "#{subsubroot}"
    end
  RUBY

  if propnames
    propnames.each do |a_propname|
      self.class_eval <<-RUBY, __FILE__, __LINE__+1
        def #{subroot}_#{subsubroot}_#{a_propname}=(val)
          #{subroot}_#{subsubroot}.store("#{a_propname}", val.to_s)
        end

        def #{subroot}_#{subsubroot}_#{a_propname}
          #{subroot}_#{subsubroot}.store("#{a_propname}", nil) unless (#{subroot}_#{subsubroot}.has_key?("#{a_propname}"))
          #{subroot}_#{subsubroot}.fetch "#{a_propname}"
        end
      RUBY
    end
  end
end