<commit_before>class Syphon::Api::ModelProxy

  class << self

    attr_reader :model

    def new_class(*args)
      Class.new(self).init(*args)
    end

    def init(resource)
      @model = resource.model_class
      @pkey  = resource.primary_key
      @decorator = Syphon::Api::ModelDecorator.new_class(resource)
      self
    end

    # TODO: limit query to included columns
    #
    def all(conditions = [])
      conditions ||= []

      # query conditions must come first
      conditions.sort_by! { |v| v.is_a?(Hash) ? 0 : 1 }

      results = conditions.reduce(@model.all) do |query, cond|
        case cond
        when Hash
          cond.reduce(query) { |query, (cond, val)| query.send(cond, val) }
        else
          query.send(cond)
        end
      end

      results.map { |o| wrap o }
    end

    def find(id)
      wrap find_by_pkey(id)
    end

    def create(attributes = {})
      obj = @model.create(attributes)
      obj.valid? ? find(obj.send(@pkey)) : obj
    end

    def update(id, attributes = {})
      find_by_pkey(id).update_attributes(attributes)
    end

    def destroy(id)
      find_by_pkey(id).destroy
    end

    def wrap(object)
      object && @decorator.wrap(object)
    end

    #TODO: why is this class a singleton? refactor it to a regular class and
    # use new here
    #
    def with_model(model)
      new_proxy = self.clone
      new_proxy.instance_variable_set('@model', model)
      new_proxy
    end

  private

    def find_by_pkey(id)
      @model.send("find_by_#{@pkey}", id)
    end

  end
end
<commit_msg>adding support for show action without id<commit_after>class Syphon::Api::ModelProxy

  class << self

    attr_reader :model

    def new_class(*args)
      Class.new(self).init(*args)
    end

    def init(resource)
      @model = resource.model_class
      @pkey  = resource.primary_key
      @decorator = Syphon::Api::ModelDecorator.new_class(resource)
      self
    end

    # TODO: limit query to included columns
    #
    def all(conditions = [])
      conditions ||= []

      # query conditions must come first
      conditions.sort_by! { |v| v.is_a?(Hash) ? 0 : 1 }

      results = conditions.reduce(@model.all) do |query, cond|
        case cond
        when Hash
          cond.reduce(query) { |query, (cond, val)| query.send(cond, val) }
        else
          query.send(cond)
        end
      end

      results.map { |o| wrap o }
    end

    def find(id)
      wrap find_by_pkey(id)
    end

    def create(attributes = {})
      obj = @model.create(attributes)
      obj.valid? ? find(obj.send(@pkey)) : obj
    end

    def update(id, attributes = {})
      find_by_pkey(id).update_attributes(attributes)
    end

    def destroy(id)
      find_by_pkey(id).destroy
    end

    def wrap(object)
      object && @decorator.wrap(object)
    end

    #TODO: why is this class a singleton? refactor it to a regular class and
    # use new here
    #
    def with_model(model)
      new_proxy = self.clone
      new_proxy.instance_variable_set('@model', model)
      new_proxy
    end

  private

    def find_by_pkey(id)
      id ? @model.send("find_by_#{@pkey}", id) : @model.first
    end

  end
end

{'fraction_non_alphanumeric': 0.10895428929242329, 'fraction_numerical': 0.0012523481527864746, 'mean_word_length': 2.2735655737704916, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '6513601', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1031, 'n_words': 254}