<commit_before>require 'trello/string'

module Trello
  class BasicData
    include ActiveModel::Validations
    include ActiveModel::Dirty
    include ActiveModel::Serializers::JSON

    class << self
      def find(path, id)
        Client.get("/#{path}/#{id}").json_into(self)
      end
    end

    def self.register_attributes(*names)
      options = { :readonly => [] }
      options.merge!(names.pop) if names.last.kind_of? Hash

      # Defines the attribute getter and setters.
      class_eval do
        define_method :attributes do
          @attributes ||= names.inject({}) { |hash,k| hash.merge(k.to_sym => nil) }
        end

        names.each do |key|
          define_method(:"#{key}") { @attributes[key] }

          unless options[:readonly].include?(key.to_sym)
            define_method :"#{key}=" do |val|
              send(:"#{key}_will_change!") unless val == @attributes[key]
              @attributes[key] = val
            end
          end
        end

        define_attribute_methods names
      end
    end

    def self.many(name, opts = {})
      class_eval do
        define_method(:"#{name}") do |*args|
          options   = opts.dup
          resource  = options.delete(:in)  || self.class.to_s.split("::").last.downcase.pluralize
          klass     = options.delete(:via) || Trello.const_get(name.to_s.singularize.camelize)
          params    = options.merge(args[0] || {})
          resources = Client.get("/#{resource}/#{id}/#{name}", options).json_into(klass)
          MultiAssociation.new(self, resources).proxy
        end
      end
    end

    register_attributes :id, :readonly => [ :id ]

    def initialize(fields = {})
      update_fields(fields)
    end

    def update_fields(fields)<commit_msg>adds one class method for one to one associations<commit_after>require 'trello/string'

module Trello
  class BasicData
    include ActiveModel::Validations
    include ActiveModel::Dirty
    include ActiveModel::Serializers::JSON

    class << self
      def find(path, id)
        Client.get("/#{path}/#{id}").json_into(self)
      end
    end

    def self.register_attributes(*names)
      options = { :readonly => [] }
      options.merge!(names.pop) if names.last.kind_of? Hash

      # Defines the attribute getter and setters.
      class_eval do
        define_method :attributes do
          @attributes ||= names.inject({}) { |hash,k| hash.merge(k.to_sym => nil) }
        end

        names.each do |key|
          define_method(:"#{key}") { @attributes[key] }

          unless options[:readonly].include?(key.to_sym)
            define_method :"#{key}=" do |val|
              send(:"#{key}_will_change!") unless val == @attributes[key]
              @attributes[key] = val
            end
          end
        end

        define_attribute_methods names
      end
    end

    def self.one(name, opts = {})
      class_eval do
        define_method(:"#{name}") do |*args|
          options = opts.dup
          klass   = options.delete(:via) || Trello.const_get(name.to_s.camelize)
          ident   = options.delete(:using) || :id
          klass.find(self.send(ident))
        end
      end
    end

    def self.many(name, opts = {})
      class_eval do
        define_method(:"#{name}") do |*args|
          options   = opts.dup
          resource  = options.delete(:in)  || self.class.to_s.split("::").last.downcase.pluralize
          klass     = options.delete(:via) || Trello.const_get(name.to_s.singularize.camelize)
          params    = options.merge(args[0] || {})
          resources = Client.get("/#{resource}/#{id}/#{name}", options).json_into(klass)
          MultiAssociation.new(self, resources).proxy
        end
      end
    end

    register_attributes :id, :readonly => [ :id ]

    def initialize(fields = {})
      update_fields(fields)
    end

    def update_fields(fields)
{'fraction_non_alphanumeric': 0.13343677269200932, 'fraction_numerical': 0.000517196793379881, 'mean_word_length': 2.2835314091680816, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6115125', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1173, 'n_words': 237}