Sha256: 81cfba6bb139fa9c57fc7e6f56bdec55ab4ef8aada305b46e2597b71be97ad66

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

module Ooz
  module Common
    class BaseClass
      class << self

        def overridable_const (method, value)
          method = method.to_s
          define_singleton_method method do
            value
          end
          define_method method do
            self.class.send(method)
          end
        end

        def passthrough(*methods, to:)
          methods.each do |method|
            method = method.to_s
            define_method method do
              send(to)[method]
            end
            define_method "#{method}=" do |value|
              send(to)[method] = value
            end
          end
        end

        def passthrough_cls(method, to:, klass:)
          method = method.to_s
          var = "@#{method}"

          define_method method do
            return instance_variable_get(var) if instance_variable_defined?(var)
            send(to)[method] = send(to)[method] || {}

            klass = klass.is_a?(Class)? klass : self.send(klass)
            obj = klass.new(send(to)[method], root: self)
            instance_variable_set(var, obj)
            obj
          end
        end

        def passthrough_arr(method, to:, klass:)
          method = method.to_s
          var = "@#{method}"

          define_method method do
            return instance_variable_get(var) if instance_variable_defined?(var)
            send(to)[method] = send(to)[method] || []

            klass = klass.is_a?(Class)? klass : self.send(klass)
            arr = send(to)[method].map do |e|
              klass.new(e, root: self)
            end
            instance_variable_set(var, arr)
            arr
          end
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ooze-parser-0.1.11 lib/ooz/common/base_class.rb
ooze-parser-0.1.10 lib/ooz/common/base_class.rb
ooze-parser-0.1.9 lib/ooz/common/base_class.rb
ooze-parser-0.1.8 lib/ooz/common/base_class.rb
ooze-parser-0.1.7 lib/ooz/common/base_class.rb