Sha256: 8cf6200cf26a2b1907234e68fabdac553ae1551606e17371aede4db33f64ae8e

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module RailsConnector

  class ObjDataFromService < ObjData
    def initialize(data)
      @data = data
    end

    def value_and_type_of(attribute_name)
      return value_and_type_of_widget_pool if attribute_name == BasicObj::WIDGET_POOL_ATTRIBUTE_NAME

      value_and_type = @data[attribute_name]

      if value_and_type.blank?
        if INTERNAL_KEYS.include?(attribute_name) || SPECIAL_KEYS.include?(attribute_name)
          type = type_of_internal(attribute_name)
          [default_attribute_value(type), type]
        else
          raise RailsConnectorError.new("Illegal attribute name #{attribute_name}")
        end
      elsif value_and_type.length == 1
        [value_and_type.first, type_of_internal(attribute_name)]
      else
        value_and_type
      end
    end

    def has_custom_attribute?(attribute_name)
      is_custom_attribute?(attribute_name) && !!@data[attribute_name]
    end

    private

    def value_and_type_of_widget_pool
      [value_of_widget_pool, nil]
    end

    def value_of_widget_pool
      raw_value_of_widget_pool.dup.tap do |hash|
        hash.each_pair do |widget_id, raw_widget_data|
          hash[widget_id] = self.class.new(raw_widget_data)
        end
      end
    end

    def raw_value_of_widget_pool
      @data[BasicObj::WIDGET_POOL_ATTRIBUTE_NAME].first
    end

    def is_custom_attribute?(attribute_name)
      !attribute_name.starts_with?('_') && !SPECIAL_KEYS.include?(attribute_name)
    end

    internal_key_list = %w[
      last_changed
      permalink
      sort_key1
      sort_key2
      sort_key3
      sort_order
      sort_type1
      sort_type2
      sort_type3
      suppress_export
      text_links
      valid_from
      valid_until
    ]

    INTERNAL_KEYS = Set.new(internal_key_list.map { |name| "_#{name}" } )

    SPECIAL_KEYS = Set.new(%w[ body title blob ])

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
infopark_cloud_connector-7.0.2 lib/rails_connector/obj_data_from_service.rb