Class: DataStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/datastruct.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DataStruct.



32
33
34
35
36
37
# File 'lib/datastruct.rb', line 32

def initialize(*args, **kwargs)
  @data ||= {}

  self.update(*args)
  self.update(**kwargs)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

This makes the struct accept the defined properties as instance methods



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/datastruct.rb', line 228

def method_missing(name, *args, &block)
  property = name
  set = false

  if is_setter?(property)
    property = getter(name)
    set = true
  end

  if valid_property? property
    if set
      @data[property] = args.first
    else
      @data[property]
    end
  else
    super
  end
end

Class Method Details

.from_array(array) ⇒ Object

A more ideomatic way of calling new(*array)



19
20
21
# File 'lib/datastruct.rb', line 19

def self.from_array(array)
  self.new(*array)
end

.from_hash(hash) ⇒ Object

A more ideomatic way of calling new(**hash)



26
27
28
29
30
# File 'lib/datastruct.rb', line 26

def self.from_hash(hash)
  hash = symbol_keys(hash)

  self.new(**hash)
end

Instance Method Details

#==(other) ⇒ Object

Delegates to Hash#==



42
43
44
45
46
47
48
# File 'lib/datastruct.rb', line 42

def ==(other)
  if not other.instance_of? self.class
    false
  else
    @data == other.instance_variable_get(:@data)
  end
end

#[](key) ⇒ Object

Does the same as #get, but returns nil instead of raising KeyError

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)


82
83
84
85
86
# File 'lib/datastruct.rb', line 82

def [](key)
  get(key)
rescue KeyError
  nil
end

#eachObject

Delegates to Hash#each

See Also:



56
57
58
# File 'lib/datastruct.rb', line 56

def each(*args, &block)
  @data.each(*args, &block)
end

#get(property) ⇒ Object

Returns a property using its getter method

Parameters:

  • property (String, Symbol)

Raises:

  • (KeyError)

    on invalid property name



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

def get(property)
  property = property.to_sym

  if not valid_property? property
    fail KeyError, "Property not defined: #{property}"
  end

  self.send(property)
end

#inspectObject

Produces a text representation of the object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/datastruct.rb', line 91

def inspect
  text = "#<#{self.class.to_s}"

  text << @data.reduce("") { |a, pair|
    a << " #{pair[0]}=#{pair[1].inspect}"
  }

  text << ">"

  return text
end

#merge(other) ⇒ Hash

Delegates to Hash#merge

Returns:

  • (Hash)


215
216
217
# File 'lib/datastruct.rb', line 215

def merge(other)
  @data.merge(other)
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
# File 'lib/datastruct.rb', line 103

def respond_to?(method_name)
  if valid_property?(method_name) or valid_property?(getter(method_name))
    true
  else
    super
  end
end

#set(property, value) ⇒ Object Also known as: []=

Sets the value of a property using its setter method

Parameters:

  • property (String, Symbol)
  • value (Object)

Raises:

  • (KeyError)

    on invalid property name



118
119
120
121
122
123
124
125
126
# File 'lib/datastruct.rb', line 118

def set(property, value)
  property = property.to_sym

  if not valid_property? property
    fail KeyError, "Property not defined: #{property}"
  end

  self.send(setter(property), value)
end

#to_arrayArray Also known as: to_a

Returns the properties of the object as an array

Returns:

  • (Array)


135
136
137
# File 'lib/datastruct.rb', line 135

def to_array
  self.class::PROPERTIES.map { |name| @data[name] }
end

#to_hashHash Also known as: to_h

Returns the properties of the object as a hash

Returns:

  • (Hash)


146
147
148
# File 'lib/datastruct.rb', line 146

def to_hash
  @data.dup
end

#to_json(*args) ⇒ String

Note:

JSON must be loaded for this function to work

Dumps the properties of this object to JSON using Ruby’s JSON module

Returns:

  • (String)

See Also:



159
160
161
# File 'lib/datastruct.rb', line 159

def to_json(*args)
  @data.to_json(*args)
end

#to_yaml(*args) ⇒ String

Note:

YAML must be loaded for this function to work

Dumps the properties of this object to YAML using Ruby’s YAML module

Returns:

  • (String)

See Also:



170
171
172
# File 'lib/datastruct.rb', line 170

def to_yaml(*args)
  @data.to_yaml(*args)
end

#update(*args, **kwargs) ⇒ Object

Note:

Keyword arguments override posisional arguments

Updates the values of this object’s properties

Both positional arguments and keyword arguments are used to update the property values of the object. Positional arguments should be passed in the same order as the defined properties.

Returns:

  • nil

Raises:

  • (ArgumentError)

    on invalid property names



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/datastruct.rb', line 185

def update(*args, **kwargs)
  @data ||= {}

  if args.length > self.class::PROPERTIES.length
    x = args.length
    y = self.class::PROPERTIES.length
    msg = "Too many arguments (you passed #{x} arguments for #{y} properties)"

    fail ArgumentError, msg
  end

  hash = Hash[self.class::PROPERTIES[0...args.length].zip(args)]
  hash.update(kwargs)

  hash.each_pair { |key, value|
    begin
      self.set(key, value)
    rescue KeyError => e
      fail ArgumentError, "Invalid property: #{key}"
    end
  }

  nil
end