Sha256: cc3419b6ff0516863620495f0bfd6b7783ac2805b56d04169fc8e4003f5d8d5d

Contents?: true

Size: 882 Bytes

Versions: 75

Compression:

Stored size: 882 Bytes

Contents

# encoding: utf-8

# implement to_hash(instance_variable_name => key, instance_variable_value => value)
module AttributesHashable
  # create hash(instance_variable_name => key, instance_variable_value => value)
  #
  # class Hoge have two_attributes(:hoge, :hige)
  #   class Hoge
  #     include AttributesInitializable
  #     attr_accessor_init :hoge, :hige
  #     include AttributesHashable
  #   end
  #
  #   hoge = Hoge.new do |h|
  #     h.hoge = 'hoge'
  #     h.hige = 'hige'
  #   end
  #
  #   hoge.to_hash # => {:hoge=>"hoge", :hige=>"hige"}
  #
  # After include AttributesHashable, you can use Hash.try_convert.
  #   Hash.try_convert hoge # => {:hoge=>"hoge", :hige=>"hige"}
  def to_hash
    instance_variables.reduce({}) do |hash, var|
      hash[var.to_s.delete('@').to_sym] = instance_variable_get(var)
      hash
    end
  end
end

Version data entries

75 entries across 75 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.93 lib/attributes_hashable.rb
tbpgr_utils-0.0.92 lib/attributes_hashable.rb
tbpgr_utils-0.0.91 lib/attributes_hashable.rb
tbpgr_utils-0.0.90 lib/attributes_hashable.rb
tbpgr_utils-0.0.89 lib/attributes_hashable.rb
tbpgr_utils-0.0.88 lib/attributes_hashable.rb
tbpgr_utils-0.0.87 lib/attributes_hashable.rb
tbpgr_utils-0.0.86 lib/attributes_hashable.rb
tbpgr_utils-0.0.85 lib/attributes_hashable.rb
tbpgr_utils-0.0.84 lib/attributes_hashable.rb
tbpgr_utils-0.0.83 lib/attributes_hashable.rb
tbpgr_utils-0.0.82 lib/attributes_hashable.rb
tbpgr_utils-0.0.81 lib/attributes_hashable.rb
tbpgr_utils-0.0.80 lib/attributes_hashable.rb
tbpgr_utils-0.0.79 lib/attributes_hashable.rb
tbpgr_utils-0.0.78 lib/attributes_hashable.rb
tbpgr_utils-0.0.77 lib/attributes_hashable.rb
tbpgr_utils-0.0.76 lib/attributes_hashable.rb
tbpgr_utils-0.0.75 lib/attributes_hashable.rb
tbpgr_utils-0.0.74 lib/attributes_hashable.rb