Sha256: 6b38a59c286d8a4d60d8bd27ceb4471adf6d4d22174bc588c0ddf1148ea3c6d7

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

module IceCream
  class Parser

    def self.get_flavor path
      all_particularities = File.read(path).split("\n").map
      objectify get_flavor_name(path), all_particularities
    end

    private

    def self.get_flavor_name path
        title = slice_between_strings(path, "/flavors/", ".flavor")
    end

    def self.parse_variables particularity
      particularity.split("=").first.strip.to_s
    end

    def self.parse_values particularity
      particularity.split("=").last.strip
    end
    def self.slice_between_strings(string, str_start, str_end)
      start_at = string.index(str_start).to_i + str_start.size
      end_at = string.index(str_end)
      string = string.slice start_at..end_at-1
    end
    
    def self.fix_value value
      if value[0] == ":"
        final = value.gsub(":","").to_sym 
      elsif !value.slice("\"").nil?
        final = value.gsub("\"","") 
      elsif !value.slice(".").nil?
        final = value.to_f if Float(value) rescue false
      elsif !((Integer(value) rescue nil) == nil)
        final = value.to_i
      else
        final = ""
      end
      final
      
    end
    def self.objectify flavor, all_particularities
      class_name = flavor.capitalize

      original_verbosity = $VERBOSE
      $VERBOSE = nil

      klass = klass || Object.const_set(class_name,Class.new)

      $VERBOSE = original_verbosity

      variables = all_particularities.each { | particularity | parse_variables particularity }
      values = all_particularities.each { | particularity | parse_values particularity }
      

      
      klass.class_eval do
        attr_accessor *variables

        define_method(:initialize) do
          variables.each_with_index do |variable_name,i|
            instance_variable_set("@"+variable_name, Parser.fix_value(values[i]))
          end
        end
      end
      obj = klass.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
icecream-0.0.7 lib/icecream/parser.rb