Sha256: 9302ca3fbf4e9f96243641214faac9d69bb1c10eaad600ab057d33cad92569c2

Contents?: true

Size: 1.72 KB

Versions: 4

Compression:

Stored size: 1.72 KB

Contents

module Representable
  # Parse strategies are just a combination of representable's options. They save you from memoizing the
  # necessary parameters.
  #
  # Feel free to contribute your strategy if you think it's worth sharing!
  class ParseStrategy
    def self.apply!(options)
      return unless strategy = options[:parse_strategy]

      strategy = :proc if strategy.is_a?(::Proc)

      parse_strategies[strategy].apply!(name, options)
    end

    def self.parse_strategies
      {
        :sync                 => Sync,
        :find_or_instantiate  => FindOrInstantiate,
        :proc                 => Proc
      }
    end


    # Using a lambda as parse_strategy does not set the parsed property for you.
    class Proc
      def self.apply!(name, options)
        options[:setter]       = lambda { |*| }
        options[:pass_options] = true
        options[:instance]     = options[:parse_strategy]
      end
    end


    class Sync
      def self.apply!(name, options)
        options[:setter]       = lambda { |*| }
        options[:pass_options] = true
        options[:instance]     = options[:collection] ?
          lambda { |fragment, i, options| options.binding.get[i] } :
          lambda { |fragment, options|    options.binding.get }
      end
    end


    # replaces current collection.
    class FindOrInstantiate
      def self.apply!(name, options)
        options[:pass_options] = true
        options[:instance]     = lambda { |fragment, *args|
          args = args.last # TODO: don't pass i as separate block parameter but in Options.
          object_class = args.binding[:class].evaluate(self, fragment, args)

          object_class.find_by(id: fragment["id"]) or object_class.new
        }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
representable-2.1.3 lib/representable/parse_strategies.rb
representable-2.1.1 lib/representable/parse_strategies.rb
representable-2.1.0 lib/representable/parse_strategies.rb
representable-2.0.4 lib/representable/parse_strategies.rb