Class: Cockpit::Settings::Definition
- Inherits:
-
Object
- Object
- Cockpit::Settings::Definition
- Defined in:
- lib/cockpit/core/definition.rb
Overview
This class defines default properties for a setting object, based on the DSL
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#key ⇒ Object
readonly
keys is the nested keys associated with child values.
-
#nested ⇒ Object
readonly
Returns the value of attribute nested.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validation ⇒ Object
readonly
Returns the value of attribute validation.
-
#value ⇒ Object
readonly
keys is the nested keys associated with child values.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #all_keys ⇒ Object
- #callback(name, record, new_value) ⇒ Object
- #child(key) ⇒ Object
- #each(&block) ⇒ Object
- #execute(executable, record, new_value) ⇒ Object
-
#flatten(separator = ".") ⇒ Object
map of nested key to definition.
-
#initialize(key, *args, &block) ⇒ Definition
constructor
A new instance of Definition.
- #iterate(method, &block) ⇒ Object
- #keys ⇒ Object
- #map(&block) ⇒ Object
- #nested? ⇒ Boolean
- #to_hash ⇒ Object
- #to_tree ⇒ Object
- #validate(record, new_value, &block) ⇒ Object
- #value_for(key) ⇒ Object
-
#with_callbacks(record, new_value, &block) ⇒ Object
callbacks.
Constructor Details
#initialize(key, *args, &block) ⇒ Definition
Returns a new instance of Definition.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cockpit/core/definition.rb', line 19 def initialize(key, *args, &block) @key = key.to_s @attributes = {} if block_given? @value = self.class.define!(&block) @nested = true @callbacks = {} else args = args.pop if args.is_a?(Array) if args.last.is_a?(Hash) @attributes.merge!(args.pop) end if args.last.is_a?(Class) @type = args.pop end args = args.pop if (args.length == 1) end if attributes.has_key?(:default) @value = attributes.delete(:default) else if args.is_a?(Class) @type = args else @value = args end end @validation = attributes.delete(:if) @callbacks = { :before => attributes.delete(:before), :after => attributes.delete(:after) } @type ||= @value.class @nested = false end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
16 17 18 |
# File 'lib/cockpit/core/definition.rb', line 16 def attributes @attributes end |
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
16 17 18 |
# File 'lib/cockpit/core/definition.rb', line 16 def callbacks @callbacks end |
#key ⇒ Object (readonly)
keys is the nested keys associated with child values
15 16 17 |
# File 'lib/cockpit/core/definition.rb', line 15 def key @key end |
#nested ⇒ Object (readonly)
Returns the value of attribute nested.
17 18 19 |
# File 'lib/cockpit/core/definition.rb', line 17 def nested @nested end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
16 17 18 |
# File 'lib/cockpit/core/definition.rb', line 16 def type @type end |
#validation ⇒ Object (readonly)
Returns the value of attribute validation.
16 17 18 |
# File 'lib/cockpit/core/definition.rb', line 16 def validation @validation end |
#value ⇒ Object (readonly)
keys is the nested keys associated with child values
15 16 17 |
# File 'lib/cockpit/core/definition.rb', line 15 def value @value end |
Class Method Details
.define!(options = {}, &block) ⇒ Object
7 8 9 10 11 |
# File 'lib/cockpit/core/definition.rb', line 7 def define!( = {}, &block) DefinedBy::DSL(&block).map do |key, value, dsl_block| Cockpit::Settings::Definition.new(key, value, &dsl_block) end end |
Instance Method Details
#[](key) ⇒ Object
96 97 98 |
# File 'lib/cockpit/core/definition.rb', line 96 def [](key) value_for(key) end |
#all_keys ⇒ Object
84 85 86 |
# File 'lib/cockpit/core/definition.rb', line 84 def all_keys @all_keys ||= get_keys(true, :all_keys) end |
#callback(name, record, new_value) ⇒ Object
147 148 149 |
# File 'lib/cockpit/core/definition.rb', line 147 def callback(name, record, new_value) execute(callbacks[name], record, new_value) end |
#child(key) ⇒ Object
88 89 90 |
# File 'lib/cockpit/core/definition.rb', line 88 def child(key) flatten[key.to_s] end |
#each(&block) ⇒ Object
61 62 63 |
# File 'lib/cockpit/core/definition.rb', line 61 def each(&block) iterate(:each, &block) end |
#execute(executable, record, new_value) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/cockpit/core/definition.rb', line 151 def execute(executable, record, new_value) return true unless executable case executable when String, Symbol if record.respond_to?(executable) case record.method(executable).arity when 0 record.send(executable) when 1 record.send(executable, key) when 2 record.send(executable, key, new_value) end end when Proc case executable.arity when 0, -1 if record record.instance_eval(&executable) else executable.call end when 1 if record record.instance_exec(key, &executable) else executable.call(key) end when 2 if record record.instance_exec(key, new_value, &executable) else executable.call(key, new_value) end end end end |
#flatten(separator = ".") ⇒ Object
map of nested key to definition
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/cockpit/core/definition.rb', line 101 def flatten(separator = ".") unless @flattened if nested? @flattened = value.inject({key => self}) do |hash, definition| sub_definition = definition.keys.inject({}) do |sub_hash, sub_key| sub_hash["#{key}#{separator}#{sub_key}"] = definition.child(sub_key) sub_hash end hash.merge(sub_definition) end else @flattened = {key => self} end end @flattened end |
#iterate(method, &block) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/cockpit/core/definition.rb', line 69 def iterate(method, &block) keys.send(method) do |key| case block.arity when 1 yield(key) when 2 yield(key, value_for(key)) end end end |
#keys ⇒ Object
80 81 82 |
# File 'lib/cockpit/core/definition.rb', line 80 def keys @keys ||= get_keys(false, :keys) end |
#map(&block) ⇒ Object
65 66 67 |
# File 'lib/cockpit/core/definition.rb', line 65 def map(&block) iterate(:map, &block) end |
#nested? ⇒ Boolean
130 131 132 |
# File 'lib/cockpit/core/definition.rb', line 130 def nested? self.nested == true end |
#to_hash ⇒ Object
119 120 121 122 123 124 |
# File 'lib/cockpit/core/definition.rb', line 119 def to_hash flatten.inject({}) do |hash, key, definition| hash[key] = definition.value unless definition.nested? hash end end |
#to_tree ⇒ Object
126 127 128 |
# File 'lib/cockpit/core/definition.rb', line 126 def to_tree {key => nested? ? value.map(&:to_tree) : value} end |
#validate(record, new_value, &block) ⇒ Object
143 144 145 |
# File 'lib/cockpit/core/definition.rb', line 143 def validate(record, new_value, &block) yield if execute(validation, record, new_value) end |
#value_for(key) ⇒ Object
92 93 94 |
# File 'lib/cockpit/core/definition.rb', line 92 def value_for(key) child(key).value rescue nil end |
#with_callbacks(record, new_value, &block) ⇒ Object
callbacks
135 136 137 138 139 140 141 |
# File 'lib/cockpit/core/definition.rb', line 135 def with_callbacks(record, new_value, &block) validate(record, new_value) do callback(:before, record, new_value) yield(new_value) callback(:after, record, new_value) end end |