Module: Structable::InstanceMethods

Extended by:
Forwardable
Defined in:
lib/structable/instancemethods.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Boolean Also known as: ===


44
45
46
# File 'lib/structable/instancemethods.rb', line 44

def ==(other)
  _compare other, :==
end

#[](key) ⇒ Object


109
110
111
# File 'lib/structable/instancemethods.rb', line 109

def [](key)
  _subscript(key) {|name|_get! name}
end

#[]=(key, value) ⇒ value


116
117
118
# File 'lib/structable/instancemethods.rb', line 116

def []=(key, value)
  _subscript(key) {|name|_set! name, value}
end

#each_member {|name| ... } ⇒ Enumerator Also known as: each_key

Yields:

  • (name)

Yield Parameters:

  • name (Symbol)

Yield Returns:

  • (self)

23
24
25
26
27
# File 'lib/structable/instancemethods.rb', line 23

def each_member(&block)
  return to_enum(__method__) unless block_given?
  self.class.each_member(&block)
  self
end

#each_pair {|name, value| ... } ⇒ Enumerator

Yields:

  • (name, value)

Yield Parameters:

  • name (Symbol)

Yield Returns:

  • (self)

140
141
142
143
144
# File 'lib/structable/instancemethods.rb', line 140

def each_pair
  return to_enum(__method__) unless block_given?
  each_member{|name|yield name, self[name]}
  self
end

#each_value {|value| ... } ⇒ Enumerator Also known as: each

Yields:

  • (value)

Yield Returns:

  • (self)

129
130
131
132
# File 'lib/structable/instancemethods.rb', line 129

def each_value
  return to_enum(__method__) unless block_given?
  each_member{|name|yield self[name]}
end

#eql?(other) ⇒ Boolean


50
51
52
# File 'lib/structable/instancemethods.rb', line 50

def eql?(other)
  _compare other, :eql?
end

#freezeself


121
122
123
124
# File 'lib/structable/instancemethods.rb', line 121

def freeze
  close
  super
end

#initialize(*values) ⇒ Object


31
32
33
34
# File 'lib/structable/instancemethods.rb', line 31

def initialize(*values)
  @_db, @_locks = {}, {}
  _replace_values(*values)
end

#inspectString


55
56
57
58
59
60
61
62
63
# File 'lib/structable/instancemethods.rb', line 55

def inspect
  "#<#{self.class} (Structable)".tap {|s|
    each_pair do |name, value|
      s << " #{name}=#{value.inspect}"
    end
    
    s << ">"
  }
end

#lock(name) ⇒ self


148
149
150
151
152
153
154
# File 'lib/structable/instancemethods.rb', line 148

def lock(name)
  name = name.to_sym
  assert_member name
  
  @_locks[name] = true
  self
end

#locked?(name) ⇒ Boolean


157
158
159
160
161
# File 'lib/structable/instancemethods.rb', line 157

def locked?(name)
  name = name.to_sym
  
  @_locks.has_key?(name)
end

#to_h(reject_no_assign = false) ⇒ Hash


78
79
80
81
82
83
84
85
86
# File 'lib/structable/instancemethods.rb', line 78

def to_h(reject_no_assign=false)
  return @_db.dup if reject_no_assign

  {}.tap {|h|
    each_pair do |key, value|
      h[key] = value
    end
  }
end

#to_sString


66
67
68
69
70
71
72
73
74
# File 'lib/structable/instancemethods.rb', line 66

def to_s
  "#<structable #{self.class}".tap {|s|
    each_pair do |name, value|
      s << " #{name}=#{value.inspect}"
    end
    
    s << '>'
  }
end

#valuesArray Also known as: to_a


37
38
39
# File 'lib/structable/instancemethods.rb', line 37

def values
  members.map{|name|@_db[name]}
end

#values_at(*_keys) ⇒ Array


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/structable/instancemethods.rb', line 90

def values_at(*_keys)
  [].tap {|r|
    _keys.each do |key|
      case key
      when Fixnum
        r << self[key]
      when Range
        key.each do |n|
          raise TypeError unless n.instance_of? Fixnum
          r << self[n]
        end
      else
        raise TypeError
      end
    end
  }
end