Sha256: 8d07b1873412d89c19257772d288b57985a9c5dae37338a7b2ae9caaf42fccf9

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

module Rocx
  module PropertyBuilder

    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def properties_tag(*args)
        @properties_tag = args.first if args.any?
        @properties_tag
      end

      def value_property(name, as: nil)
        attr_reader name

        properties[name] = (as || name).to_s

        class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name}=(value)
          property_key = "#{name}".to_sym
          class_name = properties[property_key].split("_").map(&:capitalize).join
          prop_class = Rocx::Properties.const_get class_name
          instance_variable_set "@#{name}", prop_class.new(value)
        end
        CODE
      end

      def property(name, as: nil)
        properties[name] = (as || name).to_s

        class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name}
          property_key = "#{name}".to_sym
          class_name = properties[property_key].split("_").map(&:capitalize).join
          prop_class = Rocx::Properties.const_get class_name

          if instance_variable_get("@#{name}").nil?
            instance_variable_set "@#{name}", prop_class.new
          end

          instance_variable_get "@#{name}"
        end
        CODE
      end

      def properties
        @properties ||= {}
      end
    end

  private

    def property_xml(xml)
      props = properties.keys.map(&method(:send)).compact
      return if props.none?(&:render?)

      xml[namespace].public_send(properties_tag) {
        props.each { |prop| prop.to_xml(xml) }
      }
    end

    def properties
      self.class.properties
    end

    def properties_tag
      self.class.properties_tag
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rocx-0.7.0 lib/rocx/property_builder.rb