Sha256: 023e90c4c0a76fe5bf8aac8daf09ab5427b163c846405c3128962e8c11ff4ac2

Contents?: true

Size: 1.02 KB

Versions: 14

Compression:

Stored size: 1.02 KB

Contents

module Receiver
  #
  # adds methods to load and store from json, yaml or magic
  #
  # This will require 'json' UNLESS you have already included something (so if
  # you want to say require 'yajl' then do that first).
  #
  module ActsAsLoadable

    module ClassMethods
      def receive_json stream
        receive(JSON.load(stream))
      end

      def receive_yaml stream
        receive(YAML.load(stream))
      end

      #
      # The file is loaded with
      # * YAML if the filename ends in .yaml or .yml
      # * JSON otherwise
      #
      def receive_from_file filename
        stream = File.open(filename)
        (filename =~ /.ya?ml$/) ? receive_yaml(stream) : receive_json(stream)
      end
    end

    def merge_from_file! filename
      other_obj = self.class.receive_from_file(filename)
      tree_merge! other_obj
    end

    # put all the things in ClassMethods at class level
    def self.included base
      require 'yaml'
      require 'json' unless defined?(JSON)
      base.extend ClassMethods
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
gorillib-0.6.0 old/lib/gorillib/receiver/acts_as_loadable.rb
gorillib-0.5.2 old/lib/gorillib/receiver/acts_as_loadable.rb
gorillib-0.1.11 lib/gorillib/receiver/acts_as_loadable.rb
gorillib-0.1.9 lib/gorillib/receiver/acts_as_loadable.rb
gorillib-0.1.8 lib/gorillib/receiver/acts_as_loadable.rb
gorillib-0.1.7 lib/gorillib/receiver/acts_as_loadable.rb
gorillib-0.1.6 lib/gorillib/receiver/acts_as_loadable.rb
gorillib-0.1.5 lib/gorillib/receiver/acts_as_loadable.rb
gorillib-0.1.4 lib/gorillib/receiver/acts_as_loadable.rb
gorillib-0.1.3 lib/gorillib/receiver/acts_as_loadable.rb
gorillib-0.1.2 lib/gorillib/receiver/acts_as_loadable.rb
gorillib-0.1.1 lib/gorillib/receiver/acts_as_loadable.rb
icss-0.0.4 lib/icss/receiver/acts_as_loadable.rb
gorillib-0.1.0 lib/gorillib/receiver/acts_as_loadable.rb