Module: Quant::Attributes::InstanceMethods
- Defined in:
- lib/quant/attributes.rb
Instance Method Summary collapse
-
#default_value_for(entry) ⇒ Object
The default value can be one of the following: - A symbol that is a method the instance responds to - A symbol that is a method that the instance’s tick responds to - A Proc that is bound to the instance - An immediate value (Integer, Float, Boolean, etc.).
-
#each_attribute(&block) ⇒ Object
Iterates over all defined attributes in a child => parent hierarchy, and yields the name and entry for each.
-
#initialize ⇒ Object
Makes some assumptions about the class’s initialization having a
tick
keyword argument. -
#initialize_attributes ⇒ Object
Initializes the defined attributes with default values and defines accessor methods for each attribute.
-
#self_and_ancestors ⇒ Object
Returns an array of all classes in the hierarchy, starting with the current class.
-
#to_h ⇒ Hash
Serializes keys that have been defined as serializeable attributes Key values that are nil are omitted from the hash.
-
#to_json(*args) ⇒ String
Serializes keys that have been defined as serializeable attributes Key values that are nil are removed from the hash.
Instance Method Details
#default_value_for(entry) ⇒ Object
The default value can be one of the following:
-
A symbol that is a method the instance responds to
-
A symbol that is a method that the instance’s tick responds to
-
A Proc that is bound to the instance
-
An immediate value (Integer, Float, Boolean, etc.)
127 128 129 130 131 132 133 134 |
# File 'lib/quant/attributes.rb', line 127 def default_value_for(entry) return instance_exec(&entry[:default]) if entry[:default].is_a?(Proc) return entry[:default] unless entry[:default].is_a?(Symbol) return send(entry[:default]) if respond_to?(entry[:default]) return tick.send(entry[:default]) if tick.respond_to?(entry[:default]) entry[:default] end |
#each_attribute(&block) ⇒ Object
Iterates over all defined attributes in a child => parent hierarchy, and yields the name and entry for each.
116 117 118 119 120 |
# File 'lib/quant/attributes.rb', line 116 def each_attribute(&block) self_and_ancestors.select{ |klass| Attributes.registry[klass] }.each do |klass| Attributes.registry[klass].each{ |name, entry| block.call(name, entry) } end end |
#initialize ⇒ Object
Makes some assumptions about the class’s initialization having a tick
keyword argument. If one does exist, the tick
is considered as a potential source for the declared defaults
102 103 104 105 |
# File 'lib/quant/attributes.rb', line 102 def initialize(...) super(...) initialize_attributes end |
#initialize_attributes ⇒ Object
Initializes the defined attributes with default values and defines accessor methods for each attribute. If a child class redefines a parent’s attribute, the child’s definition will be used.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/quant/attributes.rb', line 140 def initialize_attributes each_attribute do |name, entry| # use the child's definition, skipping the parent's next if respond_to?(name) ivar_name = "@#{name}" define_singleton_method(name) do return instance_variable_get(ivar_name) if instance_variable_defined?(ivar_name) # Sets the default value when accessed and ivar is not already set default_value_for(entry).tap { |value| instance_variable_set(ivar_name, value) } end define_singleton_method("#{name}=") { |value| instance_variable_set(ivar_name, value) } end end |
#self_and_ancestors ⇒ Object
Returns an array of all classes in the hierarchy, starting with the current class
108 109 110 111 112 |
# File 'lib/quant/attributes.rb', line 108 def self_and_ancestors [this_class = self.class].tap do |classes| classes << this_class = this_class.superclass while !this_class.nil? end end |
#to_h ⇒ Hash
Serializes keys that have been defined as serializeable attributes Key values that are nil are omitted from the hash
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/quant/attributes.rb', line 159 def to_h {}.tap do |key_values| each_attribute do |name, entry| next unless entry[:key] value = send(name) next unless value key_values[entry[:key]] = value end end end |
#to_json(*args) ⇒ String
Serializes keys that have been defined as serializeable attributes Key values that are nil are removed from the hash
175 176 177 |
# File 'lib/quant/attributes.rb', line 175 def to_json(*args) Oj.dump(to_h, *args) end |