Sha256: 16cd776c393733ff7ca1f5fc0d6117a88ea799628cff25fe24f5169b616ef30f

Contents?: true

Size: 976 Bytes

Versions: 1

Compression:

Stored size: 976 Bytes

Contents

require 'yaml'
module Unitwise
  class Base
    liner :names, :primary_code, :secondary_code, :symbol, :scale

    def self.all
      @all ||= data.map{|d| self.new d }
    end

    def self.find(term)
      all.find { |i| i.search_strings.any? { |string| string == term } }
    end

    def self.find_by(method, string)
      self.all.find do |i|
        key = i.send(method)
        if key.respond_to?(:each)
          key.include?(string)
        else
          key == string
        end
      end
    end

    def self.search(term)
      self.all.select do |i|
        i.search_strings.any? { |string| string =~ /#{term}/i }
      end
    end

    def names=(names)
      @names = Array(names)
    end

    def slugs
      names.map do |n|
        n.downcase.strip.gsub(/\s/, '_').gsub(/\W/, '')
      end
    end

    def search_strings
      [primary_code, secondary_code, names, slugs, symbol].flatten.compact
    end

    def to_s
      primary_code
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unitwise-0.3.2 lib/unitwise/base.rb