Sha256: c3370ad38f2dba3f6c719d284531670cc0fb55a1ff3116b3e13c3745a3a2bf3a

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true
module Faml
  module ObjectRef
    class << self
      def render(ref = nil, prefix = nil, *)
        h = {}
        if ref.nil?
          return h
        end
        c = class_name(ref)
        i = "#{c}_#{id(ref) || 'new'}"
        if prefix
          c = "#{prefix}_#{c}"
          i = "#{prefix}_#{i}"
        end
        { id: i, class: c }
      end

      private

      def class_name(ref)
        if ref.respond_to?(:haml_object_ref)
          ref.haml_object_ref
        else
          underscore(ref.class)
        end
      end

      def id(ref)
        if ref.respond_to?(:to_key)
          key = ref.to_key
          if key
            key.join('_')
          end
        else
          ref.id
        end
      end

      def underscore(camel_cased_word)
        word = camel_cased_word.to_s.dup
        word.gsub!(/::/, '_')
        word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
        word.tr!('-', '_')
        word.downcase!
        word
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
faml-0.8.1 lib/faml/object_ref.rb
faml-0.8.0 lib/faml/object_ref.rb