lib/efo_nelfo/property.rb in efo_nelfo-0.0.7 vs lib/efo_nelfo/property.rb in efo_nelfo-1.0.0
- old
+ new
@@ -1,22 +1,33 @@
module EfoNelfo
module Property
- include EfoNelfo::AttributeAssignment
def self.included(base)
base.send :extend, ClassMethods
end
+ def initialize_attributes(*args)
+ if args && args.first.is_a?(Hash)
+ args.first.each do |attr, value|
+ send "#{attr}=", value
+ end
+ end
+ end
+
def attributes
@attributes ||= initialize_default_attributes
end
def properties
self.class.properties
end
+ def has_property?(property)
+ properties.include? property
+ end
+
def to_a
properties.keys.map { |prop| formatted_for_csv(prop) }
end
private
@@ -74,15 +85,21 @@
# - required whether attribute is required. Default is false
# - limit Length the attribute can be. Default is nil
# - alias Norwegian alias name for the attribute
#
def property(name, options={})
+
options = {
type: :string,
required: false,
}.update options
name = name.to_sym
+
+ # ensure all options are valid
+ valid_options = [:type, :required, :limit, :read_only, :alias, :default]
+ invalid_options = options.keys - valid_options
+ raise EfoNelfo::UnknownPropertyOption.new("Invalid option for #{name}: #{invalid_options.join(',')}") if invalid_options.any?
# Store property info in @properties
raise EfoNelfo::DuplicateProperty if properties.has_key?(name)
properties[name] = options