Sha256: 96eed0de11dd2b9fa98e19b3b35175e61a59f3d56925eebd0a89d9edf21fde4d

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 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
      #require "pry"; binding.pry
      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
      klass = Object.const_set(class_name,Class.new)
      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, values[i])
          end
        end
      end
      obj = klass.new
    end


  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
icecream-0.0.4 lib/icecream/parser.rb
icecream-0.0.3 lib/icecream/parser.rb
icecream-0.0.2 lib/icecream/parser.rb