Class: Data

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_context/polyfill/data.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **kwargs) ⇒ Data

Returns a new instance of Data.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rails_ai_context/polyfill/data.rb', line 34

def initialize(*args, **kwargs)
  members = self.class.members
  if kwargs.empty?
    unless args.length == members.length
      raise ArgumentError, "wrong number of arguments (given #{args.length}, expected #{members.length})"
    end

    members.each_with_index { |m, i| instance_variable_set(:"@#{m}", args[i]) }
  else
    unless args.empty?
      raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0)"
    end

    extra = kwargs.keys - members
    unless extra.empty?
      raise ArgumentError, "unknown keyword#{extra.length > 1 ? 's' : ''}: #{extra.map(&:inspect).join(', ')}"
    end

    missing = members - kwargs.keys
    unless missing.empty?
      raise ArgumentError, "missing keyword#{missing.length > 1 ? 's' : ''}: #{missing.map(&:inspect).join(', ')}"
    end

    members.each { |m| instance_variable_set(:"@#{m}", kwargs[m]) }
  end
  freeze
end

Class Method Details

.define(*members, &block) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails_ai_context/polyfill/data.rb', line 16

def define(*members, &block)
  members = members.map(&:to_sym)
  raise ArgumentError, "duplicate member" if members.uniq.length != members.length

  subclass = ::Class.new(self)
  subclass.instance_variable_set(:@members, members.freeze)
  members.each do |name|
    subclass.define_method(name) { instance_variable_get(:"@#{name}") }
  end
  subclass.class_eval(&block) if block
  subclass
end

.membersObject



29
30
31
# File 'lib/rails_ai_context/polyfill/data.rb', line 29

def members
  @members || []
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



97
98
99
# File 'lib/rails_ai_context/polyfill/data.rb', line 97

def ==(other)
  other.class == self.class && other.to_h == to_h
end

#deconstructObject



85
86
87
# File 'lib/rails_ai_context/polyfill/data.rb', line 85

def deconstruct
  self.class.members.map { |m| instance_variable_get(:"@#{m}") }
end

#deconstruct_keys(keys) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/rails_ai_context/polyfill/data.rb', line 89

def deconstruct_keys(keys)
  return to_h if keys.nil?

  keys.each_with_object({}) do |k, acc|
    acc[k] = instance_variable_get(:"@#{k}") if self.class.members.include?(k)
  end
end

#hashObject



102
103
104
# File 'lib/rails_ai_context/polyfill/data.rb', line 102

def hash
  [ self.class, to_h ].hash
end

#inspectObject Also known as: to_s



106
107
108
109
# File 'lib/rails_ai_context/polyfill/data.rb', line 106

def inspect
  pairs = self.class.members.map { |m| "#{m}=#{instance_variable_get(:"@#{m}").inspect}" }
  "#<data #{self.class.name}#{pairs.empty? ? '' : " #{pairs.join(', ')}"}>"
end

#membersObject



62
63
64
# File 'lib/rails_ai_context/polyfill/data.rb', line 62

def members
  self.class.members
end

#to_h(&block) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/rails_ai_context/polyfill/data.rb', line 66

def to_h(&block)
  pairs = self.class.members.map { |m| [ m, instance_variable_get(:"@#{m}") ] }
  return pairs.to_h unless block

  pairs.each_with_object({}) do |(k, v), acc|
    nk, nv = block.call(k, v)
    acc[nk] = nv
  end
end

#with(**kwargs) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/rails_ai_context/polyfill/data.rb', line 76

def with(**kwargs)
  extra = kwargs.keys - self.class.members
  unless extra.empty?
    raise ArgumentError, "unknown keyword#{extra.length > 1 ? 's' : ''}: #{extra.map(&:inspect).join(', ')}"
  end

  self.class.new(**to_h.merge(kwargs))
end