lib/array_model.rb in array_model-1.0.2 vs lib/array_model.rb in array_model-1.0.3
- old
+ new
@@ -12,36 +12,36 @@
# that are created are read only. The data can come either from a constant in the ruby
# script itself, or from the filesystem as a YAML or JSON file.
#
# Example:
#
-# USERS = [
-# { name: 'Nathan', year: 1984 },
-# { name: 'Dave', year: 1987 }
-# ]
+# USERS = [
+# { name: 'Nathan', year: 1984 },
+# { name: 'Dave', year: 1987 }
+# ]
#
-# class User < ArrayModel
-# model_data USERS
-# attr_model_reader :name
-# attr_model_reader :year
+# class User < ArrayModel
+# model_data USERS
+# attr_model_reader :name
+# attr_model_reader :year
#
-# def age
-# Time.now.year - year
-# end
-# end
+# def age
+# Time.now.year - year
+# end
+# end
#
-# User[0].age # => 32
-# User[1].name # => "Dave"
+# User[0].age # => 32
+# User[1].name # => "Dave"
#
class ArrayModel
# get the object with the key :k:
#
# By default :k: will be a simple array index, but it can be changed to
# any field by using the :primary_key option when calling :model_data:
# when the class is defined
#
- # Users['reednj'] # => #<Users:0x007fe693866808>
+ # Users['reednj'] # => #<Users:0x007fe693866808>
#
def self.[](k)
if @data_key.nil?
item_data = @data[k.to_i]
else
@@ -68,23 +68,23 @@
# an alias for the values method to return a raw value from the data
# hash for a given model object. The attr_reader methods should be
# prefered to accessing the hash directly, but this can be useful
# in certain cases
#
- # u = Users['reednj']
- # u.username # => 'reednj'
- # u.values(:username) # => 'reednj'
- # u[:username] # => 'reednj'
+ # u = Users['reednj']
+ # u.username # => 'reednj'
+ # u.values(:username) # => 'reednj'
+ # u[:username] # => 'reednj'
#
def [](k)
values[k]
end
# returns the raw Hash that provides the data for the model
# object
#
- # Users['reednj'].values # => {:username => 'reednj', ...}
+ # Users['reednj'].values # => {:username => 'reednj', ...}
#
def values
@item_data
end
@@ -99,16 +99,16 @@
# Adds attr_reader methods to the class for a given field
# in the data hash. The :key: option can be used to set the name
# of the key in the hash, if it doesn't have the same name as the
# method
#
- # class Users < ArrayModel
- # ...
- # attr_model_reader :username
- # attr_model_reader :user_id, :key => :userId
- # ...
- # end
+ # class Users < ArrayModel
+ # ...
+ # attr_model_reader :username
+ # attr_model_reader :user_id, :key => :userId
+ # ...
+ # end
#
def self.attr_model_reader(name, options = {})
define_method name.to_sym do
values[(options[:key] || name).to_sym]
end
@@ -116,15 +116,15 @@
# like :attr_model_reader:, but mulitple readers can be added at
# once. No options can be passed when using this method to add
# the readers
#
- # class Users < ArrayModel
- # ...
- # attr_model_readers [:username, :user_id]
- # ...
- # end
+ # class Users < ArrayModel
+ # ...
+ # attr_model_readers [:username, :user_id]
+ # ...
+ # end
#
def self.attr_model_readers(keys)
keys.each {|k| attr_model_reader k }
end
@@ -138,14 +138,14 @@
#
# The :primary_key option can be used to index the data by a particular field
# in the hash when it is accessed later via the subscript operator. If this option
# is ommited then the data will be accessable simply by the array index
#
- # class Users < ArrayModel
- # # USER_LIST is a const containing an array of hashes
- # model_data USER_LIST, :primary_key => :username
- # ...
- # end
+ # class Users < ArrayModel
+ # # USER_LIST is a const containing an array of hashes
+ # model_data USER_LIST, :primary_key => :username
+ # ...
+ # end
#
def self.model_data(data, options = nil)
options ||= {}
data.is_a! Array, 'data'