Class: DataStruct
- Inherits:
-
Object
- Object
- DataStruct
- Defined in:
- lib/datastruct.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
-
.from_array(array) ⇒ Object
A more ideomatic way of calling new(*array).
-
.from_hash(hash) ⇒ Object
A more ideomatic way of calling new(**hash).
Instance Method Summary collapse
-
#each ⇒ Object
Delegates to Hash#each.
-
#get(property) ⇒ Object
(also: #[])
Returns a property using its getter method.
-
#initialize(*args, **kwargs) ⇒ DataStruct
constructor
A new instance of DataStruct.
-
#inspect ⇒ Object
Produces a text representation of the object.
- #respond_to?(method_name) ⇒ Boolean
-
#set(property, value) ⇒ Object
(also: #[]=)
Sets the value of a property using its setter method.
-
#to_array ⇒ Array
(also: #to_a)
Returns the properties of the object as an array.
-
#to_hash ⇒ Hash
(also: #to_h)
Returns the properties of the object as a hash.
-
#to_json(*args) ⇒ String
Dumps the properties of this object to JSON using Ruby’s JSON module.
-
#to_yaml(*args) ⇒ String
Dumps the properties of this object to YAML using Ruby’s YAML module.
-
#update(*args, **kwargs) ⇒ Object
Updates the values of this object’s properties.
Constructor Details
#initialize(*args, **kwargs) ⇒ DataStruct
Returns a new instance of DataStruct.
30 31 32 33 34 35 |
# File 'lib/datastruct.rb', line 30 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
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/datastruct.rb', line 192 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 |
# File 'lib/datastruct.rb', line 26 def self.from_hash(hash) self.new(**hash) end |
Instance Method Details
#each ⇒ Object
Delegates to Hash#each
43 44 45 |
# File 'lib/datastruct.rb', line 43 def each(*args, &block) @data.each(*args, &block) end |
#get(property) ⇒ Object Also known as: []
Returns a property using its getter method
53 54 55 56 57 58 59 60 61 |
# File 'lib/datastruct.rb', line 53 def get(property) property = property.to_sym if not valid_property? property fail KeyError, "Property not defined: #{property}" end self.send(property) end |
#inspect ⇒ Object
Produces a text representation of the object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/datastruct.rb', line 68 def inspect text = "#<#{self.class.to_s}" text << @data.reduce("") { |a, pair| a << " #{pair[0]}=#{pair[1].inspect}" } text << ">" return text end |
#respond_to?(method_name) ⇒ Boolean
80 81 82 83 84 85 86 |
# File 'lib/datastruct.rb', line 80 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
95 96 97 98 99 100 101 102 103 |
# File 'lib/datastruct.rb', line 95 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_array ⇒ Array Also known as: to_a
Returns the properties of the object as an array
112 113 114 |
# File 'lib/datastruct.rb', line 112 def to_array self.class::PROPERTIES.map { |name| @data[name] } end |
#to_hash ⇒ Hash Also known as: to_h
Returns the properties of the object as a hash
123 124 125 |
# File 'lib/datastruct.rb', line 123 def to_hash @data.dup end |
#to_json(*args) ⇒ String
JSON must be loaded for this function to work
Dumps the properties of this object to JSON using Ruby’s JSON module
136 137 138 |
# File 'lib/datastruct.rb', line 136 def to_json(*args) @data.to_json(*args) end |
#to_yaml(*args) ⇒ String
YAML must be loaded for this function to work
Dumps the properties of this object to YAML using Ruby’s YAML module
147 148 149 |
# File 'lib/datastruct.rb', line 147 def to_yaml(*args) @data.to_yaml(*args) end |
#update(*args, **kwargs) ⇒ Object
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.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/datastruct.rb', line 162 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 |