Class: Moonrope::Structure
- Inherits:
-
Object
- Object
- Moonrope::Structure
- Defined in:
- lib/moonrope/structure.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
Attributes which should be included in this structure.
-
#base ⇒ Moonrope::Base
readonly
The base API.
-
#basic ⇒ Proc
The basic data block.
-
#doc ⇒ Bool
Should this structure be documented.
-
#dsl ⇒ Moonrope::DSL::StructureDSL
readonly
The DSL.
-
#expansions ⇒ Hash
readonly
All expansions for the structure.
-
#full ⇒ Proc
The full data block.
-
#name ⇒ Symbol
The name of the structure.
Instance Method Summary collapse
-
#all_expansions ⇒ Object
Return an array of all expansions which are available on this structure.
-
#attribute(name) ⇒ Object
Return details for the given attribute.
-
#description_for_condition(condition) ⇒ Object
Return the description for a given condition hash.
-
#hash(object, options = {}) ⇒ Hash
Return a hash for this struture.
-
#initialize(base, name) { ... } ⇒ Structure
constructor
Initialize a new structure.
Constructor Details
#initialize(base, name) { ... } ⇒ Structure
Initialize a new structure
36 37 38 39 40 41 42 43 |
# File 'lib/moonrope/structure.rb', line 36 def initialize(base, name, &block) @base = base @name = name @expansions = {} @attributes = {:basic => [], :full => [], :expansion => []} @dsl = Moonrope::DSL::StructureDSL.new(self) @dsl.instance_eval(&block) if block_given? end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
Returns attributes which should be included in this structure.
25 26 27 |
# File 'lib/moonrope/structure.rb', line 25 def attributes @attributes end |
#base ⇒ Moonrope::Base (readonly)
Returns the base API.
22 23 24 |
# File 'lib/moonrope/structure.rb', line 22 def base @base end |
#basic ⇒ Proc
Returns the basic data block.
10 11 12 |
# File 'lib/moonrope/structure.rb', line 10 def basic @basic end |
#doc ⇒ Bool
Returns should this structure be documented.
28 29 30 |
# File 'lib/moonrope/structure.rb', line 28 def doc @doc end |
#dsl ⇒ Moonrope::DSL::StructureDSL (readonly)
Returns the DSL.
16 17 18 |
# File 'lib/moonrope/structure.rb', line 16 def dsl @dsl end |
#expansions ⇒ Hash (readonly)
Returns all expansions for the structure.
19 20 21 |
# File 'lib/moonrope/structure.rb', line 19 def expansions @expansions end |
#full ⇒ Proc
Returns the full data block.
13 14 15 |
# File 'lib/moonrope/structure.rb', line 13 def full @full end |
#name ⇒ Symbol
Returns the name of the structure.
7 8 9 |
# File 'lib/moonrope/structure.rb', line 7 def name @name end |
Instance Method Details
#all_expansions ⇒ Object
Return an array of all expansions which are available on this structure
128 129 130 |
# File 'lib/moonrope/structure.rb', line 128 def all_expansions @attributes[:expansion].map(&:name) + expansions.keys end |
#attribute(name) ⇒ Object
Return details for the given attribute
48 49 50 51 52 |
# File 'lib/moonrope/structure.rb', line 48 def attribute(name) @attributes[:basic].select { |p| p.name == name }.first || @attributes[:full].select { |p| p.name == name }.first || @attributes[:expansion].select { |p| p.name == name }.first end |
#description_for_condition(condition) ⇒ Object
Return the description for a given condition hash
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/moonrope/structure.rb', line 135 def description_for_condition(condition) if condition[:authenticator] && condition[:access_rule] if authenticator = base.authenticators[condition[:authenticator]] if access_rule = authenticator.rules[condition[:access_rule]] access_rule[:description] end end else condition[:description] end end |
#hash(object, options = {}) ⇒ Hash
Return a hash for this struture
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/moonrope/structure.rb', line 61 def hash(object, = {}) # Set up an environment environment = EvalEnvironment.new(base, [:request], [:request] ? [:request].action : nil, :o => object) # Set a new hash hash = Hash.new # Add the 'basic' structured fields DeepMerge.deep_merge! hash_for_attributes(@attributes[:basic], object, environment), hash # Always get a basic hash to work from if self.basic.is_a?(Proc) DeepMerge.deep_merge! environment.instance_eval(&self.basic), hash end # Enhance with the full hash if requested if [:full] # Add the 'full' structured fields DeepMerge.deep_merge! hash_for_attributes(@attributes[:full], object, environment), hash if self.full.is_a?(Proc) full_hash = environment.instance_eval(&self.full) DeepMerge.deep_merge! full_hash,hash end end if [:attributes] hash.reject! { |k,v| ![:attributes].include?(k.to_sym) } end # Add expansions if [:expansions] if [:expansions].is_a?(Array) expansions_to_include = [:expansions].each_with_object({}) do |expan, hash| if expan.is_a?(Symbol) || expan.is_a?(String) hash[expan.to_sym] = nil elsif expan.is_a?(Hash) hash[expan.first.first.to_sym] = expan.first.last end end else expansions_to_include = true end # Add structured expansions @attributes[:expansion].each do |attribute| next if expansions_to_include.is_a?(Hash) && !expansions_to_include.keys.include?(attribute.name.to_sym) DeepMerge.deep_merge! hash_for_attributes([attribute], object, environment, :structure_opts => expansions_to_include.is_a?(Hash) && expansions_to_include[attribute.name.to_sym]), hash end # Add the expansions expansions.each do |name, expansion| next if [:expansions].is_a?(Array) && ![:expansions].include?(name.to_sym) next unless check_conditions(environment, expansion[:conditions]) DeepMerge.deep_merge!({name.to_sym => environment.instance_eval(&expansion[:block])}, hash) end end # Return the hash hash end |