Module: WingedCouch::CoreExt::ChainAccessor

Defined in:
lib/winged_couch/core_ext/chain_accessor.rb

Overview

See Also:

Instance Method Summary collapse

Instance Method Details

#chain_accessor(attr_name, options = {}, &block) ⇒ Object

Method for defining `chain` accessors

Examples:

class MyClass
  chain_accessor :key1
  chain_accessor :key2, default: 123
  chain_accessor :params, as: :hash
  chain_accessor :options, as: :hash, chain_name: :option
end

i = MyClass.new

i.with_key1("a")
# => #<MyClass:instance key1="a" ...>
i.key1
# => "a"

i.key2
# => 123

i.with_key2("key2).key2
# => "key2"

i.with_params("key", "value")
i.params
# => { "key" => "value" }

i.with_option("key", "value")
# because "chain_value" was passed as "option"
i.options # but options for accessor
# => { "key" => "value" }

41
42
43
44
45
46
47
48
49
50
51
# File 'lib/winged_couch/core_ext/chain_accessor.rb', line 41

def chain_accessor(attr_name, options = {}, &block)

  attr_accessor attr_name

  if options[:as] == :hash
    define_hash_chain_accessor(attr_name, options, &block)
  else
    define_kv_chain_accessor(attr_name, options, &block)
  end

end