Sha256: 81ece1621ca288b4251376b0b2b6d48a5e88081513ddff4e9050eb819d9c5e9d

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

require 'yaml'
module AccessibleSeedYaml
  # This class is wrapper of seed for one record.
  #   Functions:
  #    - Fetch seed attributes by hash.
  #    - Stored original seed string. It can fetch anytime.
  class Record
    attr_reader :original_seed
    
    # @param [String] seed_for_one_record seed string for one record.
    # @raise [ArgumentError] if argument has not only one key then raise.
    # @example
    #  <Correct>
    #  ````
    #  data1:
    #    id: 1
    #    name: "one"
    #  ````
    #   => this is one record.
    #
    #  <Wrong>
    #  ````
    #  data1:
    #    id: 1
    #    name: "one"
    #  data2:
    #    id: 2
    #    name: "two"
    #  ````
    #   => this is tow record.
    #
    def initialize(seed_for_one_record)
      @original_seed = seed_for_one_record
      exchange_to_hash
    end
    
    # @return [Hash] attributes of seed data by hash.
    # @example
    #  <source>
    #  data1:
    #    id: 1
    #    name: "one"
    #
    #  <return>
    #  {"id" => 1, "name" => "one"}
    def attributes
      @seed_data_by_hash.values.first
    end
    
    # @return [String] record key
    # @example
    #  <source>
    #  data1:
    #    id: 1
    #    name: "one"
    #
    #  <return>
    #  "data1"
    def key
      @seed_data_by_hash.keys.first
    end
    
    # @return [String] this record data by yaml string
    # @example
    #  <source>
    #  "data1:\n  id: 1\n  name: "one"
    #
    #  <return>
    #  "data1:\n  id: 1\n  name: "one"
    def to_s
      self.original_seed
    end
    
    private
    
    def exchange_to_hash
      @seed_data_by_hash ||= YAML.load(@original_seed)
      
      unless @seed_data_by_hash.size == 1
        raise ArgumentError.new('original_seed key size is not 1.')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
accessible_seed_yaml-1.0.2 lib/accessible_seed_yaml/record.rb
accessible_seed_yaml-1.0.1 lib/accessible_seed_yaml/record.rb