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.



71
72
73
# File 'lib/ig_markets/model.rb', line 71

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)


94
95
96
# File 'lib/ig_markets/model.rb', line 94

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, Model) (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, Proc)

    When type is Time this specifies the time zone to append to String values assigned to this attribute prior to parsing them with :format. Defaults to ‘+0000` (UTC) unless :format is `%Q`. Can be a Proc that returns the time zone string to use.



118
119
120
121
122
123
124
# File 'lib/ig_markets/model.rb', line 118

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

.attribute_type(attribute_name) ⇒ Object

Returns the type of the specified attribute.

Parameters:

  • attribute_name (Symbol)

    The name of the attribute to return the type for.

Returns:

  • The type of the specified attribute.



85
86
87
# File 'lib/ig_markets/model.rb', line 85

def attribute_type(attribute_name)
  defined_attributes.fetch(attribute_name).fetch :type
end

.defined_attribute_namesArray<Symbol>

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

Returns:

  • (Array<Symbol>)


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

def defined_attribute_names
  (defined_attributes || {}).keys
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:



41
42
43
# File 'lib/ig_markets/model.rb', line 41

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

#initialize_copy(other) ⇒ Object

Copy initializer that duplicates the #attributes hash in full.

Parameters:

  • other (Model)

    The model to copy.



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

def initialize_copy(other)
  super

  @attributes = other.attributes.dup
end

#inspectString

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

Returns:

  • (String)


48
49
50
51
52
53
54
# File 'lib/ig_markets/model.rb', line 48

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