Class: WscSdk::SchemaAttribute
- Inherits:
-
Object
- Object
- WscSdk::SchemaAttribute
- Defined in:
- lib/wsc_sdk/schema_attribute.rb
Overview
A class to represent an attribute inside of a model's data schema.
Constant Summary collapse
- TYPES =
A list of valid types for a schema attribute
[ :string, :integer, :float, :boolean, :array, :hash, :datetime ]
Instance Attribute Summary collapse
-
#access ⇒ Object
readonly
Returns the value of attribute access.
-
#as ⇒ Object
readonly
Returns the value of attribute as.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validate ⇒ Object
readonly
Returns the value of attribute validate.
Instance Method Summary collapse
-
#attribute_name ⇒ Symbol
Returns the expected attribute name for the attribute.
-
#call_proc_or_symbol(proc_or_symbol, model) ⇒ Any
Call a Proc or Symbol against a model.
-
#default_value(model) ⇒ Object
Returns the default value for the attribute.
-
#initialize(name, type, options = {}) ⇒ SchemaAttribute
constructor
Create a new schema attribute.
-
#read_access?(model) ⇒ Boolean
Determine if the attribute is readable according to the attribute configuration.
-
#required?(model) ⇒ Boolean
Determine if the attribute is required.
-
#transform_value(model, value) ⇒ Any
Converts an inbound value to the appropriate type.
-
#type_to_class(attribute_type) ⇒ Any
Converts the specified type to a class.
-
#valid?(model) ⇒ String
Determine if a value is valid according to the attribute configuration.
-
#valid_model_type?(attribute_type) ⇒ Boolean
Determines if the specified type of the attribute matches a model.
-
#valid_type?(value) ⇒ Boolean
Determine if the provided value is of a valid type for the attribute.
-
#value_or_default(model) ⇒ Object
Return either the value from the model that is represented by the attribute, or the default value if the value is not assigned.
-
#write_access?(model) ⇒ Boolean
Determine if the attribute is writable according to the attribute configuration.
Constructor Details
#initialize(name, type, options = {}) ⇒ SchemaAttribute
Create a new schema attribute.
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 82 def initialize(name, type, ={}) raise ArgumentError.new("Invalid schema attribute type specified: #{type}") unless valid_type?(type) @name = name.to_sym @type = type @access = .fetch(:access, :read_write) @default = .fetch(:default, nil) @required = .fetch(:required, false) @validate = .fetch(:validate, nil) @as = .fetch(:as, nil) end |
Instance Attribute Details
#access ⇒ Object (readonly)
Returns the value of attribute access
21 22 23 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 21 def access @access end |
#as ⇒ Object (readonly)
Returns the value of attribute as
21 22 23 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 21 def as @as end |
#default ⇒ Object (readonly)
Returns the value of attribute default
21 22 23 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 21 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name
21 22 23 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 21 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required
21 22 23 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 21 def required @required end |
#type ⇒ Object (readonly)
Returns the value of attribute type
21 22 23 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 21 def type @type end |
#validate ⇒ Object (readonly)
Returns the value of attribute validate
21 22 23 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 21 def validate @validate end |
Instance Method Details
#attribute_name ⇒ Symbol
Returns the expected attribute name for the attribute. If the `as` value is returned, this is returned, otherwise the attributes `name` value is returned.
100 101 102 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 100 def attribute_name (self.as || self.name).to_sym end |
#call_proc_or_symbol(proc_or_symbol, model) ⇒ Any
Call a Proc or Symbol against a model.
308 309 310 311 312 313 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 308 def call_proc_or_symbol(proc_or_symbol, model) return proc_or_symbol if proc_or_symbol.nil? return proc_or_symbol.call(model) if proc_or_symbol.is_a?(Proc) return model.send(proc_or_symbol) if proc_or_symbol.is_a?(Symbol) and model.respond_to?(proc_or_symbol) return proc_or_symbol end |
#default_value(model) ⇒ Object
Returns the default value for the attribute
258 259 260 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 258 def default_value(model) call_proc_or_symbol(default, model) end |
#read_access?(model) ⇒ Boolean
Determine if the attribute is readable according to the attribute configuration.
116 117 118 119 120 121 122 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 116 def read_access?(model) return true if access.nil? raise ::ArgumentError.new("The provided #{model.class.name} model does not have the #{self.name} attribute configured") unless model.has_attribute?(self.name) current_access = call_proc_or_symbol(access, model) [:read, :read_write].include?(current_access) end |
#required?(model) ⇒ Boolean
Determine if the attribute is required.
284 285 286 287 288 289 290 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 284 def required?(model) raise ArugumentError.new("The provided #{model.class.name} model does not have the #{self.name} attribute configured") unless model.has_attribute?(self.name) return required if required.is_a?(TrueClass) or required.is_a?(FalseClass) _required = call_proc_or_symbol(required, model) return false if _required.nil? _required end |
#transform_value(model, value) ⇒ Any
Converts an inbound value to the appropriate type.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 234 def transform_value(model, value) # Booleans are a special case because they are not represented by a single # class. return value.to_s.downcase.start_with?("y","t","1") if self.type == :boolean type_class = type_to_class(self.type) transformed_value = value if type_class == Time current_timezone = Time.zone Time.zone = "UTC" transformed_value = Time.zone.parse(value) # => Tue, 23 Nov 2010 23:29:57 UTC +00:00 Time.zone = current_timezone elsif type_class < WscSdk::Model and value.is_a?(Hash) transformed_value = type_class.new(model.endpoint, value) end transformed_value end |
#type_to_class(attribute_type) ⇒ Any
Converts the specified type to a class
210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 210 def type_to_class(attribute_type) # Booleans are a special case because they are not represented by a single # class. return String if attribute_type == :boolean return Time if attribute_type == :datetime class_name = attribute_type.to_s.camelize unless TYPES.include?(attribute_type) class_name = "WscSdk::Models::#{class_name}" end return (class_name.constantize) end |
#valid?(model) ⇒ String
Determine if a value is valid according to the attribute configuration.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 156 def valid?(model) return nil if validate.nil? raise ArugumentError.new("The provided #{model.class.name} model does not have the #{self.name} attribute configured") unless model.has_attribute?(self.name) value = model.attributes[self.name] = nil if validate.is_a?(Array) valid = validate.include?(value) model.add_error(name, "value must be one of the following: #{validate.join(", ")}") unless valid return valid else return call_proc_or_symbol(validate, model) end # Calls should never get here. return true end |
#valid_model_type?(attribute_type) ⇒ Boolean
Determines if the specified type of the attribute matches a model.
197 198 199 200 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 197 def valid_model_type?(attribute_type) type_class = type_to_class(attribute_type) !type_class.nil? end |
#valid_type?(value) ⇒ Boolean
Determine if the provided value is of a valid type for the attribute.
184 185 186 187 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 184 def valid_type?(attribute_type) return true if TYPES.include?(attribute_type) return valid_model_type?(attribute_type) end |
#value_or_default(model) ⇒ Object
Return either the value from the model that is represented by the attribute, or the default value if the value is not assigned.
271 272 273 274 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 271 def value_or_default(model) raise ArugumentError.new("The provided #{model.class.name} model does not have the #{self.name} attribute configured") unless model.has_attribute?(self.name) model.attributes[self.name] || default_value(model) end |
#write_access?(model) ⇒ Boolean
Determine if the attribute is writable according to the attribute configuration.
136 137 138 139 140 141 142 |
# File 'lib/wsc_sdk/schema_attribute.rb', line 136 def write_access?(model) return true if access.nil? raise ArugumentError.new("The provided #{model.class.name} model does not have the #{self.name} attribute configured") unless model.has_attribute?(self.name) current_access = call_proc_or_symbol(access, model) [:write, :read_write].include?(current_access) end |