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.
205 206 207 208 209 210 |
# File 'lib/locomotive/presentable.rb', line 205 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.
160 161 162 |
# File 'lib/locomotive/presentable.rb', line 160 def collection(name, = {}) property(name, .merge(collection: true, type: 'Array')) end |
#define_getter(name, collection = false) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/locomotive/presentable.rb', line 164 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
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/locomotive/presentable.rb', line 179 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.
110 111 112 113 114 115 116 |
# File 'lib/locomotive/presentable.rb', line 110 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.
124 125 126 127 128 129 130 |
# File 'lib/locomotive/presentable.rb', line 124 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.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/locomotive/presentable.rb', line 139 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 |