Sha256: 450b65ccb78bb593773450fe113837a323a848d03ad871bab2f044bf1dbf11ee

Contents?: true

Size: 915 Bytes

Versions: 15

Compression:

Stored size: 915 Bytes

Contents

module SportsDataApi
  class JsonData
    def initialize(json, override_var_name = nil)
      ivar = set_data_key(override_var_name)
      json.each_pair do |attr_name, attr_value|
        ivar[attr_name.to_sym] = attr_value
      end
    end

    def [](key)
      return send(key) if respond_to?(key)
      self.instance_variable_get("@#{instance_var_name}")[key]
    end

    private

    attr_reader :instance_var_name

    def set_data_key(override_var_name)
      var_name = override_var_name || class_name
      @instance_var_name = var_name
      self.class.class_eval { attr_reader :"#{var_name}" }
      self.instance_variable_set("@#{instance_var_name}", {})
    end

    def class_name
      @class_name ||= self
        .class
        .name
        .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        .gsub(/([a-z\d])([A-Z])/, '\1_\2')
        .split('::')
        .last
        .downcase
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sports_data_api-0.15.3 lib/sports_data_api/json_data.rb
sports_data_api-0.15.2 lib/sports_data_api/json_data.rb
sports_data_api-0.15.1 lib/sports_data_api/json_data.rb
sports_data_api-0.15.0 lib/sports_data_api/json_data.rb
sports_data_api-0.14.1 lib/sports_data_api/json_data.rb
sports_data_api-0.14.0 lib/sports_data_api/json_data.rb
sports_data_api-0.13.0 lib/sports_data_api/json_data.rb
sports_data_api-0.12.1 lib/sports_data_api/json_data.rb
sports_data_api-0.12.0 lib/sports_data_api/json_data.rb
sports_data_api-0.11.6 lib/sports_data_api/json_data.rb
sports_data_api-0.11.5 lib/sports_data_api/json_data.rb
sports_data_api-0.11.3 lib/sports_data_api/json_data.rb
sports_data_api-0.11.2 lib/sports_data_api/json_data.rb
sports_data_api-0.11.1 lib/sports_data_api/json_data.rb
sports_data_api-0.11.0 lib/sports_data_api/json_data.rb