Sha256: 693cc2272f581c047f53305a28e598601d079793948afe61e36d9a41102f7bc8

Contents?: true

Size: 863 Bytes

Versions: 4

Compression:

Stored size: 863 Bytes

Contents

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

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

    def self.find(string)
      [:primary_code, :secondary_code, :names, :slugs, :symbol].reduce(nil) do |m, method|
        if found = find_by(method, string)
          return found
        end
      end
    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 initialize(attrs)
      attrs.each do |k, v|
        public_send :"#{k}=", v
      end
    end

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

    def slugs
      names.map(&:to_slug)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
unitwise-0.2.2 lib/unitwise/base.rb
unitwise-0.2.1 lib/unitwise/base.rb
unitwise-0.2.0 lib/unitwise/base.rb
unitwise-0.1.0 lib/unitwise/base.rb