Sha256: b0d4839a226433e135e60a9bb95f95a123a2e12e81fa8e22d2fb6e14da2343f0

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

class String
  def slugify
    self.dup.slugify!
  end
  
  def slugify!
    self.gsub!(/[^\x00-\x7F]+/, '') # Remove non-ASCII (e.g. diacritics).
    self.gsub!(/[^a-z0-9\-_\+]+/i, '-') # Turn non-slug chars into the separator.
    self.gsub!(/-{2,}/, '-') # No more than one of the separator in a row.
    self.gsub!(/^-|-$/, '') # Remove leading/trailing separator.
    self.downcase!
    self
  end
  
  def make_attr
    self.downcase.to_sym
  end
  
  def indents(remove_blanks = false)
    # clear blank lines and then clear left margin
    reject { |line| line.blank? && remove_blanks }.map { |line| line.gsub(/^\s+$/, "\n") }.join.margin.strip
  end
end

class Symbol
  def html
    (self.to_s + '_html').to_sym
  end
  
  def make_attr
    self.to_s.downcase.to_sym
  end
end

class Array
  def make_attrs
    self.collect! { |a| a.to_s.downcase.to_sym }
  end
end

### class instance vars that inherit down to children
module ClassLevelInheritableAttributes
  
  PRIMITIVES = [NilClass, TrueClass, FalseClass, Fixnum, Float]
  
  def cattr_inheritable(*args)
    @cattr_inheritable_attrs ||= [:cattr_inheritable_attrs]
    @cattr_inheritable_attrs += args
    args.each do |arg|
      class_eval %(
        class << self; attr_accessor :#{arg}; end
      )
    end
    @cattr_inheritable_attrs
  end

  def inherited(subclass)
    @all_children ||= []
    @cattr_inheritable_attrs.each do |inheritable_attribute|
      instance_var = "@#{inheritable_attribute}" 
      current_value = instance_variable_get(instance_var)
      current_value = current_value.dup unless PRIMITIVES.include?(current_value.class)
      subclass.instance_variable_set(instance_var, current_value)
    end
    @all_children << subclass
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
myobie-turbine-core-0.4.0 lib/turbine-core/ext.rb
myobie-turbine-core-0.5.1 lib/turbine-core/ext.rb
myobie-turbine-core-0.5.2 lib/turbine-core/ext.rb
myobie-turbine-core-0.5.3 lib/turbine-core/ext.rb