Sha256: 624f0768c5042d1e151117b87b1e52db9f50e673fb011dc04a23e070bdfe9e54

Contents?: true

Size: 838 Bytes

Versions: 3

Compression:

Stored size: 838 Bytes

Contents

module GitHubRecordsArchiver
  module DataHelper
    attr_writer :data

    def method_missing(method_sym, *arguments, &block)
      return data[method_sym] if data_key?(method_sym)
      if method_sym.to_s.end_with? '?'
        !send(non_predicate_method(method_sym)).to_s.empty?
      else
        super
      end
    end

    def respond_to_missing?(method_sym, include_private = false)
      if data_key? non_predicate_method(method_sym)
        true
      else
        super
      end
    end

    def to_h
      data.to_h
    end
    alias as_json to_h

    def to_json
      as_json.to_json
    end

    def data
      raise 'Not implemented'
    end

    private

    def data_key?(key)
      data && data.key?(key)
    end

    def non_predicate_method(method_sym)
      method_sym.to_s.gsub(/\?\z/, '').to_sym
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
github_records_archiver-0.3.1 lib/github_records_archiver/data_helper.rb
github_records_archiver-0.2.0 lib/github_records_archiver/data_helper.rb
github_records_archiver-0.1.0 lib/github_records_archiver/data_helper.rb