Module: Locomotive::Presentable::ClassMethods
- Defined in:
- lib/locomotive/presentable.rb
Instance Method Summary collapse
-
#alias_of(alias_name) ⇒ String
Get the name of the property for which the property passed in parameter is an alias.
-
#collection(name, options = {}) ⇒ Object
Add a collection to the current instance.
- #define_getter(name, collection = false) ⇒ Object
- #define_setter(name, aliases = []) ⇒ Object
-
#inherited(subclass) ⇒ Object
Override inherited in order to copy the parent list of getters, setters and property options.
-
#properties(*names) ⇒ Object
Add multiple properties all in once.
-
#property(name, options = {}) ⇒ Object
Add a property to the current instance.
Instance Method Details
#alias_of(alias_name) ⇒ String
Get the name of the property for which the property passed in parameter is an alias.
201 202 203 204 205 206 |
# File 'lib/locomotive/presentable.rb', line 201 def alias_of(alias_name) self.setters.find do |name| list = [*(self.[name] || {})[:alias]] || [] list.include?(alias_name.to_sym) end end |
#collection(name, options = {}) ⇒ Object
Add a collection to the current instance. It creates getter/setter mapped to the collection of the source object.
156 157 158 |
# File 'lib/locomotive/presentable.rb', line 156 def collection(name, = {}) property(name, .merge(:collection => true)) end |
#define_getter(name, collection = false) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/locomotive/presentable.rb', line 160 def define_getter(name, collection = false) (@getters ||= []) << name.to_s class_eval <<-EOV def #{name} if #{collection.to_s} list = self.source.send(:#{name}) list ? list.map(&:as_json) : [] else self.source.send(:#{name}) end end EOV end |
#define_setter(name, aliases = []) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/locomotive/presentable.rb', line 175 def define_setter(name, aliases = []) (@setters ||= []) << name.to_s @setters += aliases.map(&:to_s) class_eval <<-EOV def #{name}=(value) self.source.send(:#{name}=, value) end EOV aliases.each do |_name| class_eval <<-EOV def #{_name}=(value) self.#{name} = value end EOV end end |
#inherited(subclass) ⇒ Object
Override inherited in order to copy the parent list of getters, setters and property options.
106 107 108 109 110 111 112 |
# File 'lib/locomotive/presentable.rb', line 106 def inherited(subclass) subclass.getters = getters.clone if getters subclass.setters = setters.clone if setters subclass. = .clone if super end |
#properties(*names) ⇒ Object
Add multiple properties all in once. If th last property name is a hash, then it will be used as the options for all the other properties.
120 121 122 123 124 125 126 |
# File 'lib/locomotive/presentable.rb', line 120 def properties(*names) = names.last.is_a?(Hash) ? names.pop : {} names.each do |name| property(name, ) end end |
#property(name, options = {}) ⇒ Object
Add a property to the current instance. It creates getter/setter methods related to that property. By default, the getter and setter are bound to the source object.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/locomotive/presentable.rb', line 135 def property(name, = {}) aliases = [*[:alias]] collection = [:collection] == true (@property_options ||= {})[name.to_s] = unless [:only_setter] == true define_getter(name, collection) end unless [:only_getter] == true define_setter(name, aliases) end end |