Sha256: 32721d71a6c9109ae4649d9e5a8a3bb09777b18432ddd47e7f891ff8e915ca4c

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

module Apstrings
  require 'json'
  class DotStringFile
    attr_accessor :kv_pairs
    attr_accessor :raw_file
    def initialize(raw_file='')
      @kv_pairs = []
      @raw_file = raw_file
    end

    def key_values
      kv_pairs.map do |pair|
        {pair.key => pair.value}
      end
    end

    def keys
      kv_pairs.map do |pair|
        pair.key
      end
    end

    def values
      kv_pairs.map do |pair|
        pair.value
      end
    end

    def comments(args={})
      with_keys = args[:with_keys].nil? ? true : args[:with_keys]
      cleaned_pairs = kv_pairs.map do |pair|
        pair
      end
      with_keys ? build_comment_hash(cleaned_pairs) : cleaned_pairs.map(&:comment)
    end

    def to_hash(args={})
      with_comments = args[:with_comments].nil? ? true : args[:with_comments]

      build_hash { |hash, pair|
      hash_value = with_comments ? { pair.value => pair.comment } : pair.value
        hash[pair.key] = hash_value
      }
    end

    def to_json(args={})
      self.to_hash(with_comments: args[:with_comments]).to_json
    end

    private

      def build_comment_hash(kv_pairs)
        build_hash { |hash, pair|
          hash[pair.key] = pair.comment
        }
      end

      def build_hash(&block)
        hash = {}
        kv_pairs.each do |pair|
          yield hash, pair
        end
        hash
      end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
apstrings-0.4.1 lib/apstrings/dot_string_file.rb
apstrings-0.4.0 lib/apstrings/dot_string_file.rb
apstrings-0.3.9 lib/apstrings/dot_string_file.rb
apstrings-0.3.8 lib/apstrings/dot_string_file.rb
apstrings-0.3.7 lib/apstrings/dot_string_file.rb