lib/nstore.rb in nstore-0.3.0 vs lib/nstore.rb in nstore-0.4.0
- old
+ new
@@ -31,18 +31,20 @@
def nstore(attribute, options)
accessors = options[:accessors]
prefix = options.fetch(:prefix, false)
stringify = options.fetch(:stringify, true)
+ accessors = { nil => accessors } if accessors.is_a? Array
flat_accessors = []
deep_flatten(accessors, [], flat_accessors)
attribute = attribute.to_s if stringify
_nstore_generate_accessors(attribute, flat_accessors, prefix, stringify)
end
def _nstore_generate_accessors(attribute, flat_accessors, prefix, stringify)
flat_accessors.each do |keys|
+ keys.reject!(&:nil?)
keys.map!(&:to_s) if stringify
define_method("#{prefix ? "#{attribute}_" : ''}#{keys.join('_')}=".to_sym) do |value|
write_nstore_attribute(attribute, keys, value)
end
@@ -77,14 +79,12 @@
[object]
end
end
end
- private
-
- # @param [Symbol] attribute
- # @param [Array] keys
+ # @param [Symbol,String] attribute
+ # @param [Array<String,Symbol>] keys
# @param [Object] value
def write_nstore_attribute(attribute, keys, value)
send("#{attribute}=", {}) if send(attribute).nil?
position = send(attribute)
@@ -93,13 +93,12 @@
position = position[key]
end
position[keys[-1]] = value
end
+ # @param [Symbol,String] attribute
+ # @param [Array<String,Symbol>] keys
+ # @return [Object]
def read_nstore_attribute(attribute, keys)
send(attribute).send(:dig, *keys)
- end
-
- def nstore_key_brakets(attribute, keys)
- ([attribute] + keys).map { |k| "['#{k}']" }.join
end
end