Class: IGMarkets::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/ig_markets/model.rb,
lib/ig_markets/model/typecasters.rb

Overview

Implement typecaster methods for Model.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Model

Initializes this new model with the given attribute values. Attributes not known to this model will raise ArgumentError.

Parameters:

  • attributes (Hash) (defaults to: {})

    The attribute values to set on this new model.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ig_markets/model.rb', line 13

def initialize(attributes = {})
  defined_attribute_names = self.class.defined_attribute_names

  defined_attribute_names.each do |name|
    send "#{name}=", attributes[name]
  end

  (attributes.keys - defined_attribute_names).map do |attribute|
    value = attributes[attribute]

    raise ArgumentError, "Unknown attribute: #{self.class.name}##{attribute}, value: #{inspect_value value}"
  end
end

Class Attribute Details

.defined_attributesHash

Returns A hash containing details of all attributes that have been defined on this model.

Returns:

  • (Hash)

    A hash containing details of all attributes that have been defined on this model.



62
63
64
# File 'lib/ig_markets/model.rb', line 62

def defined_attributes
  @defined_attributes
end

Instance Attribute Details

#attributesHash (readonly)

Returns The current attribute values set on this model.

Returns:

  • (Hash)

    The current attribute values set on this model.



7
8
9
# File 'lib/ig_markets/model.rb', line 7

def attributes
  @attributes
end

Class Method Details

.allowed_values(attribute_name) ⇒ Array

Returns the array of allowed values for the specified attribute that was passed to attribute.

Parameters:

  • attribute_name (Symbol)

    The name of the attribute to return the allowed values for.

Returns:

  • (Array)


76
77
78
# File 'lib/ig_markets/model.rb', line 76

def allowed_values(attribute_name)
  defined_attributes.fetch(attribute_name).fetch(:allowed_values)
end

.attribute(name, type = String, options = {}) ⇒ Object

Defines setter and getter methods for a new attribute on this class.

Parameters:

  • name (Symbol)

    The name of the new attribute.

  • type (Boolean, String, Date, Time, Fixnum, Float, Symbol, #from) (defaults to: String)

    The attribute’s type.

  • options (Hash) (defaults to: {})

    The configuration options for the new attribute.

Options Hash (options):

  • :allowed_values (Array)

    The set of values that this attribute is allowed to be set to. An attempt to set this attribute to a value not in this list will raise ArgumentError. Optional.

  • :nil_if (Array)

    Values that, when set on the attribute, should be converted to nil.

  • :regex (Regexp)

    When type is String only values matching this regex will be allowed. Optional.

  • :format (String)

    When type is Date or Time this specifies the format for parsing String and Fixnum instances assigned to this attribute.

  • :time_zone (String)

    When type is Time this specifies the time zone to append to String values assigned to this attribute prior to parsing them with :format. Optional.



98
99
100
101
102
103
104
# File 'lib/ig_markets/model.rb', line 98

def attribute(name, type = String, options = {})
  define_attribute_reader name
  define_attribute_writer name, type, options

  self.defined_attributes ||= {}
  self.defined_attributes[name] = options.merge type: type
end

.defined_attribute_namesArray<Symbol>

Returns the names of all currently defined attributes for this model.

Returns:

  • (Array<Symbol>)


67
68
69
# File 'lib/ig_markets/model.rb', line 67

def defined_attribute_names
  (defined_attributes || {}).keys
end

.from(source) ⇒ nil, ...

Creates a new Model instance from the specified source, which can take a variety of different forms.

Parameters:

  • source (nil, Hash, Model, Array)

    The source object to create a new Model instance from. If source is nil then nil is returned. If source is a hash then a new Model instance is returned and the hash is passed to ‘Model#initialize`. If source is an instance of this class then dup is called on it and the duplicate returned. If source is an array then it is mapped into a new array with each item having been recursively passed through this method.

Returns:

Raises:

  • (ArgumentError)


115
116
117
118
119
120
121
122
# File 'lib/ig_markets/model.rb', line 115

def from(source)
  return nil if source.nil?
  return new source if source.is_a? Hash
  return source.dup if source.is_a? self
  return source.map { |item| from item } if source.is_a? Array

  raise ArgumentError, "Unable to make a #{self} from instance of #{source.class}"
end

Instance Method Details

#==(other) ⇒ Boolean

Compares this model to another, the attributes and class must match for them to be considered equal.

Parameters:

  • other (#class, #attributes)

    The other model to compare to.

Returns:



32
33
34
# File 'lib/ig_markets/model.rb', line 32

def ==(other)
  self.class == other.class && attributes == other.attributes
end

#inspectString

Returns a human-readable string containing this model’s type and all its current attribute values.

Returns:

  • (String)


39
40
41
42
43
44
45
# File 'lib/ig_markets/model.rb', line 39

def inspect
  formatted_attributes = self.class.defined_attribute_names.map do |attribute|
    "#{attribute}: #{inspect_value send(attribute)}"
  end

  "#<#{self.class.name} #{formatted_attributes.join ', '}>"
end