Module: Liner

Defined in:
lib/liner.rb,
lib/liner/base.rb,
lib/liner/version.rb,
lib/liner/hashable.rb,
lib/liner/equalizable.rb,
lib/liner/inspectable.rb,
lib/liner/serializable.rb

Defined Under Namespace

Modules: Base, Equalizable, Hashable, Inspectable, Serializable

Constant Summary collapse

VERSION =
'0.2.2'

Class Method Summary collapse

Class Method Details

.apply!(base, *keys) ⇒ Class

Apply a liner to a given class

Examples:

Person = Class.new
Liner.apply! Person, :name, :occupation # => Person

Parameters:

  • base (Class)

    A class to add a liner to

  • keys (Array)

    An array of symbols or strings which will serve as attributes

Returns:

  • (Class)

    The class with a liner installed



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/liner.rb', line 25

def Liner.apply!(base, *keys)
  keys = keys.map(&:to_sym).uniq

  base.send(:define_singleton_method, :liner_keys) do
    @liner_keys ||= begin
      super() + keys
    rescue NoMethodError
      keys
    end.uniq.freeze
  end

  base.send :include, Liner unless base < Liner

  keys.each do |key|
    unless base.method_defined? key
      base.send(:define_method, key){ liner_get key }
    end
    unless base.method_defined? "#{key}="
      base.send(:define_method, "#{key}="){ |value| liner_set key, value }
    end
  end
  base
end

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Includes liner submodules when included



61
62
63
64
65
# File 'lib/liner.rb', line 61

def self.included(base)
  [Base, Hashable, Equalizable, Inspectable, Serializable].each do |mod|
    base.send :include, mod
  end
end

.liner_keysArray

List of liner attributes as an array of symbols

Returns:

  • (Array)


13
14
15
# File 'lib/liner.rb', line 13

def self.liner_keys
  []
end

.new(*keys) ⇒ Class

Setup an anonymous class with liner keys

Examples:

Liner.new(:foo) # => #<Class:0x007fd0993bab98>

Parameters:

  • keys (Array)

    An array of symbols or strings which will serve as attributes

Returns:

  • (Class)


55
56
57
# File 'lib/liner.rb', line 55

def Liner.new(*keys)
  apply! Class.new, *keys
end